Spaces:
Runtime error
Runtime error
Commit ·
a57ce2c
1
Parent(s): ab8268b
done
Browse filesFiles changed (2)
- app.py+145-104
- requirements.txt+8-1
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115import gradio as grimport subprocessimport requestsimport reimport traceback# Regular expression to validate YouTube URLsYOUTUBE_URL_PATTERN = re.compile(r'(https?://)?(www\.)?(youtube\.com|youtu\.?be)/.+')def test_network():try:response = requests.get("https://www.youtube.com", timeout=10)if response.status_code == 200:return "Successfully connected to YouTube."else:return f"Failed to connect to YouTube. Status code: {response.status_code}"except Exception as e:return f"Exception occurred: {e}"def test_youtube_transcript(link):debug_info = ""transcript_text = ""thumbnail_url = None# Validate YouTube URLif not YOUTUBE_URL_PATTERN.match(link):return "", "Invalid YouTube URL provided.", None# Add network test result to debug infotest_result = test_network()debug_info += f"Network test result: {test_result}\n"# Include package versionsimport gradioimport yt_dlpimport requestsdebug_info += f"Package versions:\n"debug_info += f"gradio: {gradio.__version__}\n"debug_info += f"yt-dlp: {yt_dlp.version.__version__}\n"debug_info += f"requests: {requests.__version__}\n"try:# Use yt-dlp as a subprocess to download subtitlescommand = ['yt-dlp','--skip-download','--write-subs','--sub-lang', 'en','--sub-format', 'vtt','--output', '%(id)s.%(ext)s',link]result = subprocess.run(command, capture_output=True, text=True)if result.returncode != 0:transcript_text = "Failed to download subtitles using yt-dlp."debug_info += f"yt-dlp error: {result.stderr}\n"return transcript_text, debug_info, thumbnail_url# Extract video ID from URLvideo_id_match = re.search(r'v=([A-Za-z0-9_-]{11})', link)if video_id_match:video_id = video_id_match.group(1)debug_info += f"Video ID: {video_id}\n"thumbnail_url = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"debug_info += f"Thumbnail URL: {thumbnail_url}\n"else:video_id = "unknown"debug_info += "Could not extract Video ID.\n"# Read the downloaded subtitle filesubtitle_file = f"{video_id}.en.vtt"try:with open(subtitle_file, 'r', encoding='utf-8') as f:transcript_text = f.read()# Process the VTT file to extract plain texttranscript_text = re.sub(r'WEBVTT\n\n', '', transcript_text)transcript_text = re.sub(r'\d+\n', '', transcript_text)transcript_text = re.sub(r'\d{2}:\d{2}:\d{2}\.\d{3} --> \d{2}:\d{2}:\d{2}\.\d{3}\n', '', transcript_text)transcript_text = transcript_text.strip()debug_info += "Transcript fetched using yt-dlp subprocess.\n"except FileNotFoundError:transcript_text = "Subtitle file not found."debug_info += "Subtitle file was not found after yt-dlp execution.\n"except Exception as e:transcript_text = f"An error occurred while reading subtitles: {e}"debug_info += f"Error reading subtitles: {e}\n"debug_info += traceback.format_exc()except Exception as e:transcript_text = f"An error occurred with yt-dlp subprocess: {e}"debug_info += f"Error with yt-dlp subprocess: {e}\n"debug_info += traceback.format_exc()return transcript_text, debug_info, thumbnail_url# Gradio interfacedemo = gr.Interface(fn=test_youtube_transcript,inputs=gr.Textbox(label="YouTube Video URL", placeholder="Enter YouTube video URL here"),outputs=[gr.Textbox(label="Transcript", lines=20, interactive=False),gr.Textbox(label="Debug Information", lines=20, interactive=False),gr.Image(label="Thumbnail")],title="YouTube Transcript Fetcher",description="Enter a YouTube video link to fetch the transcript and detailed debug information.")if __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156import gradio as grimport librosaimport numpy as npimport torchfrom diffusers import StableDiffusionPipelineimport osimport gradio as grimport sysprint(f"Gradio version: {gr.__version__}")print(f"Gradio location: {gr.__file__}")print(f"Python executable: {sys.executable}")# Ensure that the script uses CUDA if availabledevice = "cuda" if torch.cuda.is_available() else "cpu"print(f"Using device: {device}")# Load the Stable Diffusion modelmodel_id = "runwayml/stable-diffusion-v1-5" # Updated model ID for better accessibilitytry:stable_diffusion = StableDiffusionPipeline.from_pretrained(model_id,torch_dtype=torch.float16 if device == "cuda" else torch.float32).to(device)except Exception as e:print(f"Error loading the model: {e}")print("Ensure you have the correct model ID and access rights.")exit(1)def describe_audio(audio_path):"""Generate a textual description based on audio features.Parameters:audio_path (str): Path to the audio file.Returns:str: Generated description."""try:# Load the audio filey, sr = librosa.load(audio_path, sr=None)# Extract Mel SpectrogramS = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)db_spec = librosa.power_to_db(S, ref=np.max)# Calculate average amplitude and frequencyavg_amplitude = np.mean(db_spec)spectral_centroids = librosa.feature.spectral_centroid(y=y, sr=sr)avg_frequency = np.mean(spectral_centroids)# Generate description based on amplitudeif avg_amplitude < -40:amplitude_desc = "a calm and serene landscape with gentle waves"elif avg_amplitude < -20:amplitude_desc = "a vibrant forest with rustling leaves"else:amplitude_desc = "a thunderstorm with dark clouds and lightning"# Generate description based on frequencyif avg_frequency < 2000:frequency_desc = "under soft, ambient light"elif avg_frequency < 4000:frequency_desc = "with vivid and lively colors"else:frequency_desc = "in a surreal and dynamic setting"# Combine descriptionsdescription = f"{amplitude_desc} {frequency_desc}"return descriptionexcept Exception as e:print(f"Error processing audio: {e}")return "an abstract artistic scene"def generate_image(description):"""Generate an image using the Stable Diffusion model based on the description.Parameters:description (str): Textual description for image generation.Returns:PIL.Image: Generated image."""try:if device == "cuda":with torch.autocast("cuda"):image = stable_diffusion(description).images[0]else:image = stable_diffusion(description).images[0]return imageexcept Exception as e:print(f"Error generating image: {e}")return Nonedef audio_to_image(audio_file):"""Convert an audio file to an artistic image.Parameters:audio_file (str): Path to the uploaded audio file.Returns:PIL.Image or str: Generated image or error message."""if audio_file is None:return "No audio file provided."description = describe_audio(audio_file)print(f"Generated Description: {description}")image = generate_image(description)if image is not None:return imageelse:return "Failed to generate image."# Gradio Interfacetitle = "🎵 Audio to Artistic Image Converter 🎨"description_text = """Upload an audio file, and this app will generate an artistic image based on the sound's characteristics."""# Define example pathsexample_paths = ["example_audio/calm_ocean.wav","example_audio/rustling_leaves.wav","example_audio/thunderstorm.wav",]# Verify example files existvalid_examples = []for path in example_paths:if os.path.isfile(path):valid_examples.append([path])else:print(f"Example file not found: {path}")if not os.path.exists("example_audio"):os.makedirs("example_audio")print("Please add some example audio files in the 'example_audio' directory.")interface = gr.Interface(fn=audio_to_image,inputs=gr.Audio(source="upload", type="filepath"),outputs=gr.Image(type="pil"),title=title,description=description_text,examples=valid_examples if valid_examples else None,allow_flagging="never",theme="default")if __name__ == "__main__":interface.launch()
123gradio==4.44.1yt-dlp==2023.10.7requests==2.32.312345678910yt-dlp==2023.10.7requests==2.32.3accelerategradio>=4.44.1librosanumpytorchdiffusersacceleratepsutil
app.py+145-104
requirements.txt+8-1