navidved commited on
Commit
afa3129
·
verified ·
1 Parent(s): eed5ed8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -27
app.py CHANGED
@@ -1,48 +1,124 @@
1
- import gradio as gr
2
- import requests, os, time
3
 
4
- print("--------- Gradio version:", gr.__version__, "----------")
 
5
 
6
  ASR_API_URL = os.getenv("ASR_API_URL")
7
  AUTH_TOKEN = os.getenv("AUTH_TOKEN")
8
 
 
 
 
 
 
9
  def transcribe_audio(file_path: str):
 
 
 
 
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 = {"file": (file_path, open(file_path, "rb"), "audio/mpeg")}
17
  start = time.time()
18
  try:
19
- resp = requests.post(ASR_API_URL, headers=headers, files=files)
 
 
20
  except Exception as e:
21
- return f"❌ Error: {e}", ""
 
22
  elapsed = time.time() - start
23
- if resp.status_code == 200:
24
- data = resp.json()
 
25
  transcript = data.get("transcription", "No transcription returned.")
26
- t = f"{data.get('time', elapsed):.2f} ثانیه"
27
- return transcript, t
28
  else:
29
- return f"❌ Error: {resp.status_code}, {resp.text}", ""
30
-
31
- def main():
32
- iface = gr.Interface(
33
- fn=transcribe_audio,
34
- inputs=gr.Audio(label="Audio Input (upload or record, up to 30 s)", type="filepath", sources=["upload", "microphone"]),
35
- outputs=[
36
- gr.Textbox(label="📝 Transcription", lines=5, show_copy_button=True, placeholder="The transcription will appear here..."),
37
- gr.Label(label="⏱️ Processing Time")
38
- ],
39
- title="Gooya ASR v1.4",
40
- description="High-performance Persian Speech-to-Text. Upload or record a Persian audio file (max 30 s) and instantly receive the transcription.",
41
- examples=[],
42
- cache_examples=False,
43
- allow_flagging="never"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  )
45
- iface.launch()
46
 
 
47
  if __name__ == "__main__":
48
- main()
 
 
1
+ import os, time, requests, gradio as gr
 
2
 
3
+ # -------------------- تنظیمات پایه --------------------
4
+ print("Gradio version:", gr.__version__)
5
 
6
  ASR_API_URL = os.getenv("ASR_API_URL")
7
  AUTH_TOKEN = os.getenv("AUTH_TOKEN")
8
 
9
+ if not ASR_API_URL or not AUTH_TOKEN:
10
+ print("⚠️ Warning: ASR_API_URL or AUTH_TOKEN is not set. "
11
+ "Transcription will fail until they are provided.")
12
+
13
+ # -------------------- توابع کمکی --------------------
14
  def transcribe_audio(file_path: str):
15
+ """
16
+ ورودی: مسیر فایل (به خاطر type='filepath')
17
+ خروجی: متن رونویسی و زمان پردازش یا پیام خطا
18
+ """
19
  if not ASR_API_URL or not AUTH_TOKEN:
20
  return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
21
+
22
  headers = {
23
  "accept": "application/json",
24
  "Authorization": f"Bearer {AUTH_TOKEN}",
25
  }
26
+
27
  start = time.time()
28
  try:
29
+ with open(file_path, "rb") as f:
30
+ files = {"file": (os.path.basename(file_path), f, "audio/mpeg")}
31
+ response = requests.post(ASR_API_URL, headers=headers, files=files, timeout=120)
32
  except Exception as e:
33
+ return f"❌ Error while calling ASR API: {e}", ""
34
+
35
  elapsed = time.time() - start
36
+
37
+ if response.status_code == 200:
38
+ data = response.json()
39
  transcript = data.get("transcription", "No transcription returned.")
40
+ processing_time = f"{data.get('time', elapsed):.2f} ثانیه"
41
+ return transcript, processing_time
42
  else:
43
+ return f"❌ Error: {response.status_code}, {response.text}", ""
44
+
45
+ # -------------------- رابط کاربری --------------------
46
+ custom_css = """
47
+ #gooya-title{color:#fff;background:linear-gradient(90deg,#224CA5 0%,#2CD8D5 100%);
48
+ border-radius:12px;padding:20px 10px;margin-bottom:12px;}
49
+ .gooya-badge{display:inline-block;background:#224CA5;color:#fff;border-radius:16px;
50
+ padding:6px 16px;font-size:.97rem;margin-top:4px;}
51
+ #gooya-box{background:#F7FAFF;border:1px solid #e7e9ef;border-radius:14px;
52
+ padding:22px 18px;margin-top:12px;}
53
+ """
54
+
55
+ with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
56
+ # عنوان
57
+ gr.HTML(
58
+ """
59
+ <div id="gooya-title">
60
+ <h1 style='margin-bottom:10px;font-weight:800;font-size:2rem;'>
61
+ Gooya ASR <span style="font-size:1.1rem;font-weight:400;opacity:.8;">v1.4</span>
62
+ </h1>
63
+ <p style='font-size:1.12rem;margin-bottom:2px;'>
64
+ High-performance Persian Speech-to-Text
65
+ </p>
66
+ <p style='font-size:.98rem;color:#c6e8fa'>
67
+ Upload or record a Persian audio file (max 30 s) and instantly receive the transcription.
68
+ </p>
69
+ </div>
70
+ """
71
+ )
72
+
73
+ # ورودی/خروجی‌ها
74
+ with gr.Row():
75
+ with gr.Column():
76
+ audio_input = gr.Audio(
77
+ label="Audio Input (upload or record, up to 30 s)",
78
+ type="filepath",
79
+ sources=["upload", "microphone"],
80
+ )
81
+ with gr.Column():
82
+ processing_time_lbl = gr.Label(label="⏱️ Processing Time",
83
+ elem_classes="gooya-badge")
84
+ transcription_tb = gr.Textbox(
85
+ label="📝 Transcription",
86
+ lines=5,
87
+ show_copy_button=True,
88
+ placeholder="The transcription will appear here...",
89
+ elem_id="gooya-textbox",
90
+ )
91
+
92
+ # دکمه‌ها
93
+ with gr.Row():
94
+ btn_transcribe = gr.Button("Transcribe", variant="primary")
95
+ btn_clear = gr.Button("Clear", variant="secondary")
96
+
97
+ gr.Markdown(
98
+ """
99
+ **دستورالعمل‌ها**
100
+ - حداکثر طول صدا: **۳۰ ثانیه**
101
+ - صدا باید فارسی باشد.
102
+ - نتیجه‌ی رونویسی و زمان پردازش بلافاصله نمایش داده می‌شود.
103
+
104
+ برای مشاهده بنچمارک‌ها به [Persian ASR Leaderboard](https://huggingface.co/spaces/navidved/open_persian_asr_leaderboard) مراجعه کنید.
105
+ """
106
+ )
107
+
108
+ # اتصال توابع به دکمه‌ها
109
+ btn_transcribe.click(
110
+ transcribe_audio,
111
+ inputs=audio_input,
112
+ outputs=[transcription_tb, processing_time_lbl],
113
+ )
114
+
115
+ btn_clear.click(
116
+ lambda: ("", "", None),
117
+ inputs=None,
118
+ outputs=[transcription_tb, processing_time_lbl, audio_input],
119
  )
 
120
 
121
+ # -------------------- اجرای برنامه --------------------
122
  if __name__ == "__main__":
123
+ # فقط یک فراخوانی برای queue و launch همانند کد مرجع
124
+ demo.queue().launch(debug=True, share=False)