Spaces:
Configuration error
Configuration error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
import re
|
4 |
+
import random
|
5 |
+
from openai import OpenAI
|
6 |
+
import fal_client
|
7 |
+
from pydantic import BaseModel
|
8 |
+
import os
|
9 |
+
from huggingface_hub import HfApi
|
10 |
+
|
11 |
+
# Environment variables from Hugging Face Secrets
|
12 |
+
api_key = os.environ.get('API_KEY')
|
13 |
+
api_base = os.environ.get('API_BASE')
|
14 |
+
FAL_KEY = os.environ.get('FAL_KEY')
|
15 |
+
|
16 |
+
# Initialize OpenAI client
|
17 |
+
client = OpenAI(
|
18 |
+
api_key=api_key,
|
19 |
+
base_url=api_base
|
20 |
+
)
|
21 |
+
|
22 |
+
model = "google/gemini-flash-1.5"
|
23 |
+
|
24 |
+
class MemeRequest(BaseModel):
|
25 |
+
domain: str
|
26 |
+
|
27 |
+
class MemeResponse(BaseModel):
|
28 |
+
image_url: str
|
29 |
+
top_text: str
|
30 |
+
bottom_text: str
|
31 |
+
|
32 |
+
def generate_meme(domain: str) -> MemeResponse:
|
33 |
+
"""
|
34 |
+
Generates a meme based on the given domain.
|
35 |
+
"""
|
36 |
+
temperature = round(random.uniform(0.5, 0.9), 2)
|
37 |
+
print(f"Temperature: {temperature}")
|
38 |
+
|
39 |
+
user_content = (f"Act like Non offensive meme maker. Always create different and unique funny memes "
|
40 |
+
f"always remember stable diffusion cannot render text natively hence, write prompt just detailing the scene or image without text "
|
41 |
+
f"Return meme details in below json format for topic {domain}\n\n"
|
42 |
+
"{ \"stableDiffusionPrompt\": \" \", \"topText\": \"\", \"bottomText\": \"\" }\n\n"
|
43 |
+
"Strictly just reply json no extra text")
|
44 |
+
|
45 |
+
try:
|
46 |
+
chat_completion = client.chat.completions.create(
|
47 |
+
messages=[
|
48 |
+
{"role": "system", "content": "You are a helpful meme maker, who makes non offensive memes(Dont include Cat in memes)"},
|
49 |
+
{"role": "user", "content": user_content}
|
50 |
+
],
|
51 |
+
model=model,
|
52 |
+
temperature=temperature
|
53 |
+
)
|
54 |
+
|
55 |
+
result = chat_completion.choices[0].message.content
|
56 |
+
|
57 |
+
try:
|
58 |
+
meme_data = json.loads(result)
|
59 |
+
except json.JSONDecodeError:
|
60 |
+
json_match = re.search(r'```json\s*(.*?)\s*```', result, re.DOTALL)
|
61 |
+
if json_match:
|
62 |
+
json_string = json_match.group(1)
|
63 |
+
meme_data = json.loads(json_string)
|
64 |
+
else:
|
65 |
+
raise ValueError("No valid JSON found in the response")
|
66 |
+
|
67 |
+
print("Stable Diffusion Prompt:", meme_data['stableDiffusionPrompt'])
|
68 |
+
print("Top Text:", meme_data['topText'])
|
69 |
+
print("Bottom Text:", meme_data['bottomText'])
|
70 |
+
|
71 |
+
# Configure fal_client with API key
|
72 |
+
fal_client.api_key = FAL_KEY
|
73 |
+
|
74 |
+
handler = fal_client.submit(
|
75 |
+
"fal-ai/flux/schnell",
|
76 |
+
arguments={
|
77 |
+
"prompt": meme_data['stableDiffusionPrompt'],
|
78 |
+
"image_size": "landscape_4_3",
|
79 |
+
"num_inference_steps": 4,
|
80 |
+
"num_images": 1,
|
81 |
+
"enable_safety_checker": True
|
82 |
+
}
|
83 |
+
)
|
84 |
+
|
85 |
+
result = handler.get()
|
86 |
+
image_url = result['images'][0]['url']
|
87 |
+
print("Generated Image URL:", image_url)
|
88 |
+
|
89 |
+
return MemeResponse(
|
90 |
+
top_text=meme_data['topText'],
|
91 |
+
image_url=image_url,
|
92 |
+
bottom_text=meme_data['bottomText']
|
93 |
+
)
|
94 |
+
|
95 |
+
except Exception as e:
|
96 |
+
print(f"Error generating meme: {str(e)}")
|
97 |
+
raise
|
98 |
+
|
99 |
+
def generate_meme_gradio(domain):
|
100 |
+
"""
|
101 |
+
Wrapper function for Gradio interface.
|
102 |
+
"""
|
103 |
+
try:
|
104 |
+
meme_response = generate_meme(domain)
|
105 |
+
return meme_response.top_text, meme_response.image_url, meme_response.bottom_text
|
106 |
+
except Exception as e:
|
107 |
+
return "Error", None, f"Failed to generate meme: {str(e)}"
|
108 |
+
|
109 |
+
# Example domains for the interface
|
110 |
+
example_domains = [
|
111 |
+
'HR', 'Technology', 'AI', 'Machine Learning', 'Sales', 'Marketing',
|
112 |
+
'Remote Work', 'Coffee', 'Monday', 'Deadlines', 'Office Pranks', 'Tech Support',
|
113 |
+
'Social Media', 'Startup Life', 'Zoom Fails', 'Work-Life Balance', 'Coding',
|
114 |
+
'Data Privacy', 'Cybersecurity', 'Cloud Computing', 'Blockchain', 'IoT',
|
115 |
+
'Virtual Reality', 'Augmented Reality', 'Quantum Computing', '5G', 'AI Ethics'
|
116 |
+
]
|
117 |
+
|
118 |
+
# Create Gradio interface
|
119 |
+
demo = gr.Interface(
|
120 |
+
fn=generate_meme_gradio,
|
121 |
+
inputs=[
|
122 |
+
gr.Textbox(
|
123 |
+
label="Enter your meme topic",
|
124 |
+
placeholder="Type a topic or choose from examples below",
|
125 |
+
info="Try topics like 'AI', 'Remote Work', or 'Tech Support'"
|
126 |
+
)
|
127 |
+
],
|
128 |
+
outputs=[
|
129 |
+
gr.Textbox(label="Top Text"),
|
130 |
+
gr.Image(label="Generated Meme"),
|
131 |
+
gr.Textbox(label="Bottom Text")
|
132 |
+
],
|
133 |
+
theme=gr.themes.Ocean(),
|
134 |
+
title="🎨 AI Meme Factory",
|
135 |
+
description="""
|
136 |
+
Welcome to the AI Meme Factory! World's First Generative meme Engine.
|
137 |
+
Generate unique, funny, and non-offensive memes using the power of AI.
|
138 |
+
|
139 |
+
🤖 Powered by:
|
140 |
+
- Gemini for creative text generation
|
141 |
+
- Flux for high-quality image generation
|
142 |
+
|
143 |
+
✨ Features:
|
144 |
+
- Instant meme generation
|
145 |
+
- Professional and work-appropriate content
|
146 |
+
- Wide range of topics supported
|
147 |
+
""",
|
148 |
+
examples=[[domain] for domain in random.sample(example_domains, 6)],
|
149 |
+
allow_flagging="never",
|
150 |
+
cache_examples=False
|
151 |
+
)
|
152 |
+
|
153 |
+
# Launch the app
|
154 |
+
if __name__ == "__main__":
|
155 |
+
demo.launch(debug=True)
|