Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -71,11 +71,17 @@
|
|
71 |
# # Launch the Gradio interface
|
72 |
# iface.launch()
|
73 |
|
74 |
-
import requests
|
75 |
-
import gradio as gr
|
76 |
import os
|
77 |
import json
|
|
|
78 |
import ffmpeg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
# Function to convert video to audio using ffmpeg
|
81 |
def convert_video_to_audio(video_path):
|
@@ -93,9 +99,25 @@ def convert_video_to_audio(video_path):
|
|
93 |
except Exception as e:
|
94 |
return f"Error converting video: {str(e)}"
|
95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
# Function to recognize audio from URL or uploaded file
|
97 |
def recognize_audio(choice, url, file):
|
98 |
-
api_url = os.getenv("API_URL"
|
99 |
params = {
|
100 |
"return": "apple_music,spotify",
|
101 |
"api_token": os.getenv("API_TOKEN")
|
@@ -105,8 +127,22 @@ def recognize_audio(choice, url, file):
|
|
105 |
if choice == "URL":
|
106 |
if not url:
|
107 |
return "Please enter a valid URL."
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
# Check if file is uploaded
|
112 |
elif choice == "Upload File":
|
@@ -117,8 +153,6 @@ def recognize_audio(choice, url, file):
|
|
117 |
file_extension = file.split('.')[-1].lower()
|
118 |
audio_file_path = file
|
119 |
|
120 |
-
video_formats = ['3gp', 'asf', 'avi', 'divx', 'flv', 'm2ts', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mts', 'ts', 'vob', 'webm', 'wmv', 'xvid']
|
121 |
-
|
122 |
if file_extension in video_formats:
|
123 |
# Convert video to audio file (mp3 format)
|
124 |
audio_file_path = convert_video_to_audio(file)
|
@@ -180,12 +214,23 @@ def recognize_audio(choice, url, file):
|
|
180 |
except Exception as e:
|
181 |
return f"Error parsing response: {str(e)}"
|
182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
# Gradio Interface
|
184 |
interface = gr.Interface(
|
185 |
fn=recognize_audio,
|
186 |
inputs=[
|
187 |
gr.Radio(["URL", "Upload File"], label="Select Input Method"),
|
188 |
-
gr.Textbox(label="Enter Audio URL", placeholder="https://example.com/audio.mp3"),
|
189 |
gr.File(label="Upload Audio or Video File", type="filepath") # Menggunakan filepath agar sesuai dengan Gradio
|
190 |
],
|
191 |
outputs=gr.Markdown(label="Recognition Result"),
|
@@ -195,4 +240,5 @@ interface = gr.Interface(
|
|
195 |
|
196 |
# Run Gradio App
|
197 |
if __name__ == "__main__":
|
|
|
198 |
interface.launch()
|
|
|
71 |
# # Launch the Gradio interface
|
72 |
# iface.launch()
|
73 |
|
|
|
|
|
74 |
import os
|
75 |
import json
|
76 |
+
import requests
|
77 |
import ffmpeg
|
78 |
+
import gradio as gr
|
79 |
+
import tempfile
|
80 |
+
import threading
|
81 |
+
import time
|
82 |
+
|
83 |
+
# Video formats that are supported
|
84 |
+
video_formats = ['3gp', 'asf', 'avi', 'divx', 'flv', 'm2ts', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mts', 'ts', 'vob', 'webm', 'wmv', 'xvid']
|
85 |
|
86 |
# Function to convert video to audio using ffmpeg
|
87 |
def convert_video_to_audio(video_path):
|
|
|
99 |
except Exception as e:
|
100 |
return f"Error converting video: {str(e)}"
|
101 |
|
102 |
+
# Function to download file from URL
|
103 |
+
def download_file(url):
|
104 |
+
try:
|
105 |
+
response = requests.get(url, stream=True)
|
106 |
+
filename = os.path.join("temp_downloads", url.split("/")[-1]) # Save in temp_downloads folder
|
107 |
+
os.makedirs("temp_downloads", exist_ok=True)
|
108 |
+
|
109 |
+
# Save file content
|
110 |
+
with open(filename, 'wb') as f:
|
111 |
+
for chunk in response.iter_content(chunk_size=8192):
|
112 |
+
f.write(chunk)
|
113 |
+
|
114 |
+
return filename
|
115 |
+
except Exception as e:
|
116 |
+
return f"Error downloading file: {str(e)}"
|
117 |
+
|
118 |
# Function to recognize audio from URL or uploaded file
|
119 |
def recognize_audio(choice, url, file):
|
120 |
+
api_url = os.getenv("API_URL")
|
121 |
params = {
|
122 |
"return": "apple_music,spotify",
|
123 |
"api_token": os.getenv("API_TOKEN")
|
|
|
127 |
if choice == "URL":
|
128 |
if not url:
|
129 |
return "Please enter a valid URL."
|
130 |
+
|
131 |
+
# Check if the URL ends with a video file extension
|
132 |
+
url_extension = url.split('.')[-1].lower()
|
133 |
+
|
134 |
+
if url_extension in video_formats:
|
135 |
+
# Download video and process as video
|
136 |
+
video_path = download_file(url)
|
137 |
+
if video_path.startswith("Error"):
|
138 |
+
return video_path
|
139 |
+
audio_file_path = convert_video_to_audio(video_path)
|
140 |
+
if audio_file_path.startswith("Error"):
|
141 |
+
return audio_file_path
|
142 |
+
else:
|
143 |
+
# Process URL as audio
|
144 |
+
params['url'] = url
|
145 |
+
response = requests.post(api_url, data=params)
|
146 |
|
147 |
# Check if file is uploaded
|
148 |
elif choice == "Upload File":
|
|
|
153 |
file_extension = file.split('.')[-1].lower()
|
154 |
audio_file_path = file
|
155 |
|
|
|
|
|
156 |
if file_extension in video_formats:
|
157 |
# Convert video to audio file (mp3 format)
|
158 |
audio_file_path = convert_video_to_audio(file)
|
|
|
214 |
except Exception as e:
|
215 |
return f"Error parsing response: {str(e)}"
|
216 |
|
217 |
+
# Function to cache the audio file and delete it after 15 minutes
|
218 |
+
def cache_and_delete_file(audio_file_path):
|
219 |
+
# Wait for 15 minutes before deleting the file
|
220 |
+
time.sleep(900) # 900 seconds = 15 minutes
|
221 |
+
|
222 |
+
try:
|
223 |
+
os.remove(audio_file_path)
|
224 |
+
print(f"File {audio_file_path} has been deleted after 15 minutes.")
|
225 |
+
except Exception as e:
|
226 |
+
print(f"Error deleting file {audio_file_path}: {str(e)}")
|
227 |
+
|
228 |
# Gradio Interface
|
229 |
interface = gr.Interface(
|
230 |
fn=recognize_audio,
|
231 |
inputs=[
|
232 |
gr.Radio(["URL", "Upload File"], label="Select Input Method"),
|
233 |
+
gr.Textbox(label="Enter Audio/Video URL", placeholder="https://example.com/audio.mp3 or video.mp4"),
|
234 |
gr.File(label="Upload Audio or Video File", type="filepath") # Menggunakan filepath agar sesuai dengan Gradio
|
235 |
],
|
236 |
outputs=gr.Markdown(label="Recognition Result"),
|
|
|
240 |
|
241 |
# Run Gradio App
|
242 |
if __name__ == "__main__":
|
243 |
+
# Launch the Gradio interface
|
244 |
interface.launch()
|