rizwanaidris2 commited on
Commit
d0aff52
·
verified ·
1 Parent(s): 494ce6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -50
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  # Import libraries
2
  import whisper
3
  import os
@@ -5,78 +6,50 @@ from gtts import gTTS
5
  import gradio as gr
6
  from groq import Groq
7
 
8
- pip install torch
9
- pip install --upgrade gradio
10
-
11
-
12
-
13
  # Load Whisper model for transcription
14
  model = whisper.load_model("base")
15
 
16
  # Set up Groq API client (ensure GROQ_API_KEY is set in your environment)
17
- GROQ_API_KEY = "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"
 
18
  client = Groq(api_key=GROQ_API_KEY)
19
 
 
20
  # Function to get the LLM response from Groq
21
  def get_llm_response(user_input):
22
- try:
23
- chat_completion = client.chat.completions.create(
24
- messages=[{"role": "user", "content": user_input}],
25
- model="llama3-8b-8192" # Replace with your desired model
26
- )
27
- return chat_completion.choices[0].message.content
28
- except Exception as e:
29
- return f"Error: {e}"
30
 
31
  # Function to convert text to speech using gTTS
32
  def text_to_speech(text, output_audio="output_audio.mp3"):
33
- try:
34
- tts = gTTS(text)
35
- tts.save(output_audio)
36
- return output_audio
37
- except Exception as e:
38
- return f"Error generating audio: {e}"
39
 
40
  # Main chatbot function to handle audio input and output
41
  def chatbot(audio):
42
  # Step 1: Transcribe the audio using Whisper
43
- try:
44
- result = model.transcribe(audio)
45
- user_text = result["text"]
46
- except Exception as e:
47
- return f"Error in transcription: {e}", None
48
 
49
  # Step 2: Get LLM response from Groq
50
  response_text = get_llm_response(user_text)
51
 
52
  # Step 3: Convert the response text to speech
53
  output_audio = text_to_speech(response_text)
54
-
55
  return response_text, output_audio
56
 
57
- # Gradio interface for dual input (microphone and file upload)
58
- with gr.Blocks() as demo:
59
- gr.Markdown("# Voice-to-Voice Chatbot")
60
-
61
- # Input section
62
- with gr.Row():
63
- microphone_input = gr.Audio(source="microphone", type="filepath", label="Speak into the microphone")
64
- file_upload_input = gr.Audio(source="upload", type="filepath", label="Upload an audio file")
65
-
66
- # Output section
67
- chatbot_response_text = gr.Textbox(label="Chatbot Response")
68
- chatbot_response_audio = gr.Audio(type="filepath", label="Response Audio")
69
-
70
- # Buttons to process each input
71
- mic_button = gr.Button("Process Microphone Input")
72
- file_button = gr.Button("Process Uploaded Audio")
73
-
74
- # Actions for buttons
75
- mic_button.click(chatbot, inputs=microphone_input, outputs=[chatbot_response_text, chatbot_response_audio])
76
- file_button.click(chatbot, inputs=file_upload_input, outputs=[chatbot_response_text, chatbot_response_audio])
77
 
78
  # Launch the Gradio app
79
- demo.launch()
80
-
81
-
82
-
 
1
+
2
  # Import libraries
3
  import whisper
4
  import os
 
6
  import gradio as gr
7
  from groq import Groq
8
 
 
 
 
 
 
9
  # Load Whisper model for transcription
10
  model = whisper.load_model("base")
11
 
12
  # Set up Groq API client (ensure GROQ_API_KEY is set in your environment)
13
+ #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
14
+ GROQ_API_KEY = "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"
15
  client = Groq(api_key=GROQ_API_KEY)
16
 
17
+
18
  # Function to get the LLM response from Groq
19
  def get_llm_response(user_input):
20
+ chat_completion = client.chat.completions.create(
21
+ messages=[{"role": "user", "content": user_input}],
22
+ model="llama3-8b-8192", # Replace with your desired model
23
+ )
24
+ return chat_completion.choices[0].message.content
 
 
 
25
 
26
  # Function to convert text to speech using gTTS
27
  def text_to_speech(text, output_audio="output_audio.mp3"):
28
+ tts = gTTS(text)
29
+ tts.save(output_audio)
30
+ return output_audio
 
 
 
31
 
32
  # Main chatbot function to handle audio input and output
33
  def chatbot(audio):
34
  # Step 1: Transcribe the audio using Whisper
35
+ result = model.transcribe(audio)
36
+ user_text = result["text"]
 
 
 
37
 
38
  # Step 2: Get LLM response from Groq
39
  response_text = get_llm_response(user_text)
40
 
41
  # Step 3: Convert the response text to speech
42
  output_audio = text_to_speech(response_text)
43
+
44
  return response_text, output_audio
45
 
46
+ # Gradio interface for real-time interaction
47
+ iface = gr.Interface(
48
+ fn=chatbot,
49
+ inputs=gr.Audio("microphone", type="filepath"), # Input from mic
50
+ outputs=[gr.Textbox(), gr.Audio(type="filepath")], # Output: response text and audio
51
+ live=True
52
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  # Launch the Gradio app
55
+ iface.launch()