Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,46 @@ import time
|
|
| 11 |
if not os.path.exists('audio'):
|
| 12 |
os.makedirs('audio')
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Step 1: Transcribe the audio file
|
| 16 |
def transcribe_audio(audio):
|
| 17 |
if audio is None:
|
|
@@ -142,43 +181,7 @@ tts_interface = gr.Interface(
|
|
| 142 |
|
| 143 |
# Combine both interfaces into one
|
| 144 |
demo = gr.TabbedInterface([interface, tts_interface], ["Speech Recognition", "Text-to-Speech"])
|
| 145 |
-
# URL của tệp âm thanh (nguồn từ internet)
|
| 146 |
-
file_url = "https://huggingface.co/spaces/mr2along/speech_recognize/blob/main/paragraph.mp3"
|
| 147 |
-
|
| 148 |
-
# URL để upload tệp âm thanh
|
| 149 |
-
upload_url = "https://mr2along-speech-recognize.hf.space/gradio_api/upload?upload_id=yw08d344te"
|
| 150 |
-
|
| 151 |
-
# Tải tệp âm thanh từ link
|
| 152 |
-
response = requests.get(file_url)
|
| 153 |
-
|
| 154 |
-
# Kiểm tra xem tải tệp thành công hay không
|
| 155 |
-
if response.status_code == 200:
|
| 156 |
-
# Lưu tệp vào bộ nhớ tạm thời
|
| 157 |
-
local_filename = "temp_audio_file.mp3"
|
| 158 |
-
with open(local_filename, 'wb') as f:
|
| 159 |
-
f.write(response.content)
|
| 160 |
-
|
| 161 |
-
# Mở tệp và gửi POST request để upload
|
| 162 |
-
with open(local_filename, 'rb') as audio_file:
|
| 163 |
-
files = {'file': audio_file}
|
| 164 |
-
upload_response = requests.post(upload_url, files=files)
|
| 165 |
-
print(f"Đường dẫn tệp đã tách: {upload_response}")
|
| 166 |
-
|
| 167 |
-
# Xử lý phản hồi từ server sau khi upload
|
| 168 |
-
if upload_response.status_code == 200:
|
| 169 |
-
result = upload_response.json()
|
| 170 |
-
print(f"Đường dẫn tệp đã tách: {result}")
|
| 171 |
-
# Lấy đường dẫn của tệp đã upload
|
| 172 |
-
if isinstance(result, list) and result:
|
| 173 |
-
file_url = result[0]
|
| 174 |
-
extracted_path = os.path.dirname(file_url)
|
| 175 |
-
print(f"Đường dẫn tệp đã tách: {extracted_path}")
|
| 176 |
-
else:
|
| 177 |
-
print(f"Lỗi khi tải lên: {upload_response.status_code}")
|
| 178 |
|
| 179 |
-
# Xóa tệp tạm nếu cần
|
| 180 |
-
os.remove(local_filename)
|
| 181 |
-
else:
|
| 182 |
-
print(f"Lỗi khi tải tệp từ URL: {response.status_code}")
|
| 183 |
# Launch Gradio app
|
| 184 |
demo.launch()
|
|
|
|
|
|
| 11 |
if not os.path.exists('audio'):
|
| 12 |
os.makedirs('audio')
|
| 13 |
|
| 14 |
+
def loadfile():
|
| 15 |
+
# URL của tệp âm thanh (nguồn từ internet)
|
| 16 |
+
file_url = "https://huggingface.co/spaces/mr2along/speech_recognize/blob/main/paragraph.mp3"
|
| 17 |
+
|
| 18 |
+
# URL để upload tệp âm thanh
|
| 19 |
+
upload_url = "https://mr2along-speech-recognize.hf.space/gradio_api/upload?upload_id=yw08d344te"
|
| 20 |
+
|
| 21 |
+
# Tải tệp âm thanh từ link
|
| 22 |
+
response = requests.get(file_url)
|
| 23 |
+
|
| 24 |
+
# Kiểm tra xem tải tệp thành công hay không
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
# Lưu tệp vào bộ nhớ tạm thời
|
| 27 |
+
local_filename = "temp_audio_file.mp3"
|
| 28 |
+
with open(local_filename, 'wb') as f:
|
| 29 |
+
f.write(response.content)
|
| 30 |
+
|
| 31 |
+
# Mở tệp và gửi POST request để upload
|
| 32 |
+
with open(local_filename, 'rb') as audio_file:
|
| 33 |
+
files = {'file': audio_file}
|
| 34 |
+
upload_response = requests.post(upload_url, files=files)
|
| 35 |
+
print(f"Đường dẫn tệp đã tách: {upload_response}")
|
| 36 |
+
|
| 37 |
+
# Xử lý phản hồi từ server sau khi upload
|
| 38 |
+
if upload_response.status_code == 200:
|
| 39 |
+
result = upload_response.json()
|
| 40 |
+
print(f"Đường dẫn tệp đã tách: {result}")
|
| 41 |
+
# Lấy đường dẫn của tệp đã upload
|
| 42 |
+
if isinstance(result, list) and result:
|
| 43 |
+
file_url = result[0]
|
| 44 |
+
extracted_path = os.path.dirname(file_url)
|
| 45 |
+
print(f"Đường dẫn tệp đã tách: {extracted_path}")
|
| 46 |
+
else:
|
| 47 |
+
print(f"Lỗi khi tải lên: {upload_response.status_code}")
|
| 48 |
+
|
| 49 |
+
# Xóa tệp tạm nếu cần
|
| 50 |
+
os.remove(local_filename)
|
| 51 |
+
else:
|
| 52 |
+
print(f"Lỗi khi tải tệp từ URL: {response.status_code}")
|
| 53 |
+
|
| 54 |
# Step 1: Transcribe the audio file
|
| 55 |
def transcribe_audio(audio):
|
| 56 |
if audio is None:
|
|
|
|
| 181 |
|
| 182 |
# Combine both interfaces into one
|
| 183 |
demo = gr.TabbedInterface([interface, tts_interface], ["Speech Recognition", "Text-to-Speech"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
# Launch Gradio app
|
| 186 |
demo.launch()
|
| 187 |
+
loadfile()
|