import os import requests import gradio as gr from huggingface_hub import InferenceClient from dotenv import load_dotenv # Load environment variables load_dotenv() # Configuration HF_TOKEN = os.getenv("HF_TOKEN") REDDIT_API = "https://www.reddit.com/r/trending/top.json?limit=5" DEFAULT_TRENDS = ["Celebrity gossip", "Tech news", "Movie drama", "Gaming leaks"] STYLES = ["Realistic", "Cartoon", "Anime", "3D Render"] # Initialize clients client = InferenceClient(token=HF_TOKEN) def get_trends(): """Fetch trending topics with Reddit API fallback""" try: res = requests.get(REDDIT_API, headers={"User-Agent": "MemeBot/1.0"}, timeout=3) return [post["data"]["title"] for post in res.json()["data"]["children"]] except: return DEFAULT_TRENDS def generate_meme(prompt: str, style: str): """Generate meme using DeepSeek Janus-Pro""" try: enhanced_prompt = ( f"Trending meme template ({style} style): {prompt}. " "Viral social media format, high contrast, funny text overlay" ) image = client.text_to_image( model="deepseek-ai/janus-pro", prompt=enhanced_prompt, negative_prompt="low quality, text, watermark", height=512, width=512 ) return image except Exception as e: print(f"Generation error: {e}") return None # Build UI with gr.Blocks(title="🚀 Viral Meme Generator", css="static/style.css") as demo: gr.Markdown("#