Qudrat0708 commited on
Commit
3a0036c
·
verified ·
1 Parent(s): 9067e63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -3,18 +3,18 @@ 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):
@@ -26,32 +26,32 @@ def get_llm_response(user_input):
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
 
 
3
  from gtts import gTTS
4
  import gradio as gr
5
  from groq import Groq
6
+ from transformers import pipeline
7
 
8
  GROQ_API_KEY = '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'
9
 
10
+ # Load Whisper model for transcription (use a multilingual model to support Urdu)
11
+ model = whisper.load_model("large") # Use "large" or "multilingual" for better Urdu support
12
 
13
  # Set up Groq API client
14
  client = Groq(api_key=GROQ_API_KEY)
15
 
16
+ # Load the translation model
17
+ translator = pipeline("translation_en_to_ur", model="Helsinki-NLP/opus-mt-en-ur")
18
 
19
  # Function to get the LLM response from Groq
20
  def get_llm_response(user_input):
 
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') # Use 'ur' for Urdu
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 Urdu audio using Whisper
36
+ result = model.transcribe(audio, language="ur") # Specify Urdu language
37
  user_text = result["text"]
38
 
39
+ # Step 2: Get LLM response from Groq (in English)
40
  response_text = get_llm_response(user_text)
41
 
42
+ # Step 3: Translate the response from English to Urdu
43
+ translated_response = translator(response_text)[0]['translation_text']
44
 
45
+ # Step 4: Convert the translated response text to Urdu speech
46
+ output_audio = text_to_speech(translated_response)
47
 
48
+ return translated_response, output_audio
49
 
50
+ # Gradio interface for real-time interaction with live microphone input
51
  iface = gr.Interface(
52
  fn=chatbot,
53
+ inputs=gr.Audio(type="filepath"),
54
+ outputs=[gr.Textbox(), gr.Audio(type="filepath")], # Output: Urdu text and audio
55
  live=True
56
  )
57