navidved commited on
Commit
f39366f
Β·
verified Β·
1 Parent(s): 657d6d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -76
app.py CHANGED
@@ -1,105 +1,87 @@
1
- import os, time, requests, gradio as gr
 
 
 
2
 
3
- print("Gradio version:", gr.version)
 
4
 
5
- # ---------- Environment Variables ----------
6
- ASR_API_URL = os.getenv("ASR_API_URL")
7
- AUTH_TOKEN = os.getenv("AUTH_TOKEN")
8
- if not ASR_API_URL or not AUTH_TOKEN:
9
- print("⚠️ ASR_API_URL or AUTH_TOKEN is not set; API calls will fail.")
10
-
11
- # ---------- Core Transcription Function ----------
12
- def transcribe_audio(file_path: str):
13
  if not ASR_API_URL or not AUTH_TOKEN:
14
  return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
15
-
16
  headers = {
17
- "accept": "application/json",
18
- "Authorization": f"Bearer {AUTH_TOKEN}",
19
  }
20
-
21
- start = time.time()
 
 
22
  try:
23
- with open(file_path, "rb") as f:
24
- files = {"file": (os.path.basename(file_path), f, "audio/mpeg")}
25
- resp = requests.post(ASR_API_URL, headers=headers, files=files, timeout=120)
26
  except Exception as e:
27
- return f"❌ Error while calling ASR API: {e}", ""
 
28
 
29
- elapsed = time.time() - start
30
- if resp.status_code == 200:
31
- data = resp.json()
32
- text = data.get("transcription", "No transcription returned.")
33
- return text, f"{data.get('time', elapsed):.2f} s"
34
- return f"❌ Error: {resp.status_code}, {resp.text}", ""
 
35
 
36
- # ---------- Styling ----------
37
- VIOLET_MAIN = "#7F3FBF"
38
- VIOLET_LIGHT = "#C3A6FF"
 
 
 
 
 
 
 
39
 
40
- custom_css = f"""
41
- #gooya-title {{
42
- color:#fff;
43
- background:linear-gradient(90deg,{VIOLET_MAIN} 0%,{VIOLET_LIGHT} 100%);
44
- border-radius:12px;padding:20px 10px;margin-bottom:12px;
45
- }}
46
- .gooya-badge {{
47
- display:inline-block;background:{VIOLET_MAIN};color:#fff;
48
- border-radius:16px;padding:6px 16px;font-size:.97rem;margin-top:4px;
49
- }}
50
- """
51
-
52
- # ---------- UI ----------
53
- with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
54
  with gr.Row():
55
  with gr.Column():
56
- audio_input = gr.Audio(
57
- label="Audio Input (upload or record, up to 30 s)",
58
  type="filepath",
59
- sources=["upload", "microphone"],
 
60
  )
61
  with gr.Column():
62
- processing_time_tb = gr.Textbox(
63
- label="⏱️ Processing Time",
64
- interactive=False,
65
- elem_classes="gooya-badge",
66
- )
67
- transcription_tb = gr.Textbox(
68
  label="πŸ“ Transcription",
69
  lines=5,
70
  show_copy_button=True,
71
  placeholder="The transcription will appear here...",
72
- elem_id="gooya-textbox",
73
  )
74
 
75
  with gr.Row():
76
- btn_transcribe = gr.Button("Transcribe", variant="primary")
77
- btn_clear = gr.Button("Clear", variant="secondary")
78
 
79
- gr.Markdown(
80
- """
81
- Guidelines
82
- - Maximum audio length: 30 seconds
83
- - Audio content should be in Persian.
84
- - Both transcription and processing time are displayed immediately.
85
 
86
- See the [Persian ASR Leaderboard](https://huggingface.co/spaces/navidved/open_persian_asr_leaderboard) for benchmarks.
87
- """
88
- )
89
 
90
- # ---------- Callbacks ----------
91
- btn_transcribe.click(
92
- fn=transcribe_audio,
93
- inputs=[audio_input],
94
- outputs=[transcription_tb, processing_time_tb],
95
  )
96
-
97
- btn_clear.click(
98
- lambda: ("", "", None),
99
- inputs=None,
100
- outputs=[transcription_tb, processing_time_tb, audio_input],
101
  )
