File size: 4,071 Bytes
b744e5b
4f95263
 
 
 
b744e5b
21763af
 
429d1a4
4f95263
 
7559acc
4f95263
21763af
4f95263
 
 
7559acc
4f95263
 
7559acc
 
4f95263
 
7559acc
21763af
7559acc
21763af
 
 
 
 
 
7559acc
 
 
 
 
21763af
7559acc
 
21763af
7559acc
331515a
4f95263
 
 
 
 
331515a
4f95263
 
 
 
 
429d1a4
4f95263
 
 
 
 
 
429d1a4
4f95263
 
 
 
 
429d1a4
4f95263
 
 
 
 
 
 
429d1a4
4dfab9c
 
 
 
 
 
 
 
 
 
 
 
429d1a4
4dfab9c
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import requests
from gtts import gTTS
import gradio as gr
import speech_recognition as sr

# Set your API key for Together API
os.environ["TOGETHER_API_KEY"] = "tgp_v1_YpR1d9dFBA6bLBE3L5dKYMj53VQ1LFI9ag9SqeV9Cmk"  # Replace with your actual API key

# Function to get chatbot response using the Together API
def get_chatbot_response(user_input):
    url = "https://api.together.ai/v1/chat/completions"  # Ensure this URL is correct
    headers = {
        "Authorization": f"Bearer {os.environ['TOGETHER_API_KEY']}",  # Ensure your API key is correct
        "Content-Type": "application/json",
    }
    data = {
        "model": "Qwen/Qwen3-Coder-480B-A35B-Instruct-FP8",  # Ensure this model name is correct
        "messages": [{"role": "user", "content": user_input}],
    }

    # Send POST request to the API
    response = requests.post(url, json=data, headers=headers)
    
    # Debugging: print the status code and response content
    print(f"Status Code: {response.status_code}")  # Debugging status code
    try:
        response_data = response.json()  # Try to parse the response
        print(f"API Response: {response_data}")  # Debugging the API response
    except ValueError:
        print("Error: Unable to parse JSON response.")
        return "Error: Unable to parse the response from the API."

    # Check if the API call was successful
    if response.status_code == 200:
        if 'choices' in response_data:
            return response_data['choices'][0]['message']['content']
        else:
            print("Error: No valid 'choices' found in response.")
            return "Error: No valid response found."
    else:
        print(f"Error: API request failed with status code {response.status_code}.")
        return f"Error: API request failed with status code {response.status_code}"

# Text-to-Speech (TTS) using gTTS
def speak_text(text):
    tts = gTTS(text=text, lang='en')
    tts.save("response.mp3")
    return "response.mp3"  # Return the file path for Gradio to display

# Function to recognize speech and convert it to text
def recognize_speech_from_audio(audio_file):
    recognizer = sr.Recognizer()
    with sr.AudioFile(audio_file) as source:
        audio_data = recognizer.record(source)
    try:
        text = recognizer.recognize_google(audio_data)
        return text
    except sr.UnknownValueError:
        return "Sorry, I didn't catch that."
    except sr.RequestError:
        return "Sorry, there was an error with the speech recognition service."

# Combined function to handle text and voice input and return text + audio output
def chat_with_bot(text_input, audio_input):
    # If audio input is provided, convert it to text
    if audio_input:
        text_input = recognize_speech_from_audio(audio_input)
    
    # Get the chatbot response
    chatbot_response = get_chatbot_response(text_input)
    
    # Convert chatbot response to speech
    audio_file = speak_text(chatbot_response)
    
    return chatbot_response, audio_file

# Gradio interface to accept both text and voice input and return both text and audio output
def create_interface():
    interface = gr.Interface(fn=chat_with_bot,
                             inputs=[gr.Textbox(lines=2, placeholder="Ask something...", label="Your Question"),
                                     gr.Audio(type="filepath", label="Speak Your Question")],
                             outputs=[gr.Textbox(label="Chatbot Response"), 
                                      gr.Audio(label="Chatbot Voice Response")],
                             live=True,
                             theme="compact",  # You can experiment with other themes as well
                             title="AI Chatbot with Voice and Text Input/Output",
                             description="This is an AI-powered chatbot that accepts both voice and text input and provides both text and voice responses.",
                             allow_flagging="never")

    return interface

# Launch the interface
if __name__ == "__main__":
    create_interface().launch()