""" STEP 3 — Gradio app that uses YOUR OWN model ============================================= Upload this as app.py to your Hugging Face Space. It loads YOUR model (dineshkasi/my-ai-assistant) — not anyone else's! Your friends visit: https://huggingface.co/spaces/YOUR_USERNAME/YOUR_MODEL_NAME """ import torch import gradio as gr from transformers import AutoModelForCausalLM, AutoTokenizer # ══════════════════════════════════════════════════════ # ✏️ CHANGE THESE TO YOUR OWN DETAILS # ══════════════════════════════════════════════════════ HF_USERNAME = "DineshKasi" MODEL_NAME = "ai-assistant" # ══════════════════════════════════════════════════════ REPO_ID = f"{HF_USERNAME}/{MODEL_NAME}" print(f"Loading YOUR model: {REPO_ID} ...") tokenizer = AutoTokenizer.from_pretrained(REPO_ID) model = AutoModelForCausalLM.from_pretrained(REPO_ID) model.eval() print("Model loaded!") def ask(question, history, temperature, max_tokens): """Generate a response from YOUR model.""" # Build context from history (last 3 turns) context = "" for user_msg, bot_msg in history[-3:]: context += f"<|user|>{user_msg}<|endoftext|>" context += f"<|assistant|>{bot_msg}<|endoftext|>" prompt = context + f"<|user|>{question}<|endoftext|><|assistant|>" inputs = tokenizer.encode(prompt, return_tensors="pt") with torch.no_grad(): output = model.generate( inputs, max_new_tokens=int(max_tokens), temperature=float(temperature), do_sample=True, top_p=0.92, repetition_penalty=1.1, pad_token_id=tokenizer.eos_token_id, eos_token_id=tokenizer.eos_token_id, ) full_text = tokenizer.decode(output[0], skip_special_tokens=True) # Extract only the last assistant reply if "<|assistant|>" in full_text: reply = full_text.split("<|assistant|>")[-1].strip() else: reply = full_text[len(prompt):].strip() return reply # ── Gradio UI ───────────────────────────────────────── with gr.Blocks( theme=gr.themes.Soft(primary_hue="violet"), title=f"{HF_USERNAME}'s AI Assistant", css=""" #header { text-align:center; padding: 24px 0 8px; } #header h1 { font-size:2rem; font-weight:700; color:#7c3aed; margin:0; } #header p { color:#6b7280; margin:6px 0 0; } .badge { display:inline-block; background:#f3e8ff; color:#7c3aed; border-radius:999px; padding:3px 14px; font-size:0.8rem; margin:3px; font-weight:500; } #badges { text-align:center; margin:8px 0 18px; } #model-credit { text-align:center; margin-top:14px; font-size:0.8rem; color:#9ca3af; } #model-credit a { color:#7c3aed; text-decoration:none; } footer { display:none !important; } """, ) as demo: gr.HTML(f"""
💻 Coding 🔬 Science 📐 Math ✍️ Writing 📊 Business 🌍 General
""") chatbot = gr.Chatbot( label="Chat with my model", bubble_full_width=False, height=460, ) with gr.Row(): txt = gr.Textbox( placeholder="Ask me anything...", show_label=False, scale=8, container=False, ) btn = gr.Button("Send ➤", variant="primary", scale=1) gr.Examples( examples=[ "Write a Python function to find prime numbers", "Explain how neural networks learn", "What is the difference between RAM and ROM?", "Help me write a professional email", "What is quantum entanglement?", "How does a binary search tree work?", ], inputs=txt, label="💡 Try these", ) with gr.Accordion("⚙️ Settings", open=False): temperature = gr.Slider(0.1, 1.2, value=0.7, step=0.1, label="Temperature (creativity)") max_tokens = gr.Slider(64, 512, value=200, step=32, label="Max response length") with gr.Accordion("🔌 Use this model in your project", open=False): gr.Markdown(f""" ### Load my model directly in Python ```python pip install transformers torch ``` ```python from transformers import AutoModelForCausalLM, AutoTokenizer import torch tokenizer = AutoTokenizer.from_pretrained("{REPO_ID}") model = AutoModelForCausalLM.from_pretrained("{REPO_ID}") def ask(question): prompt = f"<|user|>{{question}}<|endoftext|><|assistant|>" inputs = tokenizer.encode(prompt, return_tensors="pt") with torch.no_grad(): output = model.generate(inputs, max_new_tokens=200, temperature=0.7, do_sample=True, pad_token_id=tokenizer.eos_token_id) reply = tokenizer.decode(output[0], skip_special_tokens=True) return reply.split("<|assistant|>")[-1].strip() print(ask("Explain machine learning in simple terms")) ``` ### Via Hugging Face Inference API (no install) ```python from huggingface_hub import InferenceClient client = InferenceClient("{REPO_ID}") result = client.text_generation( "<|user|>What is a neural network?<|endoftext|><|assistant|>", max_new_tokens=200, ) print(result) ``` > Model page: [huggingface.co/{REPO_ID}](https://huggingface.co/{REPO_ID}) """) gr.HTML(f"""
Model: {REPO_ID}  ·  Built by {HF_USERNAME}  ·  Hosted on Hugging Face Spaces
""") # Wiring def submit(msg, history): return "", history + [[msg, None]] def respond(history, temp, max_tok): question = history[-1][0] history[-1][1] = ask(question, history[:-1], temp, max_tok) return history txt.submit(submit, [txt, chatbot], [txt, chatbot], queue=False).then( respond, [chatbot, temperature, max_tokens], chatbot ) btn.click(submit, [txt, chatbot], [txt, chatbot], queue=False).then( respond, [chatbot, temperature, max_tokens], chatbot ) demo.queue() demo.launch()