102
 
103
- # ---------- Launch ----------
104
- if name == "main":
105
- demo.queue().launch(debug=True, share=False)
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+ import time
5
 
6
+ ASR_API_URL = os.getenv('ASR_API_URL')
7
+ AUTH_TOKEN = os.getenv('AUTH_TOKEN')
8
 
9
+ def transcribe_audio(file_path):
 
 
 
 
 
 
 
10
  if not ASR_API_URL or not AUTH_TOKEN:
11
  return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
 
12
  headers = {
13
+ 'accept': 'application/json',
14
+ 'Authorization': f'Bearer {AUTH_TOKEN}',
15
  }
16
+ files = {
17
+ 'file': (file_path, open(file_path, 'rb'), 'audio/mpeg'),
18
+ }
19
+ start_time = time.time()
20
  try:
21
+ response = requests.post(ASR_API_URL, headers=headers, files=files)
 
 
22
  except Exception as e:
23
+ return f"❌ Error: {str(e)}", ""
24
+ inference_time = time.time() - start_time
25
 
26
+ if response.status_code == 200:
27
+ res = response.json()
28
+ transcription = res.get("transcription", "No transcription returned.")
29
+ inference_time_str = f"{res.get('time', inference_time):.2f} seconds"
30
+ return transcription, inference_time_str
31
+ else:
32
+ return f"❌ Error: {response.status_code}, {response.text}", ""
33
 
34
+ with gr.Blocks(css="""
35
+ #gooya-title {color:white; background: linear-gradient(90deg, #224CA5 0%, #2CD8D5 100%); border-radius: 12px; padding:20px 10px;margin-bottom:12px;}
36
+ .gooya-badge {display:inline-block; background:#224CA5; color:#fff; border-radius:16px; padding:6px 16px; font-size:0.97rem; margin-top:4px;}
37
+ #gooya-box {background:#F7FAFF; border:1px solid #e7e9ef; border-radius:14px; padding:22px 18px; margin-top:12px;}
38
+ """) as demo:
39
+ gr.HTML("""<div id="gooya-title">
40
+ <h1 style='margin-bottom:10px;font-weight:800;font-size:2rem;'>Gooya ASR <span style="font-size:1.1rem; font-weight:400; opacity:0.8;">v1.4</span></h1>
41
+ <p style='font-size:1.12rem; margin-bottom:2px;'>High-performance Persian Speech-to-Text</p>
42
+ <p style='font-size:0.98rem; color:#c6e8fa'>Upload or record a Persian audio file (max 30s) and instantly receive the transcription.</p>
43
+ </div>""")
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  with gr.Row():
46
  with gr.Column():
47
+ audio = gr.Audio(
48
+ label="Audio Input (Upload or record, up to 30s)",
49
  type="filepath",
50
+ show_label=True,
51
+ sources=["upload", "microphone"]
52
  )
53
  with gr.Column():
54
+ inference_time = gr.Label(label="⏱️ Processing Time", elem_classes="gooya-badge")
55
+ transcription = gr.Textbox(
 
 
 
 
56
  label="πŸ“ Transcription",
57
  lines=5,
58
  show_copy_button=True,
59
  placeholder="The transcription will appear here...",
60
+ elem_id="gooya-textbox"
61
  )
62
 
63
  with gr.Row():
64
+ submit_btn = gr.Button("Transcribe", variant="primary")
65
+ clear_btn = gr.Button("Clear", variant="secondary")
66
 
67
+ gr.Markdown("""
68
+ **Instructions:**
69
+ - Maximum audio length: **30 seconds**
70
+ - Input audio should be in Persian.
71
+ - The transcription and processing time will be displayed instantly.
 
72
 
73
+ For performance benchmarks, visit: [Persian ASR Leaderboard](https://huggingface.co/spaces/navidved/open_persian_asr_leaderboard)
74
+ """)
 
75
 
76
+ submit_btn.click(
77
+ transcribe_audio,
78
+ inputs=audio,
79
+ outputs=[transcription, inference_time]
 
80
  )
81
+ clear_btn.click(
82
+ lambda: ("", ""),
83
+ None,
84
+ [transcription, inference_time, audio]
 
85
  )
86
 
87
+ demo.launch(share=True)