File size: 5,303 Bytes
68ccda2 c2c28b1 68ccda2 0fb48e1 b7b771f 68ccda2 c2c28b1 68ccda2 3eee361 c2c28b1 68ccda2 |
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 |
import os
import time
import json
import gradio as gr
import requests
from groq import Groq
from dotenv import load_dotenv
from io import BytesIO
from PIL import Image
load_dotenv()
# Setup clients and credentials
IMGFLIP_USERNAME = os.getenv("IMGFLIP_USERNAME")
IMGFLIP_PASSWORD = os.getenv("IMGFLIP_PASSWORD")
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
# Load memes from Imgflip API
def list_memes():
# r = requests.get("https://api.imgflip.com/get_memes")
# data = r.json()
# if not data["success"]:
# return {}
data = json.loads(open("./metadata.json","r").read())
return {meme["name"]: {"id": meme["id"], "url": meme["url"]} for meme in data["data"]["memes"]}
MEME_DATABASE = list_memes()
# Llama-4-Scout selects the meme template + captions
def pick_meme_and_params(prompt: str) -> dict:
meme_names = list(MEME_DATABASE.keys())
meme_list_string = "\n".join(f"- {name}" for name in meme_names)
system_message = "You are a JSON-output assistant that selects the best meme template and generates top and bottom captions. Make them funny and creative"
user_message = (
f"Here is the list of available meme templates:\n{meme_list_string}\n\n"
f"Based on this list, choose the most suitable meme template for the prompt: '{prompt}'.\n\n"
f"Return your answer in this JSON format:\n"
"{\n"
"\"template_name\": \"template name from the list\",\n"
"\"top_text\": \"top caption\",\n"
"\"bottom_text\": \"bottom caption\",\n"
"\"font\": \"optional font (default to impact)\",\n"
"\"max_font_size\": integer between 10 and 100 (optional)\n"
"}"
"Only return JSON, not extra information, no delimiter, only JSON"
)
print("preresponse")
response = client.chat.completions.create(
# model="meta-llama/llama-4-scout-17b-16e-instruct",
model="llama3-70b-8192",
messages=[{"role": "system", "content": system_message}, {"role": "user", "content": user_message}],
# response_format="json"
)
# print(response.choices[0].message.content)
json_response = json.loads(response.choices[0].message.content)
return json_response
# Call Imgflip API to generate the meme
# def make_meme(config: dict) -> str:
# template = MEME_DATABASE.get(config["template_name"])
# if not template:
# return f"Error: Meme template '{config['template_name']}' not found."
# payload = {
# "template_id": template["id"],
# "username": IMGFLIP_USERNAME,
# "password": IMGFLIP_PASSWORD,
# "text0": config["top_text"],
# "text1": config["bottom_text"]
# }
# if config.get("font"):
# payload["font"] = config["font"]
# if config.get("max_font_size"):
# payload["max_font_size"] = config["max_font_size"]
# r = requests.post("https://api.imgflip.com/caption_image", data=payload)
# d = r.json()
# return d["data"]["url"] if d.get("success") else f"Error: {d.get('error_message')}"
def make_meme(config: dict):
template = MEME_DATABASE.get(config["template_name"])
if not template:
raise ValueError(f"Meme template '{config['template_name']}' not found.")
payload = {
"template_id": template["id"],
"username": IMGFLIP_USERNAME,
"password": IMGFLIP_PASSWORD,
"text0": config["top_text"],
"text1": config["bottom_text"]
}
if config.get("font"):
payload["font"] = config["font"]
if config.get("max_font_size"):
payload["max_font_size"] = config["max_font_size"]
r = requests.post("https://api.imgflip.com/caption_image", data=payload)
d = r.json()
print(d)
# with open("response.json","w") as f:
# json.dump(d,f)
if not d.get("success"):
raise ValueError(f"Error from Imgflip API: {d.get('error_message')}")
meme_url = d["data"]["url"]
# Fetch the generated image from the returned URL
img_response = requests.get(meme_url)
if img_response.status_code != 200:
raise ValueError("Failed to fetch the generated meme image.")
# Convert image bytes to PIL image for Gradio
image = Image.open(BytesIO(img_response.content))
image.save("./img.png")
return image
# Full meme generation pipeline
def generate_meme(prompt):
try:
config = pick_meme_and_params(prompt)
print(type(config))
print(config)
return make_meme(config)
except Exception as e:
return f"Error generating meme: {e}"
# Gradio + MCP interface
with gr.Blocks() as demo:
gr.Markdown("# 🖼️ Meme Context Protocol Tool")
gr.Markdown("""
**Your personal meme genie!**
Drop a prompt, and this meme conjurer uses magic to pick the perfect template and cook up hilarious captions faster than you can say "dank."
It’s the ultimate meme-making mischief machine – pun-powered, LLM-fueled, and chaos-approved.
Memes so fresh, they might just slap themselves.
""")
prompt_in = gr.Textbox(label="Enter your meme idea")
out_img = gr.Image(label="Generated Meme")
btn = gr.Button("Generate Meme")
btn.click(fn=generate_meme, inputs=prompt_in, outputs=out_img)
if __name__ == "__main__":
demo.launch(mcp_server=True)
|