Team Ai
Apppublic

Chuxia-sys/tiny-model

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
app.py62 linesDownload Raw Back to root
1import gradio as gr2from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline3 4# === Model Settings ===5BASE_MODEL = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"6FINETUNED_MODEL = "Chuxia-sys/tiny_model"7 8# โœ… Load tokenizer from base (for compatibility)9tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)10 11# โœ… Load fine-tuned model12model = AutoModelForCausalLM.from_pretrained(FINETUNED_MODEL)13 14# โœ… Create text generation pipeline15pipe = pipeline(16    "text-generation",17    model=model,18    tokenizer=tokenizer,19    max_new_tokens=180,20    temperature=0.7,21    top_p=0.9,22)23 24# === System Prompt ===25SYSTEM_PROMPT = (26    "You are a helpful AI travel planner specializing only in the Philippines. "27    "You can recommend itineraries, places, and hotels, but do NOT respond to questions "28    "outside the Philippines or unrelated to travel."29)30 31# === Chat Logic ===32def chat(user_message, history):33    # Keep recent context34    context = ""35    for turn in history[-3:]:  # Keep last 3 user-assistant pairs36        context += f"[USER] {turn[0]}\n[ASSISTANT] {turn[1]}\n"37 38    prompt = f"{SYSTEM_PROMPT}\n{context}[USER] {user_message}\n[ASSISTANT]"39 40    output = pipe(prompt)[0]["generated_text"]41 42    # Extract assistant's response43    response = output.split("[ASSISTANT]")[-1].strip()44    return response45 46# === Gradio Chat Interface ===47chatbot = gr.ChatInterface(48    fn=chat,49    title="๐Ÿ‡ต๐Ÿ‡ญ PH Travel Planner Chatbot",50    description="Ask for travel plans, itineraries, or hotel suggestions anywhere in the Philippines!",51    examples=[52        ["Plan a 3-day trip to Cebu."],53        ["Suggest some hotels in Palawan near the beach."],54        ["Where should I go for a weekend in Baguio?"],55    ],56    theme="soft",57)58 59# === Launch App ===60if __name__ == "__main__":61    chatbot.launch()62