CoolFace
Apppublic

ctrl-dev/gods

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
app.py108 linesDownload Raw Back to root
1import gradio as gr2from transformers import AutoModelForCausalLM, AutoTokenizer3import torch4 5# ✅ Naya Model Name: Qwen/Qwen2.5-0.3B-Instruct6MODEL_NAME = "Qwen/Qwen2.5-0.3B-Instruct"7 8print("⏳ Loading model...")9# Model aur Tokenizer load kar rahe hain10# Qwen ke liye 'trust_remote_code=True' ki zarurat nahi hai, par use rakha hai toh koi dikkat nahi11tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)12model = AutoModelForCausalLM.from_pretrained(13    MODEL_NAME,14    torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,15    device_map="auto"16)17print("✅ Model loaded!")18 19# CUSTOM PROMPT BUILDING FUNCTION HATA DIYA HAI (Kyunki Qwen ke liye inbuilt template better hai)20 21def chat_fn(message, history):22    # System prompt se conversation shuru karte hain23    # Note: Qwen ke official template mein system message automatically add ho jaati hai.24    messages = [] 25    26    # History ko 'messages' list mein add karte hain, Gradio ka format use karte hue27    for user, assistant in history:28        messages.append({"role": "user", "content": user})29        messages.append({"role": "assistant", "content": assistant})30 31    # Ab user ka naya message add karte hain32    messages.append({"role": "user", "content": message})33 34    # ✅ Qwen ke liye sahi template application logic:35    # `apply_chat_template` use kar rahe hain jo khud hi sahi format mein convert kar dega.36    # Agar pehle wala ValueError aaye toh `add_special_tokens=False` use kar sakte ho.37    # Lekin Qwen models ke latest version mein yeh theek se kaam karta hai.38    39    # Hum pehle wala (bug-free) Qwen logic use kar rahe hain, jo 7B mein kaam kar raha tha.40    inputs = tokenizer.apply_chat_template(41        messages, 42        add_special_tokens=True, # Qwen ka default template43        return_tensors="pt"44    )45    inputs = inputs.to(model.device) # Input ko model ki device (GPU/CPU) par bhejte hain46 47    # Model se response generate karte hain48    output = model.generate(49        inputs,50        max_new_tokens=300,51        temperature=0.7,52        top_p=0.95,53        # Agar response ruk jaye toh yeh zaroor daalna:54        eos_token_id=tokenizer.eos_token_id55    )56 57    # Generated token IDs ko human-readable string mein decode karte hain58    decoded = tokenizer.decode(output[0], skip_special_tokens=True)59    60    # ✅ Qwen-specific reply extraction:61    # Qwen ke output mein poora conversation history hota hai.62    # Hum bas naye assistant response ko nikalenge.63    64    # Pehle poori decoded string se 'system' prompt hata do, agar woh generated output mein ho.65    # Example: <|im_start|>system...<|im_end|>66    67    # Phir last 'assistant' turn ke baad wala part extract karo68    try:69        # Puraani conversation se naya reply nikalne ka try70        # Qwen ka template `<|im_start|>assistant` se start hota hai71        72        # Sahi extraction logic: last assistant tag ke baad wala text lo.73        assistant_tag = tokenizer.chat_template.split("<|im_start|>assistant")[-1].split(tokenizer.eos_token)[0].strip()74        75        # Simple split logic:76        # decoded string se last "assistant" tag aur uske baad ke text ko nikalo77        78        # NOTE: Agar pehle wala splitting logic (jo tumne use kiya tha) fail ho, toh yeh use karo:79        80        # 1. Output se user ke last message ko nikal do, jo input mein tha.81        # reply_start = decoded.rfind(message)82        # partial_decoded = decoded[reply_start:]83        84        # 2. Simplest logic: Last message ke baad ka part nikal lo85        reply = decoded.split(message)[-1].strip()86        87        # Agar reply mein koi bacha hua instruction tag aa raha ho, toh use hata do88        if "assistant" in reply:89             reply = reply.split("assistant")[-1].strip()90 91    except Exception as e:92        print(f"Extraction error: {e}")93        # Agar splitting mein dikkat aaye toh poora decode kiya hua text return kar do.94        # User ko kam se kam response mil jayega.95        reply = decoded.split("assistant")[-1].strip() if "assistant" in decoded else decoded.strip()96 97 98    return reply99 100# Gradio Chat Interface setup karte hain101ui = gr.ChatInterface(102    chat_fn,103    title="Captain Ambar — HF GPT Assistant",104    # Description update kar diya hai105    description="Running on HuggingFace 🟦 — Qwen2.5 0.3B Instruct",106)107 108ui.launch()