Spaces:
Running
Running
File size: 5,721 Bytes
09db2c8 2f7715b 09db2c8 2f7715b 09db2c8 477193a 2f7715b 09db2c8 0ba4378 09db2c8 2666885 09db2c8 |
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 |
import gradio as gr
import json
import re
import random
from openai import OpenAI
import fal_client
from pydantic import BaseModel
import os
from huggingface_hub import HfApi
# Environment variables from Hugging Face Secrets
api_key = os.environ.get('API_KEY')
api_base = os.environ.get('API_BASE')
FAL_KEY = os.environ.get('FAL_KEY')
# Initialize OpenAI client
client = OpenAI(
api_key=api_key,
base_url=api_base
)
model = "google/gemini-flash-1.5"
class MemeRequest(BaseModel):
domain: str
class MemeResponse(BaseModel):
image_url: str
top_text: str
bottom_text: str
#######################################################################
from datetime import datetime, timedelta
def get_ist_time():
utc_time = datetime.utcnow()
ist_time = utc_time + timedelta(hours=5, minutes=30)
return ist_time.strftime("%d-%m-%Y %H:%M:%S IST")
#########################################################################
def generate_meme(domain: str) -> MemeResponse:
"""
Generates a meme based on the given domain.
"""
temperature = round(random.uniform(0.5, 0.9), 2)
print(f"Temperature: {temperature}")
user_content = (f"Act like Non offensive meme maker. Always create different and unique funny memes "
f"always remember stable diffusion cannot render text natively hence, write prompt just detailing the scene or image without text "
f"Return meme details in below json format for topic {domain}\n\n"
"{ \"stableDiffusionPrompt\": \" \", \"topText\": \"\", \"bottomText\": \"\" }\n\n"
"Strictly just reply json no extra text")
try:
chat_completion = client.chat.completions.create(
messages=[
{"role": "system", "content": "You are a helpful meme maker, who makes non offensive memes(Dont include Cat in memes)"},
{"role": "user", "content": user_content}
],
model=model,
temperature=temperature
)
result = chat_completion.choices[0].message.content
try:
meme_data = json.loads(result)
except json.JSONDecodeError:
json_match = re.search(r'```json\s*(.*?)\s*```', result, re.DOTALL)
if json_match:
json_string = json_match.group(1)
meme_data = json.loads(json_string)
else:
raise ValueError("No valid JSON found in the response")
print(" Meme subject :",domain)
print("Meme Generated on ", get_ist_time())
print("Stable Diffusion Prompt:", meme_data['stableDiffusionPrompt'])
print("Top Text:", meme_data['topText'])
print("Bottom Text:", meme_data['bottomText'])
# Configure fal_client with API key
fal_client.api_key = FAL_KEY
handler = fal_client.submit(
"fal-ai/flux/schnell",
arguments={
"prompt": meme_data['stableDiffusionPrompt'],
"image_size": "landscape_4_3",
"num_inference_steps": 4,
"num_images": 1,
"enable_safety_checker": True
}
)
result = handler.get()
image_url = result['images'][0]['url']
print("Generated Image URL:", image_url)
return MemeResponse(
top_text=meme_data['topText'],
image_url=image_url,
bottom_text=meme_data['bottomText']
)
except Exception as e:
print(f"Error generating meme: {str(e)}")
raise
def generate_meme_gradio(domain):
"""
Wrapper function for Gradio interface.
"""
try:
meme_response = generate_meme(domain)
return meme_response.top_text, meme_response.image_url, meme_response.bottom_text
except Exception as e:
return "Error", None, f"Failed to generate meme: {str(e)}"
# Example domains for the interface
example_domains = [
'HR', 'Technology', 'AI', 'Machine Learning', 'Sales', 'Marketing',
'Remote Work', 'Coffee', 'Monday', 'Deadlines', 'Office Pranks', 'Tech Support',
'Social Media', 'Startup Life', 'Zoom Fails', 'Work-Life Balance', 'Coding',
'Data Privacy', 'Cybersecurity', 'Cloud Computing', 'Blockchain', 'IoT',
'Virtual Reality', 'Augmented Reality', 'Quantum Computing', '5G', 'AI Ethics'
]
# Create Gradio interface
demo = gr.Interface(
fn=generate_meme_gradio,
inputs=[
gr.Textbox(
label="Enter your meme topic",
value="Human Resource team",
placeholder="Type a topic or choose from examples below",
info="Try topics like 'AI', 'Remote Work', or 'Tech Support'"
)
],
outputs=[
gr.Textbox(label="Top Text"),
gr.Image(label="Generated Meme"),
gr.Textbox(label="Bottom Text")
],
theme=gr.themes.Ocean(),
title="🎨 AI Meme Factory",
description="""
Welcome to the AI Meme Factory! World's First Generative meme Engine.
Generate unique, funny, and non-offensive memes using the power of AI.
🤖 Powered by:
- Gemini for creative text generation
- Flux for high-quality image generation
✨ Features:
- Instant meme generation
- Professional and work-appropriate content
- Wide range of topics supported
Build with ❤️ by XHaheen | <a href='https://www.linkedin.com/in/sallu-mandya/' target='_blank'>Connect with me on LinkedIn to scale App</a>
""",
examples=[[domain] for domain in random.sample(example_domains, 6)],
allow_flagging="never",
cache_examples=False
)
# Launch the app
if __name__ == "__main__":
demo.launch(debug=True) |