import gradio as gr import json import os import random # ------------------------- # Quiz data # ------------------------- quiz_questions = [ { "question": "In our study, we applied a magnetic encapsulation technique. What primary physical phenomenon does this technique aim to suppress?", "options": ["Taylor-Couette Instability", "Kelvin-Helmholtz Instability", "Rayleigh–Plateau Instability (RPI)", "Marangoni Effect"], "answer": "Rayleigh–Plateau Instability (RPI)", "hint": "Think about the instability that makes a stream of falling water break into droplets.", "explanation": "✅ Correct! The Rayleigh–Plateau Instability causes liquid jets to break into droplets. Magnetic encapsulation stabilizes the flow, preventing this breakup." }, { "question": "According to the study's stability analysis, what condition must the magnetic Bond number (Bom) satisfy to keep the liquid-liquid interface stable?", "options": ["Bom ≈ 0", "Bom < 1", "Bom = 1", "Bom > 1"], "answer": "Bom > 1", "hint": "Stability is achieved when stabilizing magnetic forces overcome destabilizing surface tension forces.", "explanation": "✅ Correct! The interface remains stable when magnetic forces are stronger than surface tension, so Bom > 1." }, { "question": "The study shows that a stable flow can be switched to an unstable one using an external trigger. How is this achieved?", "options": ["By locally heating the ferrofluid with light irradiation", "By rapidly increasing the flow rate of the inner liquid", "By adding a surfactant to the inner glycerol liquid", "By turning off the external magnets completely"], "answer": "By locally heating the ferrofluid with light irradiation", "hint": "The instability occurs when the stabilizing magnetic force is weakened. Think about a property of the ferrofluid that can be externally altered.", "explanation": "✅ Correct! Local heating of the ferrofluid reduces its magnetic susceptibility, weakening the stabilizing magnetic force and triggering instability." }, { "question": "For the liquid-liquid interface to remain stable, what must be true about the growth rate of perturbations, τ(k), for all perturbation wave numbers k?", "options": ["τ(k) must be negative", "τ(k) must be positive", "τ(k) must be a complex number", "τ(k) must be equal to zero"], "answer": "τ(k) must be negative", "hint": "Consider whether a small ripple on the interface should grow or shrink for the jet to remain stable.", "explanation": "✅ Correct! A negative growth rate means any small perturbation decays over time, keeping the interface stable." }, { "question": "What is a significant advantage of the magnetic confinement method over conventional techniques like flow focusing, especially at very low flow rates?", "options": ["It requires a continuous and high-velocity flow of the outer sheath liquid", "It can only function at extremely high Reynolds numbers", "It completely eliminates the dripping failure mode at static or near-static conditions", "It is only stable for a very short axial length"], "answer": "It completely eliminates the dripping failure mode at static or near-static conditions", "hint": "Think about what happens to a dripping tap when the flow is extremely slow.", "explanation": "✅ Correct! Magnetic confinement stabilizes the liquid-liquid interface even at near-static conditions, preventing the usual dripping failure seen in conventional flow focusing." } ] # ------------------------- # Visitor counter # ------------------------- COUNTER_FILE = "/tmp/counters.json" # Works both locally and in Hugging Face def load_counters(): if os.path.exists(COUNTER_FILE): with open(COUNTER_FILE, "r") as f: try: return json.load(f) except json.JSONDecodeError: return {"visitors": 0} else: counters = {"visitors": 0} with open(COUNTER_FILE, "w") as f: json.dump(counters, f) return counters def save_counters(counters): with open(COUNTER_FILE, "w") as f: json.dump(counters, f) # Increment visitor count on each new user session session_id = str(random.randint(100000, 999999)) counters = load_counters() # To ensure it increments once per user, simulate a new visitor each reload if session_id not in counters: counters["visitors"] += 1 counters[session_id] = True save_counters(counters) # ------------------------- # Score tracking # ------------------------- score = [0] def check_answer(question_index, selected_option): question = quiz_questions[question_index] if selected_option == question["answer"]: score[0] += 1 feedback = f"✅ Correct!\n\n{question['explanation']}" else: feedback = f"❌ Incorrect.\n\n{question['explanation']}" if question_index == len(quiz_questions) - 1: feedback += f"\n\n🎯 Your total score: {score[0]}/{len(quiz_questions)}" feedback += "\n\n📘 Reference: A. A. Dev, G. Bagheri, E. Bodenschatz, T. M. Hermans, and B. Doudin, “Suppression of Interfacial Instability of Immiscible Liquid-in-Liquid Flow Using Magnetic Forces.” *Adv. Sci.* (2025): e10327. https://doi.org/10.1002/advs.202510327" return feedback def show_hint(question_index): return quiz_questions[question_index]["hint"] # ------------------------- # Build interface # ------------------------- def create_question_block(i, q): with gr.Blocks() as block: gr.Markdown(f"### Question {i+1}: {q['question']}") options_dropdown = gr.Radio(q["options"], label="Select your answer") output = gr.Markdown("") gr.Markdown(f"👥 Visitors: {counters['visitors']}") with gr.Row(): gr.Button("Submit Answer").click(fn=lambda x: check_answer(i, x), inputs=options_dropdown, outputs=output) gr.Button("Show Hint").click(fn=lambda: show_hint(i), inputs=None, outputs=output) return block # ------------------------- # Quiz interface # ------------------------- question_blocks = [create_question_block(i, q) for i, q in enumerate(quiz_questions)] quiz_interface = gr.TabbedInterface(question_blocks, ["Q1", "Q2", "Q3", "Q4", "Q5"]) # Launch quiz_interface.launch()