Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,29 +7,55 @@ 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 |
-
|
17 |
-
'file': (file_path, open(file_path, 'rb'), 'audio/mpeg'),
|
18 |
-
}
|
19 |
start_time = time.time()
|
|
|
20 |
try:
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
31 |
else:
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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;}
|
@@ -78,10 +104,18 @@ For performance benchmarks, visit: [Persian ASR Leaderboard](https://huggingface
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
AUTH_TOKEN = os.getenv('AUTH_TOKEN')
|
8 |
|
9 |
def transcribe_audio(file_path):
|
10 |
+
# بررسی اینکه فایل وجود دارد
|
11 |
+
if file_path is None:
|
12 |
+
return "❌ Error: Please upload or record an audio file.", ""
|
13 |
+
|
14 |
if not ASR_API_URL or not AUTH_TOKEN:
|
15 |
return "❌ Error: ASR_API_URL or AUTH_TOKEN is not set.", ""
|
16 |
+
|
17 |
headers = {
|
18 |
'accept': 'application/json',
|
19 |
'Authorization': f'Bearer {AUTH_TOKEN}',
|
20 |
}
|
21 |
+
|
|
|
|
|
22 |
start_time = time.time()
|
23 |
+
|
24 |
try:
|
25 |
+
# استفاده از with برای مدیریت بهتر فایل
|
26 |
+
with open(file_path, 'rb') as audio_file:
|
27 |
+
files = {
|
28 |
+
'file': (os.path.basename(file_path), audio_file, 'audio/mpeg'),
|
29 |
+
}
|
30 |
+
response = requests.post(ASR_API_URL, headers=headers, files=files, timeout=60)
|
31 |
+
|
32 |
+
except FileNotFoundError:
|
33 |
+
return f"❌ Error: File not found: {file_path}", ""
|
34 |
+
except requests.exceptions.Timeout:
|
35 |
+
return "❌ Error: Request timeout. Please try again.", ""
|
36 |
+
except requests.exceptions.ConnectionError:
|
37 |
+
return "❌ Error: Connection failed. Please check your internet connection.", ""
|
38 |
except Exception as e:
|
39 |
return f"❌ Error: {str(e)}", ""
|
40 |
+
|
41 |
inference_time = time.time() - start_time
|
42 |
|
43 |
if response.status_code == 200:
|
44 |
+
try:
|
45 |
+
res = response.json()
|
46 |
+
transcription = res.get("transcription", "No transcription returned.")
|
47 |
+
inference_time_str = f"{res.get('time', inference_time):.2f} seconds"
|
48 |
+
return transcription, inference_time_str
|
49 |
+
except ValueError:
|
50 |
+
return "❌ Error: Invalid response format from server.", ""
|
51 |
else:
|
52 |
+
error_msg = f"❌ Error: {response.status_code}"
|
53 |
+
try:
|
54 |
+
error_detail = response.json().get('detail', response.text)
|
55 |
+
error_msg += f", {error_detail}"
|
56 |
+
except:
|
57 |
+
error_msg += f", {response.text[:200]}" # محدود کردن طول پیام خطا
|
58 |
+
return error_msg, ""
|
59 |
|
60 |
with gr.Blocks(css="""
|
61 |
#gooya-title {color:white; background: linear-gradient(90deg, #224CA5 0%, #2CD8D5 100%); border-radius: 12px; padding:20px 10px;margin-bottom:12px;}
|
|
|
104 |
inputs=audio,
|
105 |
outputs=[transcription, inference_time]
|
106 |
)
|
107 |
+
|
108 |
+
# اصلاح دکمه Clear - سه خروجی بجای دو
|
109 |
clear_btn.click(
|
110 |
+
lambda: ("", "", None), # اضافه کردن None برای audio
|
111 |
None,
|
112 |
[transcription, inference_time, audio]
|
113 |
)
|
114 |
|
115 |
+
# تغییر share به False برای Hugging Face Spaces
|
116 |
+
if __name__ == "__main__":
|
117 |
+
demo.launch(
|
118 |
+
share=False, # در Hugging Face Spaces باید False باشد
|
119 |
+
server_name="0.0.0.0",
|
120 |
+
server_port=7860
|
121 |
+
)
|