Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -75,25 +75,51 @@ import requests
|
|
| 75 |
import gradio as gr
|
| 76 |
import os
|
| 77 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
# Function to recognize audio from URL or uploaded file
|
| 80 |
def recognize_audio(choice, url, file):
|
| 81 |
api_url = os.getenv("API_URL", "https://api.audd.io/").strip('"')
|
| 82 |
params = {
|
| 83 |
"return": "apple_music,spotify",
|
| 84 |
-
"api_token": "
|
| 85 |
}
|
| 86 |
|
|
|
|
| 87 |
if choice == "URL":
|
| 88 |
if not url:
|
| 89 |
return "Please enter a valid URL."
|
| 90 |
params['url'] = url
|
| 91 |
response = requests.post(api_url, data=params)
|
| 92 |
|
|
|
|
| 93 |
elif choice == "Upload File":
|
| 94 |
if not file:
|
| 95 |
return "Please upload a valid audio file."
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
response = requests.post(api_url, data=params, files={'file': f})
|
| 98 |
|
| 99 |
else:
|
|
@@ -145,11 +171,11 @@ interface = gr.Interface(
|
|
| 145 |
inputs=[
|
| 146 |
gr.Radio(["URL", "Upload File"], label="Select Input Method"),
|
| 147 |
gr.Textbox(label="Enter Audio URL", placeholder="https://example.com/audio.mp3"),
|
| 148 |
-
gr.File(label="Upload Audio File", type="filepath") # Menggunakan filepath agar sesuai dengan Gradio
|
| 149 |
],
|
| 150 |
outputs=gr.Markdown(label="Recognition Result"),
|
| 151 |
title="Audio Recognition",
|
| 152 |
-
description="Choose a method: Upload an audio file or enter a URL to identify the song."
|
| 153 |
)
|
| 154 |
|
| 155 |
# Run Gradio App
|
|
|
|
| 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):
|
| 82 |
+
try:
|
| 83 |
+
output_path = f"flowly_ai_audio_converter{os.path.splittext(video_path)[0]}.mp3"
|
| 84 |
+
ffmpeg.input(video_path).output(output_path).run()
|
| 85 |
+
return output_path
|
| 86 |
+
except Exception as e:
|
| 87 |
+
return f"Error converting video: {str(e)}"
|
| 88 |
|
| 89 |
# Function to recognize audio from URL or uploaded file
|
| 90 |
def recognize_audio(choice, url, file):
|
| 91 |
api_url = os.getenv("API_URL", "https://api.audd.io/").strip('"')
|
| 92 |
params = {
|
| 93 |
"return": "apple_music,spotify",
|
| 94 |
+
"api_token": os.getenv("API_TOKEN")
|
| 95 |
}
|
| 96 |
|
| 97 |
+
# Check if URL is provided
|
| 98 |
if choice == "URL":
|
| 99 |
if not url:
|
| 100 |
return "Please enter a valid URL."
|
| 101 |
params['url'] = url
|
| 102 |
response = requests.post(api_url, data=params)
|
| 103 |
|
| 104 |
+
# Check if file is uploaded
|
| 105 |
elif choice == "Upload File":
|
| 106 |
if not file:
|
| 107 |
return "Please upload a valid audio file."
|
| 108 |
+
|
| 109 |
+
# Check if the uploaded file is a video (e.g., mp4)
|
| 110 |
+
file_extension = file.split('.')[-1].lower()
|
| 111 |
+
audio_file_path = file
|
| 112 |
+
|
| 113 |
+
video_formats = [data.upper() for data in (sorted(['3GP', 'ASF', 'AVI', 'DIVX', 'FLV', 'M2TS', 'M4V', 'MKV', 'MOV', 'MP4', 'MPEG', 'MPG', 'MTS', 'TS', 'VOB', 'WEBM', 'WMV', 'XVID'])) if data not in ['3GP', 'DIVX', 'XVID']]
|
| 114 |
+
|
| 115 |
+
if file_extension in video_formats:
|
| 116 |
+
# Convert video to audio file (mp3 format)
|
| 117 |
+
audio_file_path = convert_video_to_audio(file)
|
| 118 |
+
if audio_file_path.startswith("Error"):
|
| 119 |
+
return audio_file_path
|
| 120 |
+
|
| 121 |
+
# If it's already an audio file, use it as is
|
| 122 |
+
with open(audio_file_path, "rb") as f:
|
| 123 |
response = requests.post(api_url, data=params, files={'file': f})
|
| 124 |
|
| 125 |
else:
|
|
|
|
| 171 |
inputs=[
|
| 172 |
gr.Radio(["URL", "Upload File"], label="Select Input Method"),
|
| 173 |
gr.Textbox(label="Enter Audio URL", placeholder="https://example.com/audio.mp3"),
|
| 174 |
+
gr.File(label="Upload Audio or Video File", type="filepath") # Menggunakan filepath agar sesuai dengan Gradio
|
| 175 |
],
|
| 176 |
outputs=gr.Markdown(label="Recognition Result"),
|
| 177 |
title="Audio Recognition",
|
| 178 |
+
description="Choose a method: Upload an audio/video file or enter a URL to identify the song."
|
| 179 |
)
|
| 180 |
|
| 181 |
# Run Gradio App
|