File size: 1,898 Bytes
93d8b63
f63c480
949eaba
93d8b63
 
f63c480
 
93d8b63
b10c901
 
 
93d8b63
 
f63c480
 
 
 
 
 
93d8b63
 
5230299
93d8b63
b10c901
 
 
93d8b63
 
 
 
 
 
 
b10c901
93d8b63
7385882
93d8b63
 
7385882
93d8b63
7385882
 
93d8b63
 
 
b10c901
93d8b63
 
 
 
7385882
 
 
93d8b63
 
949eaba
 
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
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()