Enrique Cardoza
commited on
Commit
·
4d3ad0e
1
Parent(s):
0060871
revert: restore code to a3e6bb5
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ from groq import Groq
|
|
4 |
import tempfile
|
5 |
import requests
|
6 |
import urllib.parse
|
7 |
-
import json
|
8 |
|
9 |
def validate_file(file):
|
10 |
"""Validate uploaded file type and size."""
|
@@ -64,19 +63,7 @@ def validate_url_file(url):
|
|
64 |
except Exception as e:
|
65 |
return False, f"Error validating URL: {str(e)}"
|
66 |
|
67 |
-
def
|
68 |
-
"""Extract headers from the request object."""
|
69 |
-
if request is None:
|
70 |
-
return {"message": "No request object available"}
|
71 |
-
|
72 |
-
try:
|
73 |
-
# Extract all headers from the request
|
74 |
-
headers = {key: value for key, value in request.headers.items()}
|
75 |
-
return headers
|
76 |
-
except Exception as e:
|
77 |
-
return {"error": f"Error extracting headers: {str(e)}"}
|
78 |
-
|
79 |
-
def transcribe_audio(audio_file, api_key, request: gr.Request = None):
|
80 |
"""Transcribe audio/video files into text using Groq's Whisper model.
|
81 |
|
82 |
This tool converts spoken content from audio and video files into written text.
|
@@ -88,7 +75,6 @@ def transcribe_audio(audio_file, api_key, request: gr.Request = None):
|
|
88 |
Maximum size: 25MB.
|
89 |
api_key: Your Groq API key, required for authentication.
|
90 |
You can obtain this from https://console.groq.com/
|
91 |
-
request: The Gradio request object containing headers.
|
92 |
|
93 |
Returns:
|
94 |
A text transcript of the spoken content in the audio file.
|
@@ -97,31 +83,12 @@ def transcribe_audio(audio_file, api_key, request: gr.Request = None):
|
|
97 |
Upload a podcast episode to get a complete text transcript.
|
98 |
"""
|
99 |
try:
|
100 |
-
# Log request headers if available
|
101 |
-
headers = {}
|
102 |
-
if request is not None:
|
103 |
-
headers = {key: value for key, value in request.headers.items()}
|
104 |
-
print(f"Request Headers: {json.dumps(headers, indent=2)}")
|
105 |
-
|
106 |
-
# Check for Authorization header
|
107 |
-
auth_header = request.headers.get('Authorization')
|
108 |
-
if auth_header and auth_header.startswith('Bearer '):
|
109 |
-
# You could use the token from the header here
|
110 |
-
token = auth_header[7:] # Remove 'Bearer ' prefix
|
111 |
-
print(f"Authorization token received: {token[:10]}...")
|
112 |
-
|
113 |
# First check for environment variable, then use provided API key
|
114 |
actual_api_key = os.environ.get("GROQ_API_KEY", api_key)
|
115 |
|
116 |
-
# Check if API key is in Authorization header
|
117 |
-
if not actual_api_key and request is not None:
|
118 |
-
auth_header = request.headers.get('Authorization')
|
119 |
-
if auth_header and auth_header.startswith('Bearer '):
|
120 |
-
actual_api_key = auth_header[7:] # Remove 'Bearer ' prefix
|
121 |
-
|
122 |
# Validate API key
|
123 |
if not actual_api_key:
|
124 |
-
return "Error: Please provide your Groq API key or set the GROQ_API_KEY environment variable
|
125 |
|
126 |
if audio_file is None:
|
127 |
return "Error: Please upload an audio or video file"
|
@@ -147,7 +114,7 @@ def transcribe_audio(audio_file, api_key, request: gr.Request = None):
|
|
147 |
except Exception as e:
|
148 |
return f"Error: {str(e)}"
|
149 |
|
150 |
-
def transcribe_audio_from_url(audio_url, api_key
|
151 |
"""Transcribe audio/video files from a URL into text using Groq's Whisper model.
|
152 |
|
153 |
This tool converts spoken content from audio and video files into written text.
|
@@ -159,7 +126,6 @@ def transcribe_audio_from_url(audio_url, api_key, request: gr.Request = None):
|
|
159 |
Maximum size: 25MB.
|
160 |
api_key: Your Groq API key, required for authentication.
|
161 |
You can obtain this from https://console.groq.com/
|
162 |
-
request: The Gradio request object containing headers.
|
163 |
|
164 |
Returns:
|
165 |
A text transcript of the spoken content in the audio file.
|
@@ -168,31 +134,12 @@ def transcribe_audio_from_url(audio_url, api_key, request: gr.Request = None):
|
|
168 |
Provide a URL to a podcast episode to get a complete text transcript.
|
169 |
"""
|
170 |
try:
|
171 |
-
# Log request headers if available
|
172 |
-
headers = {}
|
173 |
-
if request is not None:
|
174 |
-
headers = {key: value for key, value in request.headers.items()}
|
175 |
-
print(f"Request Headers: {json.dumps(headers, indent=2)}")
|
176 |
-
|
177 |
-
# Check for Authorization header
|
178 |
-
auth_header = request.headers.get('Authorization')
|
179 |
-
if auth_header and auth_header.startswith('Bearer '):
|
180 |
-
# You could use the token from the header here
|
181 |
-
token = auth_header[7:] # Remove 'Bearer ' prefix
|
182 |
-
print(f"Authorization token received: {token[:10]}...")
|
183 |
-
|
184 |
# First check for environment variable, then use provided API key
|
185 |
actual_api_key = os.environ.get("GROQ_API_KEY", api_key)
|
186 |
|
187 |
-
# Check if API key is in Authorization header
|
188 |
-
if not actual_api_key and request is not None:
|
189 |
-
auth_header = request.headers.get('Authorization')
|
190 |
-
if auth_header and auth_header.startswith('Bearer '):
|
191 |
-
actual_api_key = auth_header[7:] # Remove 'Bearer ' prefix
|
192 |
-
|
193 |
# Validate API key
|
194 |
if not actual_api_key:
|
195 |
-
return "Error: Please provide your Groq API key or set the GROQ_API_KEY environment variable
|
196 |
|
197 |
if not audio_url or audio_url.strip() == "":
|
198 |
return "Error: Please provide a URL to an audio or video file"
|
@@ -241,28 +188,6 @@ def transcribe_audio_from_url(audio_url, api_key, request: gr.Request = None):
|
|
241 |
except Exception as e:
|
242 |
return f"Error: {str(e)}"
|
243 |
|
244 |
-
# Create a dedicated endpoint for viewing request headers
|
245 |
-
def view_headers(request: gr.Request = None):
|
246 |
-
"""View all request headers.
|
247 |
-
|
248 |
-
This function displays all the headers sent in the HTTP request.
|
249 |
-
|
250 |
-
Parameters:
|
251 |
-
request: The Gradio request object.
|
252 |
-
|
253 |
-
Returns:
|
254 |
-
A formatted string containing all request headers.
|
255 |
-
"""
|
256 |
-
if request is None:
|
257 |
-
return "No request object available"
|
258 |
-
|
259 |
-
try:
|
260 |
-
# Extract all headers
|
261 |
-
headers = {key: value for key, value in request.headers.items()}
|
262 |
-
return json.dumps(headers, indent=2)
|
263 |
-
except Exception as e:
|
264 |
-
return f"Error extracting headers: {str(e)}"
|
265 |
-
|
266 |
# Create the Gradio interface with custom layout
|
267 |
with gr.Blocks(title="Audio/Video Transcription with Groq", theme=gr.themes.Soft()) as demo:
|
268 |
gr.Markdown("# 🎵 Audio/Video Transcription with Groq Whisper")
|
@@ -357,26 +282,8 @@ with gr.Blocks(title="Audio/Video Transcription with Groq", theme=gr.themes.Soft
|
|
357 |
show_copy_button=True,
|
358 |
interactive=False
|
359 |
)
|
360 |
-
|
361 |
-
# Tab 3: Request Headers
|
362 |
-
with gr.TabItem("Request Headers"):
|
363 |
-
with gr.Row():
|
364 |
-
with gr.Column():
|
365 |
-
gr.Markdown("### 🔍 View Request Headers")
|
366 |
-
gr.Markdown("Click the button below to view all headers sent in the current request.")
|
367 |
-
|
368 |
-
view_headers_btn = gr.Button(
|
369 |
-
"👁️ View Headers",
|
370 |
-
variant="primary",
|
371 |
-
size="lg"
|
372 |
-
)
|
373 |
-
|
374 |
-
headers_output = gr.JSON(
|
375 |
-
label="Request Headers",
|
376 |
-
value={"message": "Click the button to view headers"}
|
377 |
-
)
|
378 |
|
379 |
-
# Connect the buttons to their respective functions
|
380 |
upload_transcribe_btn.click(
|
381 |
fn=transcribe_audio,
|
382 |
inputs=[audio_input, api_key_input],
|
@@ -391,12 +298,6 @@ with gr.Blocks(title="Audio/Video Transcription with Groq", theme=gr.themes.Soft
|
|
391 |
show_progress=True
|
392 |
)
|
393 |
|
394 |
-
view_headers_btn.click(
|
395 |
-
fn=view_headers,
|
396 |
-
inputs=[],
|
397 |
-
outputs=headers_output
|
398 |
-
)
|
399 |
-
|
400 |
# Add examples section
|
401 |
gr.Markdown("### 🔗 Useful Links")
|
402 |
gr.Markdown("""
|
@@ -406,4 +307,4 @@ with gr.Blocks(title="Audio/Video Transcription with Groq", theme=gr.themes.Soft
|
|
406 |
""")
|
407 |
|
408 |
if __name__ == "__main__":
|
409 |
-
demo.launch(mcp_server=True)
|
|
|
4 |
import tempfile
|
5 |
import requests
|
6 |
import urllib.parse
|
|
|
7 |
|
8 |
def validate_file(file):
|
9 |
"""Validate uploaded file type and size."""
|
|
|
63 |
except Exception as e:
|
64 |
return False, f"Error validating URL: {str(e)}"
|
65 |
|
66 |
+
def transcribe_audio(audio_file, api_key):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
"""Transcribe audio/video files into text using Groq's Whisper model.
|
68 |
|
69 |
This tool converts spoken content from audio and video files into written text.
|
|
|
75 |
Maximum size: 25MB.
|
76 |
api_key: Your Groq API key, required for authentication.
|
77 |
You can obtain this from https://console.groq.com/
|
|
|
78 |
|
79 |
Returns:
|
80 |
A text transcript of the spoken content in the audio file.
|
|
|
83 |
Upload a podcast episode to get a complete text transcript.
|
84 |
"""
|
85 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
# First check for environment variable, then use provided API key
|
87 |
actual_api_key = os.environ.get("GROQ_API_KEY", api_key)
|
88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
# Validate API key
|
90 |
if not actual_api_key:
|
91 |
+
return "Error: Please provide your Groq API key or set the GROQ_API_KEY environment variable"
|
92 |
|
93 |
if audio_file is None:
|
94 |
return "Error: Please upload an audio or video file"
|
|
|
114 |
except Exception as e:
|
115 |
return f"Error: {str(e)}"
|
116 |
|
117 |
+
def transcribe_audio_from_url(audio_url, api_key):
|
118 |
"""Transcribe audio/video files from a URL into text using Groq's Whisper model.
|
119 |
|
120 |
This tool converts spoken content from audio and video files into written text.
|
|
|
126 |
Maximum size: 25MB.
|
127 |
api_key: Your Groq API key, required for authentication.
|
128 |
You can obtain this from https://console.groq.com/
|
|
|
129 |
|
130 |
Returns:
|
131 |
A text transcript of the spoken content in the audio file.
|
|
|
134 |
Provide a URL to a podcast episode to get a complete text transcript.
|
135 |
"""
|
136 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
# First check for environment variable, then use provided API key
|
138 |
actual_api_key = os.environ.get("GROQ_API_KEY", api_key)
|
139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
# Validate API key
|
141 |
if not actual_api_key:
|
142 |
+
return "Error: Please provide your Groq API key or set the GROQ_API_KEY environment variable"
|
143 |
|
144 |
if not audio_url or audio_url.strip() == "":
|
145 |
return "Error: Please provide a URL to an audio or video file"
|
|
|
188 |
except Exception as e:
|
189 |
return f"Error: {str(e)}"
|
190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
# Create the Gradio interface with custom layout
|
192 |
with gr.Blocks(title="Audio/Video Transcription with Groq", theme=gr.themes.Soft()) as demo:
|
193 |
gr.Markdown("# 🎵 Audio/Video Transcription with Groq Whisper")
|
|
|
282 |
show_copy_button=True,
|
283 |
interactive=False
|
284 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
285 |
|
286 |
+
# Connect the buttons to their respective transcription functions
|
287 |
upload_transcribe_btn.click(
|
288 |
fn=transcribe_audio,
|
289 |
inputs=[audio_input, api_key_input],
|
|
|
298 |
show_progress=True
|
299 |
)
|
300 |
|
|
|
|
|
|
|
|
|
|
|
|
|
301 |
# Add examples section
|
302 |
gr.Markdown("### 🔗 Useful Links")
|
303 |
gr.Markdown("""
|
|
|
307 |
""")
|
308 |
|
309 |
if __name__ == "__main__":
|
310 |
+
demo.launch(mcp_server=True)
|