Spaces:
Sleeping
Sleeping
| import os | |
| import streamlit as st | |
| from groq import Groq | |
| from PIL import Image, ImageDraw, ImageFont | |
| import textwrap | |
| import requests | |
| from io import BytesIO | |
| # Set your GROQ API key from the Hugging Face secret | |
| api_key = os.environ.get("GROQ_API_KEY") | |
| client = Groq(api_key=api_key) | |
| # App UI | |
| st.set_page_config(page_title="AI Meme Generator", layout="centered") | |
| st.title("π AI Meme Generator") | |
| st.caption("Create hilarious memes from your text prompts!") | |
| # User input | |
| user_input = st.text_input("Describe a meme idea in any language", "") | |
| if st.button("Generate Meme") and user_input.strip(): | |
| with st.spinner("Thinking like a meme lord..."): | |
| chat_completion = client.chat.completions.create( | |
| messages=[ | |
| {"role": "user", "content": f"Create a funny meme caption and suggest an image idea for: {user_input}"} | |
| ], | |
| model="llama-3.3-70b-versatile" | |
| ) | |
| response = chat_completion.choices[0].message.content | |
| lines = response.split("\n") | |
| meme_text = lines[0].strip() | |
| image_prompt = " ".join(lines[1:]).strip() or "funny cartoon character" | |
| image_url = "https://i.imgflip.com/30b1gx.jpg" | |
| image = Image.open(BytesIO(requests.get(image_url).content)) | |
| draw = ImageDraw.Draw(image) | |
| font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" | |
| font = ImageFont.truetype(font_path, size=26) | |
| wrapped = textwrap.fill(meme_text, width=30) | |
| bbox = draw.multiline_textbbox((0, 0), wrapped, font=font) | |
| text_w = bbox[2] - bbox[0] | |
| text_h = bbox[3] - bbox[1] | |
| x = (image.width - text_w) / 2 | |
| y = 10 | |
| draw.multiline_text((x, y), wrapped, fill="white", font=font, align="center", stroke_width=2, stroke_fill="black") | |
| st.image(image, caption="Your AI-generated Meme", use_column_width=True) | |
| st.success("π Meme Generated!") | |
| elif user_input.strip() == "": | |
| st.info("Enter a funny idea to generate a meme.") | |