navidved commited on
Commit
1967b9f
Β·
verified Β·
1 Parent(s): 663a602

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -36
app.py CHANGED
@@ -4,60 +4,72 @@ import requests, os, time
4
  ASR_API_URL = os.getenv("ASR_API_URL")
5
  AUTH_TOKEN = os.getenv("AUTH_TOKEN")
6
 
7
- def transcribe_audio(file_path):
 
8
  if not file_path:
9
- return "❌ Audio Loading ...", ""
10
  if not ASR_API_URL or not AUTH_TOKEN:
11
  return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
12
-
13
- headers = {"accept": "application/json",
14
- "Authorization": f"Bearer {AUTH_TOKEN}"}
15
- files = {"file": (file_path, open(file_path, "rb"), "audio/mpeg")}
 
 
16
  start = time.time()
17
  try:
18
- response = requests.post(ASR_API_URL, headers=headers, files=files)
 
 
19
  except Exception as e:
20
  return f"❌ Error: {e}", ""
21
-
22
- inf_time = time.time() - start
23
  if response.status_code == 200:
24
- res = response.json()
25
- text = res.get("transcription", "No transcription returned.")
26
- inf_time = f"{res.get('time', inf_time):.2f} seconds"
27
- return text, inf_time
28
- else:
29
- return f"❌ Error {response.status_code}: {response.text}", ""
30
-
31
- with gr.Blocks(css="""
32
  #gooya-title {color:white; background:linear-gradient(90deg,#224CA5 0%,#2CD8D5 100%);
33
  border-radius:12px;padding:20px 10px;margin-bottom:12px;}
34
  .gooya-badge {display:inline-block;background:#224CA5;color:#fff;border-radius:16px;
35
  padding:6px 16px;font-size:0.97rem;margin-top:4px;}
36
  #gooya-box {background:#F7FAFF;border:1px solid #e7e9ef;border-radius:14px;
37
  padding:22px 18px;margin-top:12px;}
38
- """) as demo:
 
 
39
 
40
- gr.HTML("""<div id="gooya-title">
 
41
  <h1 style='margin-bottom:10px;font-weight:800;font-size:2rem;'>Gooya ASR
42
- <span style="font-size:1.1rem; font-weight:400; opacity:0.8;">v1.4</span></h1>
43
  <p style='font-size:1.12rem;margin-bottom:2px;'>High-performance Persian Speech-to-Text</p>
44
- <p style='font-size:0.98rem;color:#c6e8fa'>
45
- Upload or record a Persian audio file (max 30s) and instantly receive the transcription.</p>
46
- </div>""")
47
 
48
  with gr.Row():
49
  with gr.Column():
50
- audio = gr.Audio(label="Audio Input (Upload or record, up to 30s)",
51
- type="filepath",
52
- sources=["upload", "microphone"],
53
- show_label=True)
 
 
54
  with gr.Column():
55
  inf_time_lbl = gr.Label(label="⏱️ Processing Time", elem_classes="gooya-badge")
56
- transcription = gr.Textbox(label="πŸ“ Transcription",
57
- lines=5,
58
- show_copy_button=True,
59
- placeholder="The transcription will appear here...",
60
- elem_id="gooya-textbox")
 
 
61
 
62
  with gr.Row():
63
  submit_btn = gr.Button("Transcribe", variant="primary", interactive=False)
