Spaces:
Sleeping
Sleeping
| 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() | |