Spaces:
Runtime error

tiny-model / app.py
Chuxia-sys's picture
Update app.py
3b74c67 verified
Raw History Blame Contribute Delete
1.82 kB
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
# === Model Settings ===
BASE_MODEL = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
FINETUNED_MODEL = "Chuxia-sys/tiny_model"
# βœ… Load tokenizer from base (for compatibility)
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
# βœ… Load fine-tuned model
model = AutoModelForCausalLM.from_pretrained(FINETUNED_MODEL)
# βœ… Create text generation pipeline
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_new_tokens=180,
temperature=0.7,
top_p=0.9,
)
# === System Prompt ===
SYSTEM_PROMPT = (
"You are a helpful AI travel planner specializing only in the Philippines. "
"You can recommend itineraries, places, and hotels, but do NOT respond to questions "
"outside the Philippines or unrelated to travel."
)
# === Chat Logic ===
def chat(user_message, history):
# Keep recent context
context = ""
for turn in history[-3:]: # Keep last 3 user-assistant pairs
context += f"[USER] {turn[0]}\n[ASSISTANT] {turn[1]}\n"
prompt = f"{SYSTEM_PROMPT}\n{context}[USER] {user_message}\n[ASSISTANT]"
output = pipe(prompt)[0]["generated_text"]
# Extract assistant's response
response = output.split("[ASSISTANT]")[-1].strip()
return response
# === Gradio Chat Interface ===
chatbot = gr.ChatInterface(
fn=chat,
title="πŸ‡΅πŸ‡­ PH Travel Planner Chatbot",
description="Ask for travel plans, itineraries, or hotel suggestions anywhere in the Philippines!",
examples=[
["Plan a 3-day trip to Cebu."],
["Suggest some hotels in Palawan near the beach."],
["Where should I go for a weekend in Baguio?"],
],
theme="soft",
)
# === Launch App ===
if __name__ == "__main__":
chatbot.launch()