Spaces:
Runtime error
Runtime error
File size: 1,818 Bytes
7b65932 3b74c67 7b65932 3b74c67 7b65932 3b74c67 7b65932 3b74c67 7b65932 3b74c67 7b65932 3b74c67 7b65932 3b74c67 7b65932 3b74c67 7b65932 3b74c67 7b65932 3b74c67 7b65932 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 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()
|