""" 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"""
Powered by {REPO_ID} — your very own model!