File size: 2,008 Bytes
271887a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.")