File size: 2,184 Bytes
68f4347
6358b7f
 
 
 
 
 
 
d42f81f
 
721f0f3
d42f81f
68f4347
f8d8e0f
70e3b26
f8d8e0f
68f4347
70e3b26
 
d42f81f
68f4347
6358b7f
d42f81f
68f4347
6358b7f
 
 
721f0f3
 
 
bf2ed0a
f8d8e0f
 
 
 
 
 
 
721f0f3
f8d8e0f
 
68f4347
70e3b26
 
f8d8e0f
70e3b26
38f5808
 
 
 
 
 
 
 
 
 
f8d8e0f
68f4347
f8d8e0f
 
 
 
 
 
 
6197d6a
f8d8e0f
6358b7f
f8d8e0f
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
# apne imp libraries
from sentence_transformers import SentenceTransformer, util
from huggingface_hub import hf_hub_download
import pickle
import pandas as pd
from PIL import Image
import requests
from io import BytesIO
import gradio as gr

pd.options.mode.chained_assignment = None

# embeddings load kiye dataset repo se 
embeddings = pickle.load(open(
    hf_hub_download("Go-Raw/semantic-memes", repo_type="dataset", filename="meme-embeddings.pkl"), "rb"))

# apne meme ka metadata load kiya
df = pd.read_csv(
    hf_hub_download("Go-Raw/semantic-memes", repo_type="dataset", filename="semantic_memes.csv"))

# ye apna model hai
model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')

# iss func se meme search hota hai
def generate_memes(prompt):
    prompt_embedding = model.encode(prompt, convert_to_tensor=True)
    hits = util.semantic_search(prompt_embedding, embeddings, top_k=6)
    hits_df = pd.DataFrame(hits[0], columns=["corpus_id", "score"])
    matched_ids = hits_df["corpus_id"]
    matched_memes = df[df["id"].isin(matched_ids)]

    images = []
    for url in matched_memes["url"]:
        try:
            response = requests.get(url)
            image = Image.open(BytesIO(response.content))
            images.append(image)
        except Exception as e:
            print(f"Error loading image {url}: {e}")
    return images

# Gradio ka UI
input_textbox = gr.Textbox(lines=1, label="Type your vibe here 🧠")
output_gallery = gr.Gallery(label="Your Meme Results", columns=3, rows=2, height="auto")

title = "🧠 Meme Lord"
description = (
    "Search memes from a diverse collection using sentence-level semantic similarity. "
    "Built with Sentence Transformers and hosted on Hugging Face. "
    "[Dataset](https://huggingface.co/datasets/Go-Raw/semantic-memes)"
)
examples = [
    "When you realize it's Monday again",
    "Internet explorer in 2024",
    "This meeting could’ve been an email"
]

# app launch karne ke liye
iface = gr.Interface(
    fn=generate_memes,
    inputs=input_textbox,
    outputs=output_gallery,
    examples=examples,
    cache_examples=True,
    title=title,
    description=description
)

iface.launch()