Enrique Cardoza commited on
Commit Β·
473f76d
1
Parent(s): a3e6bb5
feat(api): enable request header handling in Gradio MCP server
Browse files- Add support for extracting and utilizing Authorization headers
- Create dedicated function to view all request headers for debugging
- Add new UI tab for inspecting incoming HTTP headers
- Modify transcription functions to accept and process request headers
- Enable API key extraction from Bearer tokens in Authorization header
- Improve server configuration with explicit host and port settings
- Enable API documentation with show_api flag
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from groq import Groq
|
|
| 4 |
import tempfile
|
| 5 |
import requests
|
| 6 |
import urllib.parse
|
|
|
|
| 7 |
|
| 8 |
def validate_file(file):
|
| 9 |
"""Validate uploaded file type and size."""
|
|
@@ -63,7 +64,19 @@ def validate_url_file(url):
|
|
| 63 |
except Exception as e:
|
| 64 |
return False, f"Error validating URL: {str(e)}"
|
| 65 |
|
| 66 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,6 +88,7 @@ def transcribe_audio(audio_file, api_key):
|
|
| 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,12 +97,31 @@ def transcribe_audio(audio_file, api_key):
|
|
| 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,7 +147,7 @@ def transcribe_audio(audio_file, api_key):
|
|
| 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,6 +159,7 @@ def transcribe_audio_from_url(audio_url, api_key):
|
|
| 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,12 +168,31 @@ def transcribe_audio_from_url(audio_url, api_key):
|
|
| 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,6 +241,28 @@ def transcribe_audio_from_url(audio_url, api_key):
|
|
| 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,8 +357,26 @@ with gr.Blocks(title="Audio/Video Transcription with Groq", theme=gr.themes.Soft
|
|
| 282 |
show_copy_button=True,
|
| 283 |
interactive=False
|
| 284 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
-
# Connect the buttons to their respective
|
| 287 |
upload_transcribe_btn.click(
|
| 288 |
fn=transcribe_audio,
|
| 289 |
inputs=[audio_input, api_key_input],
|
|
@@ -298,6 +391,12 @@ with gr.Blocks(title="Audio/Video Transcription with Groq", theme=gr.themes.Soft
|
|
| 298 |
show_progress=True
|
| 299 |
)
|
| 300 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
# Add examples section
|
| 302 |
gr.Markdown("### π Useful Links")
|
| 303 |
gr.Markdown("""
|
|
@@ -307,4 +406,4 @@ with gr.Blocks(title="Audio/Video Transcription with Groq", theme=gr.themes.Soft
|
|
| 307 |
""")
|
| 308 |
|
| 309 |
if __name__ == "__main__":
|
| 310 |
-
demo.launch(mcp_server=True)
|
|
|
|
| 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 |
except Exception as e:
|
| 65 |
return False, f"Error validating URL: {str(e)}"
|
| 66 |
|
| 67 |
+
def get_request_headers(request: gr.Request):
|
| 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 |
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 |
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 or include in Authorization header"
|
| 125 |
|
| 126 |
if audio_file is None:
|
| 127 |
return "Error: Please upload an audio or video file"
|
|
|
|
| 147 |
except Exception as e:
|
| 148 |
return f"Error: {str(e)}"
|
| 149 |
|
| 150 |
+
def transcribe_audio_from_url(audio_url, api_key, request: gr.Request = None):
|
| 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 |
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 |
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 or include in Authorization header"
|
| 196 |
|
| 197 |
if not audio_url or audio_url.strip() == "":
|
| 198 |
return "Error: Please provide a URL to an audio or video file"
|
|
|
|
| 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 |
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 |
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 |
""")
|
| 407 |
|
| 408 |
if __name__ == "__main__":
|
| 409 |
+
demo.launch(mcp_server=True)
|