Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import numpy as np | |
| from sentence_transformers import SentenceTransformer | |
| # Load your model | |
| model = SentenceTransformer("PietroSaveri/meme-cluster-classifier-02") | |
| # Define seed texts (example only — fill in your real seeds) | |
| # Define a few seed texts per category | |
| seed_texts = { | |
| 'Conspiracy': [ | |
| "following the science i am trying to follow the science but it keeps leading me back to the money", | |
| "scrolling through the pictures on my phone and thought id share this here. anybody want to talk chemtrails with me? o.0", | |
| "experts baffled fact check no evidence covid19 vaccines cause cancer a global epidemic of cancer among people younger than 50 could be emerging", | |
| "this is your brain on vax xray of the brain after covid vaccine", | |
| "lets stop climate change! lets stop climate change! no, not you no, not you no, not you no, not you no, not you you must stop it now", | |
| "science keeps changing its story, but when i question it, i'm labeled a conspiracy theorist", | |
| "they are hiding the cure for cancer because it makes more money to keep people sick", | |
| "do your own research. the media won't tell you the truth about 5g and brainwaves", | |
| "the moon landing was faked to beat russia. open your eyes!", | |
| "climate change is just a hoax to control how we live. it's all about control", | |
| "they inject microchips with vaccines to track us. it’s not even hidden anymore" | |
| ], | |
| 'Wordplay & Nerd Humor': [ | |
| "no bromo noo! you cant just pour halogenated solvents down the sink! haha halogens go br", | |
| "pov youre a chemistry student, and you know what topic you are studying when you come across this mfs. cooh hcch oh hooc hcch oh", | |
| "the 1 elorated physicist is isaac newton not sure if maxwell should be 3 though. heres the link if you want to vote", | |
| "inverse square law i am the universal law of gravitation f g mm r i am coulombs law f k qq r you look identical to me? we follow inverse square law", | |
| "it works benzene in any normal reaction with br2 or any halogens benzene in a reaction with albr3 and br2", | |
| "i told a chemistry joke. no reaction.", | |
| "your puns have low entropy, try increasing the disorder", | |
| "physics teacher: 'i’ll give you a moment to think about it' me: *calculates impulse*", | |
| "why did the biologist go broke? because they lost their cells at the stock exchange" | |
| ], | |
| 'Educational Science Humor': [ | |
| "they are now even numbers odd numbers when they are multiplied by 2 were even now.", | |
| "the science is in very warm warm moderate mild cool cold very cold freezing 27.0c 29.9c 23.0c 26.9c 18.0c 22.9c 14.0c 17.9c all you homies who found this meme 6.0c 9.9c 0c 5.9c 0c", | |
| "only science freaks understand. normal people with a time machine i am your granddaughter really? legends with a time machine nice party, mr. hawking.", | |
| "is oc science good? 602.3 sextillion or 6.02310sup23sup of avocados avocado number a guaca mole", | |
| "no more sleeping scientists teenagers need nine and a half hours of sleep... education system", | |
| "a photon checks into a hotel. the clerk asks if he has any luggage. the photon says: no, i'm traveling light.", | |
| "mitochondria is the powerhouse of the cell. if you know, you know.", | |
| "entropy: the reason your room gets messy without you doing anything", | |
| "when you finally understand mitosis and you're like 'i see what you did there'" | |
| ] | |
| } | |
| # Compute centroids | |
| seed_centroids = { | |
| cat: model.encode(texts, convert_to_numpy=True).mean(axis=0) | |
| for cat, texts in seed_texts.items() | |
| } | |
| # Cosine similarity helper | |
| def cosine_sim(a, b): | |
| return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) | |
| # Prediction logic | |
| def predict(text): | |
| vec = model.encode(text, convert_to_numpy=True) | |
| sims = {cat: cosine_sim(vec, centroid) for cat, centroid in seed_centroids.items()} | |
| sorted_sims = sorted(sims.items(), key=lambda x: x[1], reverse=True) | |
| top_category = sorted_sims[0][0] | |
| return f"Predicted category: **{top_category}**", {k: round(v, 3) for k, v in sorted_sims} | |
| # Gradio UI | |
| demo = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Textbox(label="Enter a meme or science joke"), | |
| outputs=[ | |
| gr.Markdown(label="Prediction"), | |
| gr.JSON(label="Similarity scores by category") | |
| ], | |
| title="Meme Cluster Classifier", | |
| description="A model that clusters science memes or jokes into Conspiracy, Nerd Humor, or Educational using sentence embeddings and cosine similarity." | |
| ) | |
| demo.launch() | |