Spaces:
Sleeping
Sleeping
File size: 4,749 Bytes
c5d71f9 e7bc4b9 d87992a 290f77e d87992a 290f77e c5d71f9 e7e0cc4 d87992a e7e0cc4 c5d71f9 d87992a c5d71f9 d87992a c5d71f9 e7bc4b9 d87992a e7e0cc4 d87992a e7e0cc4 d87992a e7bc4b9 d87992a e7bc4b9 e7e0cc4 c5d71f9 d87992a e7bc4b9 c5d71f9 d87992a c5d71f9 d87992a c5d71f9 d87992a c5d71f9 e7bc4b9 e7e0cc4 e7bc4b9 d87992a e7bc4b9 d87992a 290f77e d87992a e7e0cc4 d87992a 290f77e d87992a e7bc4b9 d87992a 290f77e d87992a 290f77e d87992a 290f77e d87992a 290f77e | 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 | import os
import logging
import requests
import random
from PIL import Image, ImageDraw
from io import BytesIO
from pytrends.request import TrendReq
from moviepy.editor import ImageClip, AudioFileClip
from gtts import gTTS
import gradio as gr
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
ASSETS_DIR = "assets"
os.makedirs(ASSETS_DIR, exist_ok=True)
def fetch_trending_topic():
try:
logging.info("Fetching trending topics from Google...")
pytrends = TrendReq(hl='en-US', tz=360)
pytrends.build_payload(kw_list=[""])
trending = pytrends.trending_searches(pn='united_states')
topic = random.choice(trending[0].tolist())
logging.info(f"Trending Topic Fetched: {topic}")
return topic
except Exception as e:
logging.error(f"Failed to fetch trends: {e}")
fallback_topics = ["Global News", "AI Breakthrough", "SpaceX Launch", "Stock Market Update"]
fallback = random.choice(fallback_topics)
logging.info(f"Using fallback trending topic: {fallback}")
return fallback
def summarize_news(news_text):
logging.info(f"Summarizing news: {news_text}")
summary = news_text + " Follow us on Facebook and Twitter to see what's trending."
logging.info(f"Summary generated: {summary}")
return summary
def generate_tts(summary_text):
try:
logging.info("Generating TTS audio...")
tts = gTTS(summary_text)
audio_path = os.path.join(ASSETS_DIR, "news_audio.wav")
tts.save(audio_path)
logging.info(f"TTS audio saved to {audio_path}")
return audio_path
except Exception as e:
logging.error(f"TTS generation failed: {e}")
return None
def fetch_image():
try:
img_url = "https://picsum.photos/800/600"
logging.info(f"Fetching image from {img_url}")
response = requests.get(img_url)
if response.status_code == 200:
img = Image.open(BytesIO(response.content))
img_path = os.path.join(ASSETS_DIR, "news_img.jpg")
img.save(img_path)
logging.info("Image fetched and saved.")
return img_path
else:
raise ValueError("Image fetch returned non-200")
except Exception as e:
logging.error(f"Image fetch failed: {e}")
return None
def create_video(image_path, audio_path):
try:
logging.info("Creating video...")
audio_clip = AudioFileClip(audio_path)
image_clip = ImageClip(image_path).set_duration(audio_clip.duration).set_audio(audio_clip)
image_clip = image_clip.set_fps(24)
video_path = os.path.join(ASSETS_DIR, "news_video.mp4")
image_clip.write_videofile(video_path, codec="libx264", audio_codec="aac")
logging.info(f"Video created successfully at {video_path}")
return video_path
except Exception as e:
logging.error(f"Video creation failed: {e}")
return None
def create_dummy_video():
logging.info("Creating fallback dummy video (red screen)...")
dummy_img = Image.new('RGB', (800, 600), color='red')
draw = ImageDraw.Draw(dummy_img)
draw.text((250, 280), "No Image Available", fill='white')
dummy_img_path = os.path.join(ASSETS_DIR, "dummy_img.jpg")
dummy_img.save(dummy_img_path)
audio_clip = AudioFileClip(os.path.join(ASSETS_DIR, "news_audio.wav"))
image_clip = ImageClip(dummy_img_path).set_duration(audio_clip.duration).set_audio(audio_clip)
image_clip = image_clip.set_fps(24)
video_path = os.path.join(ASSETS_DIR, "news_video.mp4")
image_clip.write_videofile(video_path, codec="libx264", audio_codec="aac")
logging.info(f"Dummy video created at {video_path}")
return video_path
def generate_news_video():
topic = fetch_trending_topic()
summary = summarize_news(topic)
audio_path = generate_tts(summary)
image_path = fetch_image()
if audio_path and image_path:
video_path = create_video(image_path, audio_path)
if video_path:
logging.info(f"News video ready: {video_path}")
return video_path
else:
logging.error("Video generation failed, triggering fallback...")
return create_dummy_video()
else:
logging.error("Generation failed: Audio or Image generation failed")
return create_dummy_video()
# ✅ Gradio Interface
iface = gr.Interface(
fn=generate_news_video,
inputs=[],
outputs=gr.Video(label="Generated News Video"),
title="Trending News Video Generator",
description="Click to generate a news video from trending topics."
)
if __name__ == "__main__":
iface.launch(server_name="0.0.0.0", server_port=7860)
|