Spaces:
Running
Running
Upload 2 files
Browse files- app.py +93 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
|
6 |
+
BASE_URL = "https://api.jigsawstack.com/v1"
|
7 |
+
headers = {
|
8 |
+
"x-api-key": os.getenv("JIGSAWSTACK_API_KEY")
|
9 |
+
}
|
10 |
+
|
11 |
+
def transcribe_audio(input_type, audio_url, file_store_key, language):
|
12 |
+
"""Transcribe audio using JigsawStack Speech-to-Text API"""
|
13 |
+
if input_type == "Audio URL" and not audio_url:
|
14 |
+
return "Error: Please provide an audio URL.", ""
|
15 |
+
if input_type == "File Store Key" and not file_store_key:
|
16 |
+
return "Error: Please provide a file store key.", ""
|
17 |
+
try:
|
18 |
+
payload = {}
|
19 |
+
if input_type == "Audio URL":
|
20 |
+
payload["url"] = audio_url.strip()
|
21 |
+
if input_type == "File Store Key":
|
22 |
+
payload["file_store_key"] = file_store_key.strip()
|
23 |
+
if language:
|
24 |
+
payload["language"] = language
|
25 |
+
response = requests.post(
|
26 |
+
f"{BASE_URL}/ai/transcribe",
|
27 |
+
headers=headers,
|
28 |
+
json=payload
|
29 |
+
)
|
30 |
+
response.raise_for_status()
|
31 |
+
result = response.json()
|
32 |
+
if not result.get("success"):
|
33 |
+
error_msg = f"Error: API call failed - {result.get('message', 'Unknown error')}"
|
34 |
+
return error_msg, ""
|
35 |
+
transcribed_text = result.get("text", "")
|
36 |
+
return "Transcription completed successfully!", transcribed_text
|
37 |
+
except requests.exceptions.RequestException as e:
|
38 |
+
return f"Request failed: {str(e)}", ""
|
39 |
+
except Exception as e:
|
40 |
+
return f"An unexpected error occurred: {str(e)}", ""
|
41 |
+
|
42 |
+
with gr.Blocks() as demo:
|
43 |
+
gr.Markdown("""
|
44 |
+
<div style='text-align: center; margin-bottom: 24px;'>
|
45 |
+
<h1 style='font-size:2.2em; margin-bottom: 0.2em;'>Speech-to-Text Transcription</h1>
|
46 |
+
<p style='font-size:1.2em; margin-top: 0;'>Transcribe video and audio files with ease leveraging Whisper large V3 AI model.</p>
|
47 |
+
<p style='font-size:1em; margin-top: 0.5em;'>Supported formats: MP3, WAV, M4A, FLAC, AAC, OGG, WEBM. Max file size: 100MB, Max duration: 4 hours.</p>
|
48 |
+
</div>
|
49 |
+
""")
|
50 |
+
with gr.Row():
|
51 |
+
with gr.Column():
|
52 |
+
gr.Markdown("#### Audio Input")
|
53 |
+
input_type = gr.Radio([
|
54 |
+
"Audio URL",
|
55 |
+
"File Store Key"
|
56 |
+
], value="Audio URL", label="Select Input Type")
|
57 |
+
audio_url = gr.Textbox(
|
58 |
+
label="Audio URL",
|
59 |
+
placeholder="Enter the URL of the audio/video file...",
|
60 |
+
visible=True
|
61 |
+
)
|
62 |
+
file_store_key = gr.Textbox(
|
63 |
+
label="File Store Key",
|
64 |
+
placeholder="Enter the file store key from JigsawStack File Storage...",
|
65 |
+
visible=False
|
66 |
+
)
|
67 |
+
language = gr.Textbox(
|
68 |
+
label="Language (optional)",
|
69 |
+
placeholder="e.g., en, es, fr, de, ja, zh... (leave empty for auto-detect)"
|
70 |
+
)
|
71 |
+
transcribe_btn = gr.Button("Start Transcription", variant="primary")
|
72 |
+
with gr.Column():
|
73 |
+
gr.Markdown("#### Transcription Result")
|
74 |
+
status_message = gr.Textbox(label="Status", interactive=False)
|
75 |
+
transcribed_text = gr.Textbox(
|
76 |
+
label="Transcribed Text",
|
77 |
+
interactive=False,
|
78 |
+
lines=10,
|
79 |
+
max_lines=20
|
80 |
+
)
|
81 |
+
def toggle_inputs(selected):
|
82 |
+
if selected == "Audio URL":
|
83 |
+
return gr.update(visible=True), gr.update(visible=False)
|
84 |
+
else:
|
85 |
+
return gr.update(visible=False), gr.update(visible=True)
|
86 |
+
input_type.change(toggle_inputs, inputs=[input_type], outputs=[audio_url, file_store_key])
|
87 |
+
transcribe_btn.click(
|
88 |
+
transcribe_audio,
|
89 |
+
inputs=[input_type, audio_url, file_store_key, language],
|
90 |
+
outputs=[status_message, transcribed_text]
|
91 |
+
)
|
92 |
+
|
93 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
requests
|
3 |
+
Pillow
|