Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,50 @@
|
|
1 |
-
import
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
print("
|
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 |
-
|
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 |
-
|
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
|
34 |
-
|
35 |
elapsed = time.time() - start
|
36 |
|
37 |
-
if
|
38 |
-
data =
|
39 |
transcript = data.get("transcription", "No transcription returned.")
|
40 |
-
|
41 |
-
return transcript,
|
42 |
else:
|
43 |
-
return f"❌ Error: {
|
44 |
|
45 |
-
# -------------------- رابط کاربری --------------------
|
46 |
custom_css = """
|
47 |
-
#gooya-title{color:#fff;
|
|
|
48 |
border-radius:12px;padding:20px 10px;margin-bottom:12px;}
|
49 |
-
.gooya-badge{display:inline-block;background:#224CA5;color:#fff;
|
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
|
56 |
-
# عنوان
|
57 |
gr.HTML(
|
58 |
"""
|
59 |
<div id="gooya-title">
|
@@ -70,18 +61,18 @@ with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
|
|
70 |
"""
|
71 |
)
|
72 |
|
73 |
-
# ورودی/خروجیها
|
74 |
with gr.Row():
|
75 |
with gr.Column():
|
76 |
-
|
77 |
label="Audio Input (upload or record, up to 30 s)",
|
78 |
type="filepath",
|
79 |
sources=["upload", "microphone"],
|
80 |
)
|
81 |
with gr.Column():
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
85 |
label="📝 Transcription",
|
86 |
lines=5,
|
87 |
show_copy_button=True,
|
@@ -89,7 +80,6 @@ with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
|
|
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")
|
@@ -105,20 +95,21 @@ with gr.Blocks(css=custom_css, title="Gooya ASR v1.4") as demo:
|
|
105 |
"""
|
106 |
)
|
107 |
|
108 |
-
# اتصال توابع به دکمهها
|
109 |
btn_transcribe.click(
|
110 |
transcribe_audio,
|
111 |
-
inputs=
|
112 |
-
outputs=[
|
|
|
113 |
)
|
114 |
|
|
|
115 |
btn_clear.click(
|
116 |
lambda: ("", "", None),
|
117 |
inputs=None,
|
118 |
-
outputs=[
|
119 |
)
|
120 |
|
121 |
-
# -------------------- اجرای برنامه --------------------
|
122 |
if __name__ == "__main__":
|
123 |
-
|
124 |
-
demo.queue()
|
|
|
|
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">
|
|
|
61 |
"""
|
62 |
)
|
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,
|
|
|
80 |
elem_id="gooya-textbox",
|
81 |
)
|
82 |
|
|
|
83 |
with gr.Row():
|
84 |
btn_transcribe = gr.Button("Transcribe", variant="primary")
|
85 |
btn_clear = gr.Button("Clear", variant="secondary")
|
|
|
95 |
"""
|
96 |
)
|
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)
|