Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,31 +9,39 @@ import os
|
|
| 9 |
# Ensure nltk resources are downloaded
|
| 10 |
nltk.download("punkt")
|
| 11 |
|
|
|
|
| 12 |
def transcribe(audio_path):
|
|
|
|
| 13 |
with open(audio_path, "rb") as audio_file:
|
| 14 |
audio_data = audio_file.read()
|
| 15 |
|
|
|
|
| 16 |
groq_api_endpoint = "https://api.groq.com/openai/v1/audio/transcriptions"
|
|
|
|
| 17 |
headers = {
|
| 18 |
"Authorization": "Bearer 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", # Replace with your actual API key
|
| 19 |
}
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
data = {
|
| 22 |
'model': 'whisper-large-v3-turbo',
|
| 23 |
'response_format': 'json',
|
| 24 |
'language': 'en',
|
| 25 |
}
|
| 26 |
|
| 27 |
-
# Send audio to Groq API
|
| 28 |
response = requests.post(groq_api_endpoint, headers=headers, files=files, data=data)
|
|
|
|
| 29 |
if response.status_code == 200:
|
| 30 |
result = response.json()
|
| 31 |
-
|
|
|
|
| 32 |
else:
|
| 33 |
error_msg = response.json().get("error", {}).get("message", "Unknown error.")
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
# Function to generate notes and questions
|
| 39 |
def generate_notes(transcript):
|
|
@@ -102,8 +110,15 @@ def create_pdf(transcript, long_questions, short_questions, mcqs):
|
|
| 102 |
return pdf_path
|
| 103 |
|
| 104 |
# Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
iface = gr.Interface(
|
| 106 |
-
fn=
|
| 107 |
inputs=gr.Audio(type="filepath"),
|
| 108 |
outputs=gr.File(label="Download PDF with Notes and Questions"),
|
| 109 |
title="Voice to Text Converter and Notes Generator",
|
|
|
|
| 9 |
# Ensure nltk resources are downloaded
|
| 10 |
nltk.download("punkt")
|
| 11 |
|
| 12 |
+
# Function to send audio to Groq API and get transcription
|
| 13 |
def transcribe(audio_path):
|
| 14 |
+
# Read audio file in binary mode
|
| 15 |
with open(audio_path, "rb") as audio_file:
|
| 16 |
audio_data = audio_file.read()
|
| 17 |
|
| 18 |
+
# Groq API endpoint for audio transcription
|
| 19 |
groq_api_endpoint = "https://api.groq.com/openai/v1/audio/transcriptions"
|
| 20 |
+
|
| 21 |
headers = {
|
| 22 |
"Authorization": "Bearer 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", # Replace with your actual API key
|
| 23 |
}
|
| 24 |
+
|
| 25 |
+
files = {
|
| 26 |
+
'file': ('audio.wav', audio_data, 'audio/wav'),
|
| 27 |
+
}
|
| 28 |
data = {
|
| 29 |
'model': 'whisper-large-v3-turbo',
|
| 30 |
'response_format': 'json',
|
| 31 |
'language': 'en',
|
| 32 |
}
|
| 33 |
|
| 34 |
+
# Send audio to Groq API
|
| 35 |
response = requests.post(groq_api_endpoint, headers=headers, files=files, data=data)
|
| 36 |
+
|
| 37 |
if response.status_code == 200:
|
| 38 |
result = response.json()
|
| 39 |
+
transcript = result.get("text", "No transcription available.")
|
| 40 |
+
return generate_notes(transcript)
|
| 41 |
else:
|
| 42 |
error_msg = response.json().get("error", {}).get("message", "Unknown error.")
|
| 43 |
+
print(f"API Error: {error_msg}")
|
| 44 |
+
return None # Indicate failure
|
|
|
|
| 45 |
|
| 46 |
# Function to generate notes and questions
|
| 47 |
def generate_notes(transcript):
|
|
|
|
| 110 |
return pdf_path
|
| 111 |
|
| 112 |
# Gradio interface
|
| 113 |
+
def gradio_interface(audio_path):
|
| 114 |
+
pdf_path = transcribe(audio_path)
|
| 115 |
+
if pdf_path:
|
| 116 |
+
return pdf_path
|
| 117 |
+
else:
|
| 118 |
+
return "Error: Unable to process the audio file. Please check the API key and try again."
|
| 119 |
+
|
| 120 |
iface = gr.Interface(
|
| 121 |
+
fn=gradio_interface,
|
| 122 |
inputs=gr.Audio(type="filepath"),
|
| 123 |
outputs=gr.File(label="Download PDF with Notes and Questions"),
|
| 124 |
title="Voice to Text Converter and Notes Generator",
|