Qudrat0708 commited on
Commit
49847a8
·
verified ·
1 Parent(s): cda2dbe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import whisper
3
+ from gtts import gTTS
4
+ import gradio as gr
5
+ from groq import Groq
6
+ from googletrans import Translator
7
+
8
+ GROQ_API_KEY = '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'
9
+
10
+ # Load Whisper model for transcription
11
+ model = whisper.load_model("base")
12
+
13
+ # Set up Groq API client
14
+ client = Groq(api_key=GROQ_API_KEY)
15
+
16
+ # Translator for English to Urdu translation
17
+ translator = Translator()
18
+
19
+ # Function to get the LLM response from Groq
20
+ def get_llm_response(user_input):
21
+ chat_completion = client.chat.completions.create(
22
+ messages=[{"role": "user", "content": user_input}],
23
+ model="llama3-8b-8192", # Replace with your desired model
24
+ )
25
+ return chat_completion.choices[0].message.content
26
+
27
+ # Function to convert text to speech using gTTS
28
+ def text_to_speech(text, output_audio="output_audio.mp3"):
29
+ tts = gTTS(text, lang='ur')
30
+ tts.save(output_audio)
31
+ return output_audio
32
+
33
+ # Main chatbot function to handle audio input and output
34
+ def chatbot(audio):
35
+ # Step 1: Transcribe the audio using Whisper
36
+ result = model.transcribe(audio)
37
+ user_text = result["text"]
38
+
39
+ # Step 2: Get LLM response from Groq
40
+ response_text = get_llm_response(user_text)
41
+
42
+ # Step 3: Translate response to Urdu
43
+ translated_text = translator.translate(response_text, src='en', dest='ur').text
44
+
45
+ # Step 4: Convert the translated text to speech
46
+ output_audio = text_to_speech(translated_text)
47
+
48
+ return translated_text, output_audio
49
+
50
+ # Gradio interface for real-time interaction
51
+ iface = gr.Interface(
52
+ fn=chatbot,
53
+ inputs=gr.Audio(type="filepath"), # Input from mic or file
54
+ outputs=[gr.Textbox(), gr.Audio(type="filepath")], # Output: response text and audio
55
+ live=True
56
+ )
57
+
58
+ # Launch the Gradio app
59
+ iface.launch()