navidved commited on
Commit
e83050a
·
verified ·
1 Parent(s): a71525e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -34
app.py CHANGED
@@ -1,50 +1,44 @@
1
- import gradio as gr
2
- import requests, os, time
3
- import gradio_client
4
-
5
- print("--------- Gradio version:", gr.__version__, "----------")
6
- print("gradio-client version:", gradio_client.__version__)
7
 
8
  ASR_API_URL = os.getenv("ASR_API_URL")
9
  AUTH_TOKEN = os.getenv("AUTH_TOKEN")
10
 
 
 
 
11
  def transcribe_audio(file_path: str):
12
- """
13
- دریافت مسیر فایل صوتی (type='filepath') و برگرداندن رونوشت و مدت زمان پردازش.
14
- """
15
  if not ASR_API_URL or not AUTH_TOKEN:
16
  return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
 
17
  headers = {
18
  "accept": "application/json",
19
  "Authorization": f"Bearer {AUTH_TOKEN}",
20
  }
21
- files = {"file": (file_path, open(file_path, "rb"), "audio/mpeg")}
22
  start = time.time()
23
  try:
24
- resp = requests.post(ASR_API_URL, headers=headers, files=files)
 
 
25
  except Exception as e:
26
- return f"❌ Error: {e}", ""
27
- elapsed = time.time() - start
28
 
 
29
  if resp.status_code == 200:
30
  data = resp.json()
31
  transcript = data.get("transcription", "No transcription returned.")
32
- t = f"{data.get('time', elapsed):.2f} ثانیه"
33
- return transcript, t
34
  else:
35
  return f"❌ Error: {resp.status_code}, {resp.text}", ""
36
 
37
  custom_css = """
38
- #gooya-title{color:#fff;
39
- background:linear-gradient(90deg,#224CA5 0%,#2CD8D5 100%);
40
  border-radius:12px;padding:20px 10px;margin-bottom:12px;}
41
- .gooya-badge{display:inline-block;background:#224CA5;color:#fff;
42
- border-radius:16px;padding:6px 16px;font-size:.97rem;margin-top:4px;}
43
- #gooya-box{background:#F7FAFF;border:1px solid #e7e9ef;border-radius:14px;
44
- padding:22px 18px;margin-top:12px;}
45
  """
46
 
47
- with gr.Blocks(css=custom_css, title="Gooya ASR") as demo:
48
  gr.HTML(
49
  """
50
  <div id="gooya-title">
@@ -63,16 +57,18 @@ with gr.Blocks(css=custom_css, title="Gooya ASR") as demo:
63
 
64
  with gr.Row():
65
  with gr.Column():
66
- audio = gr.Audio(
67
  label="Audio Input (upload or record, up to 30 s)",
68
  type="filepath",
69
  sources=["upload", "microphone"],
70
  )
71
  with gr.Column():
72
- inference_time = gr.Label(
73
- label="⏱️ Processing Time", elem_classes="gooya-badge"
 
 
74
  )
75
- transcription = gr.Textbox(
76
  label="📝 Transcription",
77
  lines=5,
78
  show_copy_button=True,
@@ -97,19 +93,16 @@ with gr.Blocks(css=custom_css, title="Gooya ASR") as demo:
97
 
98
  btn_transcribe.click(
99
  transcribe_audio,
100
- inputs=audio,
101
- outputs=[transcription, inference_time],
102
- queue=True
103
  )
104
 
105
- # خروجی سوم (audio) هم باید reset شود، تا همه چیز مطابق gradio خوب پاک شود
106
  btn_clear.click(
107
  lambda: ("", "", None),
108
  inputs=None,
109
- outputs=[transcription, inference_time, audio]
110
  )
111
 
112
  if __name__ == "__main__":
113
- print("==== Gooya ASR Demo Launching... ====")
114
- demo.queue(max_size=128) \
115
- .launch(debug=True, share=False)
 
1
+ import os, time, requests, gradio as gr
2
+ print("Gradio version:", gr.__version__)
 
 
 
 
3
 
4
  ASR_API_URL = os.getenv("ASR_API_URL")
5
  AUTH_TOKEN = os.getenv("AUTH_TOKEN")
6
 
7
+ if not ASR_API_URL or not AUTH_TOKEN:
8
+ print("⚠️ Warning: ASR_API_URL or AUTH_TOKEN is not set; calls will fail.")
9
+
10
  def transcribe_audio(file_path: str):
 
 
 
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
  start = time.time()
19
  try:
20
+ with open(file_path, "rb") as f:
21
+ files = {"file": (os.path.basename(file_path), f, "audio/mpeg")}
22
+ resp = requests.post(ASR_API_URL, headers=headers, files=files, timeout=120)
23
  except Exception as e:
24
+ return f"❌ Error while calling ASR API: {e}", ""
 
25
 
26
+ elapsed = time.time() - start
27
  if resp.status_code == 200:
28
  data = resp.json()
29
  transcript = data.get("transcription", "No transcription returned.")
30
+ return transcript, f"{data.get('time', elapsed):.2f} ثانیه"
 
31
  else:
32
  return f"❌ Error: {resp.status_code}, {resp.text}", ""
33
 
34
  custom_css = """
35
+ #gooya-title{color:#fff;background:linear-gradient(90deg,#224CA5 0%,#2CD8D5 100%);
 
36
  border-radius:12px;padding:20px 10px;margin-bottom:12px;}
37
+ .gooya-badge{display:inline-block;background:#224CA5;color:#fff;border-radius:16px;
38
+ padding:6px 16px;font-size:.97rem;margin-top:4px;}
 
 
39
  """
40
 
41
+ with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
42
  gr.HTML(
43
  """
44
  <div id="gooya-title">
 
57
 
58
  with gr.Row():
59
  with gr.Column():
60
+ audio_input = gr.Audio(
61
  label="Audio Input (upload or record, up to 30 s)",
62
  type="filepath",
63
  sources=["upload", "microphone"],
64
  )
65
  with gr.Column():
66
+ processing_time_tb = gr.Textbox(
67
+ label="⏱️ Processing Time",
68
+ interactive=False,
69
+ elem_classes="gooya-badge",
70
  )
71
+ transcription_tb = gr.Textbox(
72
  label="📝 Transcription",
73
  lines=5,
74
  show_copy_button=True,
 
93
 
94
  btn_transcribe.click(
95
  transcribe_audio,
96
+ inputs=audio_input,
97
+ outputs=[transcription_tb, processing_time_tb],
 
98
  )
99
 
 
100
  btn_clear.click(
101
  lambda: ("", "", None),
102
  inputs=None,
103
+ outputs=[transcription_tb, processing_time_tb, audio_input],
104
  )
105
 
106
  if __name__ == "__main__":
107
+ # show_api=False تا مسیر /api-info ساخته نشود و باگ برطرف گردد
108
+ demo.queue().launch(show_api=False, share=False)