@@ -74,22 +86,28 @@ For benchmarks visit: [Persian ASR Leaderboard](https://huggingface.co/spaces/na
74
  def toggle_btn(path):
75
  return gr.Button.update(interactive=bool(path))
76
 
77
- audio.change(toggle_btn, inputs=audio, outputs=submit_btn)
 
78
 
79
  submit_btn.click(
80
  transcribe_audio,
81
  inputs=audio,
82
- outputs=[transcription, inf_time_lbl]
83
  ).then(
84
  lambda: gr.Button.update(interactive=False),
85
  None,
86
- submit_btn
 
87
  )
88
 
 
 
 
89
  clear_btn.click(
90
- lambda: ("", "", None, False),
91
  None,
92
  [transcription, inf_time_lbl, audio, submit_btn],
 
93
  )
94
 
95
  demo.launch()
 
4
  ASR_API_URL = os.getenv("ASR_API_URL")
5
  AUTH_TOKEN = os.getenv("AUTH_TOKEN")
6
 
7
+ def transcribe_audio(file_path: str | None):
8
+ # Ϊ†Ϊ© ΩΨ§ΫŒΩ„
9
  if not file_path:
10
+ return "❌ Audio File loading ...", ""
11
  if not ASR_API_URL or not AUTH_TOKEN:
12
  return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
13
+
14
+ headers = {
15
+ "accept": "application/json",
16
+ "Authorization": f"Bearer {AUTH_TOKEN}",
17
+ }
18
+
19
  start = time.time()
20
  try:
21
+ with open(file_path, "rb") as f:
22
+ files = {"file": (os.path.basename(file_path), f, "audio/mpeg")}
23
+ response = requests.post(ASR_API_URL, headers=headers, files=files)
24
  except Exception as e:
25
  return f"❌ Error: {e}", ""
26
+
27
+ elapsed = time.time() - start
28
  if response.status_code == 200:
29
+ data = response.json()
30
+ txt = data.get("transcription", "No transcription returned.")
31
+ elapsed = data.get("time", elapsed)
32
+ return txt, f"{elapsed:.2f} seconds"
33
+ return f"❌ Error {response.status_code}: {response.text}", ""
34
+
35
+
36
+ CUSTOM_CSS = """
37
  #gooya-title {color:white; background:linear-gradient(90deg,#224CA5 0%,#2CD8D5 100%);
38
  border-radius:12px;padding:20px 10px;margin-bottom:12px;}
39
  .gooya-badge {display:inline-block;background:#224CA5;color:#fff;border-radius:16px;
40
  padding:6px 16px;font-size:0.97rem;margin-top:4px;}
41
  #gooya-box {background:#F7FAFF;border:1px solid #e7e9ef;border-radius:14px;
42
  padding:22px 18px;margin-top:12px;}
43
+ """
44
+
45
+ with gr.Blocks(css=CUSTOM_CSS) as demo:
46
 
47
+ gr.HTML("""
48
+ <div id="gooya-title">
49
  <h1 style='margin-bottom:10px;font-weight:800;font-size:2rem;'>Gooya ASR
50
+ <span style="font-size:1.1rem;font-weight:400;opacity:0.8;'>v1.4</span></h1>
51
  <p style='font-size:1.12rem;margin-bottom:2px;'>High-performance Persian Speech-to-Text</p>
52
+ <p style='font-size:0.98rem;color:#c6e8fa'>Upload or record a Persian audio file (max 30s) and instantly receive the transcription.</p>
53
+ </div>
54
+ """)
55
 
56
  with gr.Row():
57
  with gr.Column():
58
+ audio = gr.Audio(
59
+ label="Audio Input (Upload or record, up to 30s)",
60
+ type="filepath",
61
+ sources=["upload", "microphone"],
62
+ show_label=True,
63
+ )
64
  with gr.Column():
65
  inf_time_lbl = gr.Label(label="⏱️ Processing Time", elem_classes="gooya-badge")
66
+ transcription = gr.Textbox(
67
+ label="πŸ“ Transcription",
68
+ lines=5,
69
+ show_copy_button=True,
70
+ placeholder="The transcription will appear here...",
71
+ elem_id="gooya-textbox",
72
+ )
73
 
74
  with gr.Row():
75
  submit_btn = gr.Button("Transcribe", variant="primary", interactive=False)
 
86
  def toggle_btn(path):
87
  return gr.Button.update(interactive=bool(path))
88
 
89
+ audio.change(toggle_btn, inputs=audio, outputs=submit_btn, queue=False)
90
+ audio.stop_record(toggle_btn, inputs=audio, outputs=submit_btn, queue=False)
91
 
92
  submit_btn.click(
93
  transcribe_audio,
94
  inputs=audio,
95
+ outputs=[transcription, inf_time_lbl],
96
  ).then(
97
  lambda: gr.Button.update(interactive=False),
98
  None,
99
+ submit_btn,
100
+ queue=False,
101
  )
102
 
103
+ def clear_all():
104
+ return "", "", None, gr.Button.update(interactive=False)
105
+
106
  clear_btn.click(
107
+ clear_all,
108
  None,
109
  [transcription, inf_time_lbl, audio, submit_btn],
110
+ queue=False,
111
  )
112
 
113
  demo.launch()