Safwanahmad619 commited on
Commit
586d983
·
verified ·
1 Parent(s): f0ad67c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -40
app.py CHANGED
@@ -55,8 +55,6 @@
55
 
56
  # iface.launch()
57
 
58
-
59
-
60
  import os
61
  import gradio as gr
62
  import whisper
@@ -66,51 +64,53 @@ from groq import Groq
66
 
67
  # Initialize the Groq client
68
  groq_api_key = os.getenv('GROQ_API_KEY')
69
- client = Groq(api_key=groq_api_key)
 
 
70
 
71
  # Load the Whisper model
72
  model = whisper.load_model("base") # You can choose other models like "small", "medium", "large"
73
 
74
  def process_audio(file_path):
75
- if not file_path:
76
- return "Please upload an audio file.", None
77
- try:
78
- # Load the audio file
79
- audio = whisper.load_audio(file_path)
80
-
81
- # Transcribe the audio using Whisper
82
- result = model.transcribe(audio)
83
- text = result["text"]
84
-
85
- # Generate a response using Groq
86
- chat_completion = client.chat.completions.create(
87
- messages=[{"role": "user", "content": text}],
88
- model="llama3-8b-8192", # Replace with the correct model if necessary
89
- )
90
-
91
- # Access the response using dot notation
92
- response_message = chat_completion.choices[0].message.content.strip()
93
-
94
- # Convert the response text to speech
95
- tts = gTTS(response_message)
96
- response_audio_io = io.BytesIO()
97
- tts.write_to_fp(response_audio_io) # Save the audio to the BytesIO object
98
- response_audio_io.seek(0)
99
-
100
- # Save audio to a file to ensure it's generated correctly
101
- with open("response.mp3", "wb") as audio_file:
102
- audio_file.write(response_audio_io.getvalue())
103
-
104
- # Return the response text and the path to the saved audio file
105
- return response_message, "response.mp3"
106
- except Exception as e:
107
- return f"An error occurred: {e}", None
108
 
109
  iface = gr.Interface(
110
- fn=process_audio,
111
- inputs=gr.Audio(type="filepath"), # Use type="filepath"
112
- outputs=[gr.Textbox(label="Response Text"), gr.Audio(label="Response Audio")],
113
- live=True
114
  )
115
 
116
  iface.launch()
 
55
 
56
  # iface.launch()
57
 
 
 
58
  import os
59
  import gradio as gr
60
  import whisper
 
64
 
65
  # Initialize the Groq client
66
  groq_api_key = os.getenv('GROQ_API_KEY')
67
+ if not groq_api_key:
68
+ raise ValueError("GROQ_API_KEY environment variable is not set.")
69
+ client = Groq(api_key=groq_api_key)
70
 
71
  # Load the Whisper model
72
  model = whisper.load_model("base") # You can choose other models like "small", "medium", "large"
73
 
74
  def process_audio(file_path):
75
+ try:
76
+ # Load the audio file
77
+ audio = whisper.load_audio(file_path)
78
+
79
+ # Transcribe the audio using Whisper
80
+ result = model.transcribe(audio)
81
+ text = result["text"]
82
+
83
+ # Generate a response using Groq
84
+ chat_completion = client.chat.completions.create(
85
+ messages=[{"role": "user", "content": text}],
86
+ model="llama3-8b-8192", # Replace with the correct model if necessary
87
+ )
88
+
89
+ # Access the response using dot notation
90
+ response_message = chat_completion.choices[0].message.content.strip()
91
+
92
+ # Convert the response text to speech
93
+ tts = gTTS(response_message)
94
+ response_audio_io = io.BytesIO()
95
+ tts.write_to_fp(response_audio_io) # Save the audio to the BytesIO object
96
+ response_audio_io.seek(0)
97
+
98
+ # Save audio to a file to ensure it's generated correctly
99
+ response_audio_path = "response.mp3"
100
+ with open(response_audio_path, "wb") as audio_file:
101
+ audio_file.write(response_audio_io.getvalue())
102
+
103
+ # Return the response text and the path to the saved audio file
104
+ return response_message, response_audio_path
105
+
106
+ except Exception as e:
107
+ return f"An error occurred: {e}", None
108
 
109
  iface = gr.Interface(
110
+ fn=process_audio,
111
+ inputs=gr.Audio(type="filepath"), # Use type="filepath"
112
+ outputs=[gr.Textbox(label="Response Text"), gr.Audio(label="Response Audio")],
113
+ live=True
114
  )
115
 
116
  iface.launch()