Update app.py
Browse files
app.py
CHANGED
@@ -104,19 +104,16 @@ def pdf_chat(pdf_file, text_query, temperature, top_p, max_output_tokens):
|
|
104 |
return f"Error processing the PDF: {str(e)}"
|
105 |
|
106 |
# Function to transcribe audio to text using OpenAI Whisper API
|
107 |
-
def transcribe_audio(
|
108 |
if not openai_api_key:
|
109 |
return "Error: No API key provided."
|
110 |
|
111 |
openai.api_key = openai_api_key
|
112 |
|
113 |
try:
|
114 |
-
#
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
# Transcribe the audio to text using OpenAI's whisper model
|
119 |
-
audio_file_transcription = openai.Audio.transcribe(file=audio_file_obj, model="whisper-1")
|
120 |
return audio_file_transcription.text
|
121 |
except Exception as e:
|
122 |
return f"Error transcribing audio: {str(e)}"
|
@@ -173,8 +170,8 @@ with gr.Blocks() as demo:
|
|
173 |
pdf_button = gr.Button("Ask")
|
174 |
|
175 |
with gr.Tab("Voice Chat"):
|
176 |
-
# Record Audio Component for Voice Chat
|
177 |
-
audio_record = gr.Audio(label="Record your Voice", type="
|
178 |
# Upload Audio File Component
|
179 |
audio_upload = gr.File(label="Or Upload an Audio File", type="file", file_types=["audio/wav", "audio/mp3"])
|
180 |
audio_query = gr.Textbox(label="Ask about the transcription")
|
@@ -198,7 +195,7 @@ with gr.Blocks() as demo:
|
|
198 |
return "Please either record or upload an audio file."
|
199 |
|
200 |
# Process the audio (either from recording or upload)
|
201 |
-
transcription = transcribe_audio(audio, api_key)
|
202 |
if transcription.startswith("Error"):
|
203 |
return transcription # Return transcription error
|
204 |
return query_openai(
|
|
|
104 |
return f"Error processing the PDF: {str(e)}"
|
105 |
|
106 |
# Function to transcribe audio to text using OpenAI Whisper API
|
107 |
+
def transcribe_audio(audio_filepath, openai_api_key):
|
108 |
if not openai_api_key:
|
109 |
return "Error: No API key provided."
|
110 |
|
111 |
openai.api_key = openai_api_key
|
112 |
|
113 |
try:
|
114 |
+
# Open the audio file and transcribe using OpenAI's Whisper model
|
115 |
+
with open(audio_filepath, "rb") as audio_file:
|
116 |
+
audio_file_transcription = openai.Audio.transcribe(file=audio_file, model="whisper-1")
|
|
|
|
|
|
|
117 |
return audio_file_transcription.text
|
118 |
except Exception as e:
|
119 |
return f"Error transcribing audio: {str(e)}"
|
|
|
170 |
pdf_button = gr.Button("Ask")
|
171 |
|
172 |
with gr.Tab("Voice Chat"):
|
173 |
+
# Record Audio Component for Voice Chat
|
174 |
+
audio_record = gr.Audio(label="Record your Voice", type="filepath", show_label=True)
|
175 |
# Upload Audio File Component
|
176 |
audio_upload = gr.File(label="Or Upload an Audio File", type="file", file_types=["audio/wav", "audio/mp3"])
|
177 |
audio_query = gr.Textbox(label="Ask about the transcription")
|
|
|
195 |
return "Please either record or upload an audio file."
|
196 |
|
197 |
# Process the audio (either from recording or upload)
|
198 |
+
transcription = transcribe_audio(audio.name, api_key)
|
199 |
if transcription.startswith("Error"):
|
200 |
return transcription # Return transcription error
|
201 |
return query_openai(
|