Spaces:
Running
Running
File size: 8,325 Bytes
aabd712 a98d34a 30b04a4 4b00a9a 3da3c9d 4b00a9a 3da3c9d 908cfbb 3da3c9d 3e5c391 4b00a9a 3e5c391 4b00a9a d2237c6 b1ca521 3da3c9d d2237c6 3e5c391 3da3c9d 3e5c391 4b00a9a f9a0d06 3da3c9d 3e5c391 92f3382 3da3c9d 3e5c391 f9a0d06 aabd712 3e5c391 03dc344 9cd4fcb 03dc344 9cd4fcb 7c06ed6 03dc344 7c06ed6 03dc344 7c06ed6 03dc344 aabd712 4b00a9a 3e5c391 aabd712 3e5c391 4b00a9a 3da3c9d aabd712 03dc344 aabd712 94bdcc4 da3e61e 3e5c391 aabd712 4b00a9a aabd712 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# import gradio as gr
# import os
# from acrcloud.recognizer import ACRCloudRecognizer
# import tempfile
# import shutil
# import json
# # Retrieve ACRCloud credentials from environment variables
# acr_access_key = os.environ.get('ACR_ACCESS_KEY')
# acr_access_secret = os.environ.get('ACR_ACCESS_SECRET')
# acr_host = 'identify-ap-southeast-1.acrcloud.com' # os.environ.get('ACR_HOST', 'eu-west-1.api.acrcloud.com')
# # ACRCloud recognizer configuration
# config = {
# 'host': acr_host,
# 'access_key': acr_access_key,
# 'access_secret': acr_access_secret,
# 'timeout': 10 # seconds
# }
# # Initialize ACRCloud recognizer
# acr = ACRCloudRecognizer(config)
# def identify_audio(file):
# # Gradio provides a file object, and file.name contains the path
# file_path = file.name # Gradio file object already provides a file path
# # Get the duration of the audio file in milliseconds
# duration_ms = int(acr.get_duration_ms_by_file(file_path))
# results = []
# # Full recognition result
# full_result = acr.recognize_by_file(file_path, 0)
# full_result_dict = json.loads(full_result)
# music = full_result_dict['metadata']['music'][0]
# # Spotify link
# spotify_track_id = music['external_metadata']['spotify']['track']['id']
# spotify_link = f"https://open.spotify.com/track/{spotify_track_id}"
# # Deezer link
# deezer_track_id = music['external_metadata']['deezer']['track']['id']
# deezer_link = f"https://www.deezer.com/track/{deezer_track_id}"
# # Final markdown result
# result_md = f"""
# ### **Full Result**:
# - **Track**: {music['title']}
# - **Artist**: {music['artists'][0]['name']}
# - **Album**: {music['album']['name']}
# - **Release Date**: {music['release_date']}
# - **Score**: {music['score']}%
# - **Download Link**:
# - [Listen on Spotify]({spotify_link})
# - [Listen on Deezer]({deezer_link})
# """
# return gr.Markdown(result_md)
# # Create Gradio interface
# iface = gr.Interface(
# fn=identify_audio,
# inputs=gr.File(label="Upload Audio or Video File"),
# outputs=gr.Markdown(label="Audio Metadata"),
# title="Audio Search by File (Support Audio or Video File)",
# description="Upload an audio or video file to identify it using ACRCloud."
# )
# # Launch the Gradio interface
# iface.launch()
import os
import json
import requests
import ffmpeg
import gradio as gr
import tempfile
import threading
import time
# Video formats that are supported
video_formats = ['3gp', 'asf', 'avi', 'divx', 'flv', 'm2ts', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'mts', 'ts', 'vob', 'webm', 'wmv', 'xvid']
# Function to convert video to audio using ffmpeg
def convert_video_to_audio(video_path):
try:
# Ensure the directory exists
output_dir = "flowly_ai_audio_converter"
os.makedirs(output_dir, exist_ok=True)
# Correctly construct the output file path
output_path = os.path.join(output_dir, os.path.splitext(os.path.basename(video_path))[0] + ".mp3")
# Run ffmpeg conversion
ffmpeg.input(video_path).output(output_path).run()
return output_path
except Exception as e:
return f"Error converting video: {str(e)}"
# Function to download file from URL
def download_file(url):
try:
response = requests.get(url, stream=True)
filename = os.path.join("temp_downloads", url.split("/")[-1]) # Save in temp_downloads folder
os.makedirs("temp_downloads", exist_ok=True)
# Save file content
with open(filename, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
return filename
except Exception as e:
return f"Error downloading file: {str(e)}"
# Function to recognize audio from URL or uploaded file
def recognize_audio(choice, url, file):
api_url = os.getenv("API_URL")
params = {
"return": "apple_music,spotify",
"api_token": os.getenv("API_TOKEN")
}
# Check if URL is provided
if choice == "URL":
if not url:
return "Please enter a valid URL."
# Check if the URL ends with a video file extension
url_extension = url.split('.')[-1].lower()
if url_extension in video_formats:
# Download video and process as video
video_path = download_file(url)
if video_path.startswith("Error"):
return video_path
audio_file_path = convert_video_to_audio(video_path)
if audio_file_path.startswith("Error"):
return audio_file_path
else:
# Process URL as audio
params['url'] = url
response = requests.post(api_url, data=params)
# Check if file is uploaded
elif choice == "Upload File":
if not file:
return "Please upload a valid audio file."
# Check if the uploaded file is a video (e.g., mp4)
file_extension = file.split('.')[-1].lower()
audio_file_path = file
if file_extension in video_formats:
# Convert video to audio file (mp3 format)
audio_file_path = convert_video_to_audio(file)
if audio_file_path.startswith("Error"):
return audio_file_path
# If it's already an audio file, use it as is
with open(audio_file_path, "rb") as f:
response = requests.post(api_url, data=params, files={'file': f})
else:
return "Please select a method (URL or Upload File)."
# Parse the response into a structured format
try:
if response.status_code != 200:
return f"Error: Received unexpected status code {response.status_code}"
# Try to parse the response as JSON
data = response.json()
# Check if the response is valid
if not data:
return "Error: No data received from the API."
# Check if there's an error in the response
if data.get("status") == "error":
error_message = data.get("error", {}).get("error_message", "Unknown error.")
return f"""
### Song recognition failed
{error_message}
"""
result = data.get('result', {})
artist = result.get('artist', 'Unknown Artist')
title = result.get('title', 'Unknown Title')
album = result.get('album', 'Unknown Album')
release_date = result.get('release_date', 'Unknown Date')
song_link = result.get('song_link', '')
apple_music_link = result.get('apple_music', {}).get('url', '')
spotify_link = result.get('spotify', {}).get('external_urls', {}).get('spotify', '')
markdown_output = f"""
### Song Recognition Result
- **Title**: {title}
- **Artist**: {artist}
- **Album**: {album}
- **Release Date**: {release_date}
[Listen on Apple Music]({apple_music_link})
[Listen on Spotify]({spotify_link})
#### Song Link:
[Click here to listen]({song_link})
"""
return markdown_output
except Exception as e:
return f"Error parsing response: {str(e)}"
# Function to cache the audio file and delete it after 15 minutes
def cache_and_delete_file(audio_file_path):
# Wait for 15 minutes before deleting the file
time.sleep(900) # 900 seconds = 15 minutes
try:
os.remove(audio_file_path)
print(f"File {audio_file_path} has been deleted after 15 minutes.")
except Exception as e:
print(f"Error deleting file {audio_file_path}: {str(e)}")
# Gradio Interface
interface = gr.Interface(
fn=recognize_audio,
inputs=[
gr.Radio(["URL", "Upload File"], label="Select Input Method"),
gr.Textbox(label="Enter Audio/Video URL", placeholder="https://example.com/audio.mp3 or video.mp4"),
gr.File(label="Upload Audio or Video File", type="filepath") # Menggunakan filepath agar sesuai dengan Gradio
],
outputs=gr.Markdown(label="Recognition Result"),
title="Audio Recognition",
description="Choose a method: Upload an audio/video file or enter a URL to identify the song.",
css="footer {visibility: hidden}"
)
# Run Gradio App
if __name__ == "__main__":
# Launch the Gradio interface
interface.launch()
|