CoolFace
Apppublic

shabul/feynman-explainer

sourceHugging Faceapache-2.0updated 5mo agoView on Hugging Face
0likes
app.py90 linesDownload Raw Back to root
1"""2Feynman Explainer — Gradio Chat App3Runs on Hugging Face Spaces (CPU free tier).4"""5 6import gradio as gr7import torch8from transformers import AutoModelForCausalLM, AutoTokenizer9 10MODEL_ID = "shabul/qwen2.5-3b-feynman-explainer"11 12SYSTEM_PROMPT = (13    "You are a Feynman-style explainer. For every question, build intuition "14    "from the ground up using concrete analogies and everyday language. "15    "No jargon until it's earned. No bullet points. Pure flowing prose. "16    "Be conversational and enthusiastic — like Feynman genuinely loved this topic."17)18 19print(f"Loading model: {MODEL_ID}")20tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)21model = AutoModelForCausalLM.from_pretrained(22    MODEL_ID,23    torch_dtype=torch.float32,24    low_cpu_mem_usage=True,25)26model.eval()27print("Model loaded.")28 29 30def respond(message: str, history: list):31    try:32        messages = [{"role": "system", "content": SYSTEM_PROMPT}]33        for h in history:34            role = h.get("role") if isinstance(h, dict) else getattr(h, "role", None)35            content = h.get("content") if isinstance(h, dict) else getattr(h, "content", None)36            if role and content:37                messages.append({"role": role, "content": str(content)})38        messages.append({"role": "user", "content": message})39 40        encoded = tokenizer.apply_chat_template(41            messages,42            tokenize=True,43            add_generation_prompt=True,44            return_tensors="pt",45            return_dict=True,46        )47        prompt_len = encoded["input_ids"].shape[1]48 49        with torch.no_grad():50            output_ids = model.generate(51                **encoded,52                max_new_tokens=100,53                do_sample=True,54                temperature=0.75,55                repetition_penalty=1.1,56            )57 58        response = tokenizer.decode(59            output_ids[0][prompt_len:],60            skip_special_tokens=True,61        )62        return response63 64    except Exception as e:65        import traceback66        err = traceback.format_exc()67        print(err)68        return f"⚠️ TRACEBACK:\n{err}"69 70 71demo = gr.ChatInterface(72    fn=respond,73    type="messages",74    title="🔬 Feynman Explainer",75    description=(76        "Ask anything. Feynman-style explanations — analogy first, no jargon until it's earned.\n\n"77        "⏱️ **CPU only** — responses take 2–4 minutes. First token appears after ~30s."78    ),79    examples=[80        "How does gradient descent actually work?",81        "What is entropy and why does it always increase?",82        "What is a p-value?",83        "Why does ice float on water?",84        "How does attention work in language models?",85    ],86    cache_examples=False,87)88 89demo.launch()90