Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os
|
|
|
2 |
import speech_recognition as sr
|
3 |
import difflib
|
4 |
import gradio as gr
|
@@ -9,6 +10,43 @@ import time
|
|
9 |
# Create audio directory if it doesn't exist
|
10 |
if not os.path.exists('audio'):
|
11 |
os.makedirs('audio')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Step 1: Transcribe the audio file
|
14 |
def transcribe_audio(audio):
|
|
|
1 |
import os
|
2 |
+
import requests
|
3 |
import speech_recognition as sr
|
4 |
import difflib
|
5 |
import gradio as gr
|
|
|
10 |
# Create audio directory if it doesn't exist
|
11 |
if not os.path.exists('audio'):
|
12 |
os.makedirs('audio')
|
13 |
+
# URL của tệp âm thanh (nguồn từ internet)
|
14 |
+
file_url = "https://example.com/path_to_audio_file.mp3"
|
15 |
+
|
16 |
+
# URL để upload tệp âm thanh
|
17 |
+
upload_url = "https://mr2along-speech-recognize.hf.space/gradio_api/upload?upload_id=yw08d344te"
|
18 |
+
|
19 |
+
# Tải tệp âm thanh từ link
|
20 |
+
response = requests.get(file_url)
|
21 |
+
|
22 |
+
# Kiểm tra xem tải tệp thành công hay không
|
23 |
+
if response.status_code == 200:
|
24 |
+
# Lưu tệp vào bộ nhớ tạm thời
|
25 |
+
local_filename = "temp_audio_file.mp3"
|
26 |
+
with open(local_filename, 'wb') as f:
|
27 |
+
f.write(response.content)
|
28 |
+
|
29 |
+
# Mở tệp và gửi POST request để upload
|
30 |
+
with open(local_filename, 'rb') as audio_file:
|
31 |
+
files = {'file': audio_file}
|
32 |
+
upload_response = requests.post(upload_url, files=files)
|
33 |
+
|
34 |
+
# Xử lý phản hồi từ server sau khi upload
|
35 |
+
if upload_response.status_code == 200:
|
36 |
+
result = upload_response.json()
|
37 |
+
|
38 |
+
# Lấy đường dẫn của tệp đã upload
|
39 |
+
if isinstance(result, list) and result:
|
40 |
+
file_url = result[0]
|
41 |
+
extracted_path = os.path.dirname(file_url)
|
42 |
+
print(f"Đường dẫn tệp đã tách: {extracted_path}")
|
43 |
+
else:
|
44 |
+
print(f"Lỗi khi tải lên: {upload_response.status_code}")
|
45 |
+
|
46 |
+
# Xóa tệp tạm nếu cần
|
47 |
+
os.remove(local_filename)
|
48 |
+
else:
|
49 |
+
print(f"Lỗi khi tải tệp từ URL: {response.status_code}")
|
50 |
|
51 |
# Step 1: Transcribe the audio file
|
52 |
def transcribe_audio(audio):
|