Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from openai import OpenAI | |
| from deep_translator import GoogleTranslator | |
| import os | |
| # Initialize OpenAI client | |
| client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) | |
| def translate_text(text, target_lang): | |
| translator = GoogleTranslator(source='auto', target=target_lang) | |
| return translator.translate(text) | |
| def get_llm_response(prompt): | |
| response = client.chat.completions.create( | |
| model="gpt-4-0125-preview", | |
| messages=[ | |
| {"role": "system", "content": "You are a helpful assistant that provides informative and concise responses."}, | |
| {"role": "user", "content": prompt} | |
| ], | |
| max_tokens=150 | |
| ) | |
| return response.choices[0].message.content.strip() | |
| def process_input(input_text, output_lang): | |
| # Translate input to English | |
| english_query = translate_text(input_text, "en") | |
| # Get response from LLM | |
| llm_response = get_llm_response(english_query) | |
| # Translate LLM response to desired output language | |
| final_response = translate_text(llm_response, output_lang) | |
| return input_text, english_query, llm_response, final_response | |
| # Gradio interface | |
| # Gradio interface | |
| iface = gr.Interface( | |
| fn=process_voice_or_text, | |
| inputs=[ | |
| gr.Audio(type="filepath", label="Voice Input (Tamil or English)"), | |
| gr.Textbox(label="Text Input (Tamil or English)", placeholder="Type your input here..."), | |
| gr.Radio(["ta", "en"], label="Output Language") | |
| ], | |
| outputs=[ | |
| gr.Textbox(label="Original Input"), | |
| gr.Textbox(label="Translated English Query"), | |
| gr.Textbox(label="LLM Response (English)"), | |
| gr.Textbox(label="Final Response (Tamil/English)") | |
| ], | |
| live=True, | |
| title="Nisha - Tamil-English Voice Assistant", | |
| description="Speak or type in Tamil or English, and get responses in your preferred language!" | |
| ) | |
| iface.launch() | |