""" Magic 8-Ball — The Wonder You Stopped Seeing. Shake a toy. See what you stopped seeing. """ import gradio as gr import os import random from huggingface_hub import InferenceClient HF_TOKEN = os.environ.get("HF_TOKEN", "") MODEL = "mistralai/Mistral-7B-Instruct-v0.3" client = InferenceClient(model=MODEL, token=HF_TOKEN) if HF_TOKEN else None SYSTEM_PROMPT = """You are the Magic 8-Ball. You don't predict the future. You reveal the present. Someone names something ordinary — something they see every day and stopped noticing. Your job is to show them the wonder hiding in it. The thing they walk past a thousand times without seeing. Rules: - 3 to 4 sentences. No more. - Talk like a friend pointing something out, not a professor giving a lecture. - No jargon. No "did you know." No scientific terminology. - Don't explain how something works. Show why it's extraordinary. - Be warm but not sappy. Be specific but not technical. - Never start with "Did you know" or "Actually" or "Fun fact." - Make them see it like it's the first time.""" ORDINARY_THINGS = [ "your kitchen sponge", "the tree outside your window", "your shoelaces", "dust floating in sunlight", "the hum of your refrigerator", "a spiderweb in the corner", "your fingerprint", "the smell after rain", "a puddle on the sidewalk", "your shadow", "a dandelion in a crack", "the steam from your coffee", "the moon during the day", "an old penny", "the sound of wind", "a bird on a power line", "your breath on a cold morning", "the grain in a wooden table", "a cloud", "the static on a blanket at night", "a doorknob you touch every day", "the color of rust", "an ant carrying something", "the pattern in your ceiling", "a crack in the sidewalk", "the sound of your own heartbeat", "a glass of water", "the dirt under your fingernails", "a stain on your shirt", "the way paint peels", ] FALLBACKS = [ "**your kitchen sponge**\n\nThat sponge has more living organisms in it than there are people in your city. Every time you wring it out, you're deciding who survives. You've been playing god over the sink every night after dinner and never once thought about it.", "**the tree outside your window**\n\nIt's been standing there longer than you've lived in that house. It watched the previous tenants leave. It'll watch you leave too. And it'll still be pulling water from underground like it's nothing.", "**the steam from your coffee**\n\nEvery curl and twist is a shape that has never existed before and will never exist again. You just watched something unrepeatable happen and called it Tuesday morning.", "**a puddle on the sidewalk**\n\nIt's holding an upside-down copy of the entire sky. Every cloud, every bird, every plane — all of it, in a few inches of rainwater. And you stepped over it.", "**a dandelion in a crack**\n\nIt broke through concrete to get there. Not around it. Through it. Whatever problem you're working on today, that weed already solved a harder one before sunrise.", "**your shadow**\n\nIt's been following you around since before you could walk. It's the only thing that's been with you every single day of your life, and you haven't looked at it in months. It doesn't need you to notice it. It just shows up.", "**dust floating in sunlight**\n\nThat's not dirt. Most of it is dead skin cells, fabric fibers, and pollen that traveled miles to get into your room. You're watching a tiny cross-section of everything that touched the air today. The sunbeam just made it visible. It was always there.", "**the hum of your refrigerator**\n\nIt's running a thermodynamic cycle that took 200 years of physics to figure out. It's keeping your food cold by moving heat from inside to outside, over and over, all day, and the only time you notice is when it stops. The silence is louder than the hum ever was.", "**your fingerprint**\n\nIt formed before you were born and nothing you do will change it. Every surface you've ever touched has a copy of it. You've been leaving a signature on the world since the day you arrived and you've never once done it on purpose.", "**the sound of wind**\n\nWind doesn't make a sound. It's silent. What you're hearing is everything the wind touches — leaves, buildings, your jacket, the gap under your door. The wind is invisible and quiet. The world is the instrument. You're hearing the planet play itself.", "**a glass of water**\n\nSome of those molecules fell as rain on a field in the Midwest last spring. Some of them were in the Great Lakes when there was a mile of ice on top. Water doesn't get made or destroyed — it just moves. You're drinking history and it doesn't even taste like anything.", "**an ant carrying something**\n\nThat ant is carrying something that weighs 50 times its body weight, navigating by smell, communicating with chemicals, and it will do this exact thing ten thousand more times before it dies without ever once wondering why. It doesn't need a reason. It just carries.", "**my coffee**\n\nThat cup started as a cherry on a tree in Ethiopia or Colombia, picked by someone you'll never meet, dried in sun you'll never feel, roasted in a drum spinning at 450 degrees, ground into dust, and drowned in water that fell as rain last month. Fourteen countries touched that cup before it touched your lips. You called it 'just coffee' and checked your phone.", "**the crack in the sidewalk**\n\nThat crack is the earth winning. Concrete is rigid and the ground beneath it never stops moving — freezing, thawing, shifting, settling. The crack is where the sidewalk finally admitted it couldn't hold. A weed will grow in it by next month. The city will patch it. The ground will crack it again. This argument has been going on longer than the sidewalk has existed and the ground has never lost.", "**my dog sleeping**\n\nYour dog is dreaming. Their paws twitch because the same brain regions that control movement during waking life light up during REM sleep. They're running somewhere you can't follow. A creature that trusts you enough to lose consciousness in your presence, whose entire emotional world begins and ends with whether you're in the room. They don't know they're going to die. They don't know you are either. They just sleep next to you because you're warm and you're theirs.", "**a spiderweb**\n\nThat web is stronger per weight than steel and more elastic than nylon. The spider built it in about an hour using material produced inside its own body, following an engineering blueprint encoded in a brain the size of a poppy seed. If the wind destroys it tonight, the spider will build another one tomorrow. Same design. Same precision. No frustration. The web isn't a home. It's a tool. The spider is a factory that never closes and never complains.", "**my own hands**\n\nYour hands have 27 bones each, more nerve endings per square inch than almost anywhere else on your body, and they're controlled by muscles that are mostly in your forearm, not your hand — your fingers are puppets on strings. They're the reason humans took over the planet. Not the brain. The hands. Every tool, every word ever written, every meal, every embrace — all of it happened because these ten strange appendages can grip, pinch, and feel the difference between silk and sandpaper with their eyes closed.", ] def find_fallback(thing): """Find a fallback that matches the input, or return random.""" if thing: thing_lower = thing.lower() for fb in FALLBACKS: # Check if the fallback's bold topic contains the input if thing_lower in fb.lower().split("\n")[0]: return fb return random.choice(FALLBACKS) def shake(user_input): thing = user_input.strip() if user_input and user_input.strip() else "" if client and thing: try: response = client.chat_completion( messages=[ {"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": f"The ordinary thing: {thing}"}, ], max_tokens=300, temperature=0.85, ) result = response.choices[0].message.content if result and result.strip(): return f"**{thing}**\n\n{result.strip()}" except Exception: pass return find_fallback(thing) def shake_random(): return shake("") CUSTOM_CSS = """ @import url('https://fonts.googleapis.com/css2?family=Space+Mono:wght@400;700&family=Inter:wght@300;400;500;600&display=swap'); body, .gradio-container { background: #f5f5f8 !important; font-family: 'Inter', sans-serif !important; color: #1a1a2e !important; } footer { display: none !important; } .ball-header { text-align: center; padding: 28px 20px 8px; } .ball-header h1 { font-family: 'Space Mono', monospace; font-size: 2.4rem; font-weight: 700; background: linear-gradient(135deg, #4a6cf7, #00f0ff); -webkit-background-clip: text; -webkit-text-fill-color: transparent; margin: 0; } .ball-header .sub { color: #777777; font-size: 0.88rem; margin-top: 6px; font-weight: 300; letter-spacing: 0.04em; } .ball-visual { text-align: center; font-size: 6rem; padding: 12px 0 4px; filter: drop-shadow(0 0 30px rgba(74, 108, 247, 0.3)); } /* Input styling */ .input-box textarea { background: #ffffff !important; border: 1px solid #d0d0d8 !important; color: #1a1a2e !important; font-family: 'Inter', sans-serif !important; border-radius: 12px !important; font-size: 0.95rem !important; } .input-box textarea::placeholder { color: #888888 !important; } .input-box textarea:focus { border-color: #4a6cf7 !important; box-shadow: 0 0 20px rgba(74, 108, 247, 0.15) !important; } /* Labels */ .input-box label, .output-box label { color: #666666 !important; font-family: 'Space Mono', monospace !important; font-size: 0.75rem !important; letter-spacing: 0.05em !important; } /* Button */ button.primary { background: linear-gradient(135deg, #4a6cf7, #7b5ea7) !important; border: none !important; color: #fff !important; font-family: 'Space Mono', monospace !important; font-size: 1.1rem !important; font-weight: 700 !important; border-radius: 24px !important; padding: 12px 40px !important; box-shadow: 0 4px 20px rgba(74, 108, 247, 0.3) !important; letter-spacing: 0.03em !important; } button.primary:hover { box-shadow: 0 4px 30px rgba(74, 108, 247, 0.5) !important; transform: translateY(-1px); } button.secondary { background: transparent !important; border: 1px solid rgba(74, 108, 247, 0.3) !important; color: #4a6cf7 !important; font-family: 'Space Mono', monospace !important; font-size: 0.8rem !important; border-radius: 20px !important; letter-spacing: 0.05em !important; } button.secondary:hover { background: rgba(74, 108, 247, 0.1) !important; } /* Output */ .output-box .prose, .output-box .prose *, .output-box .md, .output-box .md *, .output-box p, .output-box span, .output-box div { background: #ffffff !important; border-radius: 12px !important; color: #1a1a2e !important; font-size: 0.95rem !important; line-height: 1.7 !important; } .output-box .prose strong, .output-box strong, .output-box .md strong { color: #00f0ff !important; font-family: 'Space Mono', monospace !important; } .output-box > div { border: 1px solid #e0e0e8 !important; border-radius: 12px !important; padding: 24px !important; background: #ffffff !important; } .footer-text { text-align: center; padding: 20px; color: #777777; font-size: 0.65rem; font-weight: 300; letter-spacing: 0.05em; } .footer-text a { color: #4a6cf7; text-decoration: none; } """ with gr.Blocks(css=CUSTOM_CSS, title="Magic 8-Ball", theme=gr.themes.Base()) as demo: gr.HTML("""

