Spaces:
Runtime error
Runtime error
Update app.py
Browse files3 unmodified lines456789101143 unmodified lines5556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311328 unmodified lines1411421431441451461471481491501519 unmodified lines1611621631641651661673 unmodified linesfrom google.genai import typesimport reMODEL_ID = "gemini-2.0-flash"DEFAULT_PROMPT = """You are a phoneme-precise music transcriptionist and sonic prompt architect. Your task is to analyze the provided audio and return a Suno-compatible output consisting of:β’ Structured section headers for musical form43 unmodified lines- All output must be parsable by a downstream music generation agent"""def validate_youtube_url(url: str) -> bool:if not url:return Falseyoutube_patterns = [r'https?://(?:www\.)?youtube\.com/watch\?v=[\w-]+',r'https?://youtu\.be/[\w-]+',r'https?://(?:www\.)?youtube\.com/embed/[\w-]+',r'https?://(?:m\.)?youtube\.com/watch\?v=[\w-]+']return any(re.match(pattern, url.strip()) for pattern in youtube_patterns)def analyze_audio(audio_file, youtube_url, api_key, custom_prompt):if not api_key or not api_key.strip():return "β Please enter your Google AI API key"try:client = genai.Client(api_key=api_key.strip())except Exception as e:return f"β API Connection Failed: {str(e)}"# YouTube URLif youtube_url and youtube_url.strip():url = youtube_url.strip()if not validate_youtube_url(url):return "β Invalid YouTube URL"try:response = client.models.generate_content(model=MODEL_ID,contents=[types.Part.from_text(custom_prompt),types.Part.from_uri(file_uri=url,mime_type="video/mp4")])return response.textexcept Exception as e:return f"β YouTube Analysis Failed: {str(e)}"# Audio fileelif audio_file is not None:try:file_size = os.path.getsize(audio_file)if file_size > 20 * 1024 * 1024:uploaded_file = client.files.upload(file=audio_file)response = client.models.generate_content(model=MODEL_ID,contents=[custom_prompt, uploaded_file])else:with open(audio_file, 'rb') as f:audio_bytes = f.read()response = client.models.generate_content(model=MODEL_ID,contents=[custom_prompt,types.Part.from_bytes(data=audio_bytes,mime_type="audio/mp3")])return response.textexcept Exception as e:return f"β File Analysis Failed: {str(e)}"else:return "β Please upload audio file OR enter YouTube URL"with gr.Blocks() as demo:gr.Markdown("# π§ Audio β Suno Analyzer")8 unmodified linestype="filepath")youtube_url = gr.Textbox(label="YouTube URL",placeholder="https://www.youtube.com/watch?v=VIDEO_ID")custom_prompt_input = gr.Textbox(value=DEFAULT_PROMPT,label="Prompt",9 unmodified linesanalyze_btn.click(fn=analyze_audio,inputs=[audio_file, youtube_url, api_key_input, custom_prompt_input],outputs=[results_output])3 unmodified lines45678943 unmodified lines53545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798998 unmodified lines1081091101111121139 unmodified lines1231241251261271281293 unmodified linesfrom google.genai import typesimport reDEFAULT_PROMPT = """You are a phoneme-precise music transcriptionist and sonic prompt architect. Your task is to analyze the provided audio and return a Suno-compatible output consisting of:β’ Structured section headers for musical form43 unmodified lines- All output must be parsable by a downstream music generation agent"""def analyze_audio(audio_file, api_key, custom_prompt):if not api_key or not api_key.strip():return "β Please enter your Google AI API key"if audio_file is None:return "β Please upload an audio file"try:# Initialize Gemini client following official docsclient = genai.Client(api_key=api_key.strip())# Get file size to determine upload methodfile_size = os.path.getsize(audio_file)if file_size > 20 * 1024 * 1024: # 20MB threshold from docs# Use Files API for large files (following official example)myfile = client.files.upload(file=audio_file)response = client.models.generate_content(model="gemini-2.0-flash",contents=[custom_prompt, myfile])else:# Pass inline audio data (following official example)with open(audio_file, 'rb') as f:audio_bytes = f.read()response = client.models.generate_content(model='gemini-2.0-flash',contents=[custom_prompt,types.Part.from_bytes(data=audio_bytes,mime_type='audio/mp3',)])return response.textexcept Exception as e:return f"β Error: {str(e)}"with gr.Blocks() as demo:gr.Markdown("# π§ Audio β Suno Analyzer")8 unmodified linestype="filepath")custom_prompt_input = gr.Textbox(value=DEFAULT_PROMPT,label="Prompt",9 unmodified linesanalyze_btn.click(fn=analyze_audio,inputs=[audio_file, api_key_input, custom_prompt_input],outputs=[results_output])
app.py+31-69