Magic 8-Ball

The wonder you stopped seeing
""") with gr.Column(elem_classes="input-box"): user_input = gr.Textbox( label="What can you see right now?", placeholder="your coffee, the rain outside, a crack in the wall... or leave blank and shake", lines=1, max_lines=1, ) shake_btn = gr.Button("Shake", variant="primary", size="lg") with gr.Column(elem_classes="output-box"): output = gr.Markdown(label="") gr.Markdown("**Or try one of these:**", elem_classes="suggestions-label") with gr.Row(): ex1 = gr.Button("my coffee", variant="secondary", size="sm") ex2 = gr.Button("the crack in the sidewalk", variant="secondary", size="sm") ex3 = gr.Button("my dog sleeping", variant="secondary", size="sm") with gr.Row(): ex4 = gr.Button("the rain outside", variant="secondary", size="sm") ex5 = gr.Button("a spiderweb", variant="secondary", size="sm") ex6 = gr.Button("surprise me", variant="secondary", size="sm") shake_btn.click(shake, [user_input], [output]) ex1.click(lambda: shake("my coffee"), [], [output]) ex2.click(lambda: shake("the crack in the sidewalk"), [], [output]) ex3.click(lambda: shake("my dog sleeping"), [], [output]) ex4.click(lambda: shake("the rain outside"), [], [output]) ex5.click(lambda: shake("a spiderweb"), [], [output]) ex6.click(shake_random, [], [output]) gr.HTML(""" """) if __name__ == "__main__": demo.launch()