Spaces:
Runtime error
Runtime error
Update app.py
Browse files5 unmodified lines67891011123 unmodified lines16171819202122232411 unmodified lines3637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182834 unmodified lines8889909192939495969798991001011021031041051061071081091101111121131141151161171181191205 unmodified lines# ===============================# Load model and tokenizer# ===============================MODEL_PATH = "Chuxia-sys/travel-chatbot-flan-t5" # Change to your model pathprint("π Loading model...")tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_PATH)3 unmodified lines# ===============================# Cleaning & Formatting# ===============================def format_itinerary(text):text = re.sub(r'\s+', ' ', text).strip()text = re.sub(r'(?i)(day\s*\d+)', lambda m: '\n\n' + m.group(1).title(), text)11 unmodified linesfor wrong, correct in replacements.items():text = text.replace(wrong, correct)# Enforce desired display formatformatted = ("π **Daily Itinerary**\n"f"{text}\n\n""π **Estimated Budget:** β±[Insert per day budget]\n""π **Accommodation:** [Insert hotel/resort]\n""________________________________________\n\n""π° **Total Budget:** β±[Insert total]\n""________________________________________")return formatted.strip()# ===============================# Generate Reply# ===============================def generate_reply(user_input):prompt = f"### Instruction:\n{user_input}\n\n### Response:\nCreate a 3-day travel itinerary in the following format:\n\n" \"π Daily Itinerary\n" \"Day 1 β [Location/Theme Title]\n" \"Morning: Visit (place) β short description or activity\n" \"Afternoon: Visit (place) β activity\n" \"Evening: Activity or dinner suggestion\n" \"π Estimated Budget: β±[amount]\n" \"π Accommodation: [hotel/resort]\n" \"________________________________________\n" \"Day 2 β [Location/Theme Title]\n" \"Morning: Activity / Site visit\n" \"Afternoon: Activity / Local food stop\n" \"Evening: Nightlife / Shopping / Relaxation\n" \"π Estimated Budget: β±[amount]\n" \"________________________________________\n" \"Day 3 β [Location/Theme Title]\n" \"Morning: Last-minute tours or souvenir shopping\n" \"Afternoon: Lunch and check-out\n" \"Evening: Return trip\n" \"π Estimated Budget: β±[amount]\n" \"________________________________________\n" \"Total Budget : β±[total]\n" \"________________________________________\n"inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512).to(device)with torch.no_grad():outputs = model.generate(**inputs,4 unmodified linesdo_sample=True)response = tokenizer.decode(outputs[0], skip_special_tokens=True)return format_itinerary(response)# ===============================# Gradio UI# ===============================with gr.Blocks(title="π΅π Philippine Travel Chatbot π΄") as demo:gr.Markdown("""# π§ **AI Travel Chatbot**Enter your request below and get a beautifully formatted **Daily Itinerary**.Example:> Plan a 3-day trip to Cebu with beaches and food for β±15,000.""")chat_input = gr.Textbox(label="Your Travel Request", placeholder="e.g. Plan a 2-day trip to Baguio for β±8,000")chat_output = gr.Textbox(label="Generated Itinerary", lines=20, placeholder="The AI itinerary will appear here...")generate_btn = gr.Button("Generate Itinerary βοΈ")generate_btn.click(fn=generate_reply, inputs=chat_input, outputs=chat_output)gr.Markdown("________________________________________")gr.Markdown("β¨ Created by [= Chuxia =] | Powered by Hugging Face Transformers")# ===============================# Launch# ===============================if __name__ == "__main__":demo.launch()5 unmodified lines6789101112133 unmodified lines17181920212223242511 unmodified lines3738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101114 unmodified lines1161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451465 unmodified lines# ===============================# Load model and tokenizer# ===============================MODEL_PATH = "Chuxia-sys/travel-chatbot-flan-t5" # or HF Hub model pathprint("π Loading model...")tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_PATH)3 unmodified lines# ===============================# Cleaning & formatting function# ===============================def clean_itinerary(text):text = re.sub(r'\s+', ' ', text).strip()text = re.sub(r'(?i)(day\s*\d+)', lambda m: '\n\n' + m.group(1).title(), text)11 unmodified linesfor wrong, correct in replacements.items():text = text.replace(wrong, correct)# Normalize currencytext = re.sub(r'(\d{1,3})(\d{3})(?!\d)', r'\1,\2', text)text = re.sub(r'(?<!β±)\b(\d{3,5})\b', r'β±\1', text)# Add emojistext = text.replace("Morning:", "\nπ Morning:") \.replace("Afternoon:", "\nπ Afternoon:") \.replace("Evening:", "\nπ Evening:") \.replace("Total for Day", "π° Total for Day")text = re.sub(r'\s{2,}', ' ', text)return text.strip()# ===============================# Chatbot logic# ===============================def generate_reply(user_input):prompt = f"""You are a Philippine Travel Planner AI.Your task is to generate detailed, day-by-day travel itineraries strictly following this format below.Never deviate from the structure, emojis, or line spacing.-------------------------------π Daily ItineraryDay 1 β [Location/Theme Title]Morning:Visit (place) β short description or activityAfternoon:Visit (place) β activityEvening:Activity or dinner suggestionπ Estimated Budget: β±[amount]π Accommodation: [hotel or resort name]________________________________________Day 2 β [Location/Theme Title]Morning: Activity / Site visitAfternoon: Activity / Local food stopEvening: Nightlife / Shopping / Relaxationπ Estimated Budget: β±[amount]________________________________________Day 3 β [Location/Theme Title]Morning: Last-minute tours or souvenir shoppingAfternoon: Lunch and check-outEvening: Return tripπ Estimated Budget: β±[amount]________________________________________π° Total Budget: β±[total amount]________________________________________-------------------------------Rules:- Always use the emoji-based headers and dividers.- Always use ββ±β before numbers.- Each Day must include Morning, Afternoon, and Evening activities.- Use real or realistic Philippine destinations, restaurants, and hotels.- Keep language natural, concise, and travel-guide friendly.- Do not include explanations, summaries, or meta text.User Request:{user_input}"""inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(device)with torch.no_grad():outputs = model.generate(**inputs,4 unmodified linesdo_sample=True)raw_output = tokenizer.decode(outputs[0], skip_special_tokens=True)return clean_itinerary(raw_output)# ===============================# Gradio UI# ===============================with gr.Blocks(title="Philippine Travel Chatbot π΄") as demo:gr.Markdown("""# π΅π **AI Travel Chatbot**Type your request (e.g., *"Create a 3-day travel plan to Cebu with budget"*)The chatbot will generate a clean, formatted itinerary with day-by-day details.""")chat_input = gr.Textbox(label="Your Travel Request", placeholder="e.g., 2-day trip to Ilocos with budget β±10,000")chat_output = gr.Textbox(label="AI Travel Plan", lines=15)submit_btn = gr.Button("Generate Itinerary βοΈ")submit_btn.click(fn=generate_reply, inputs=chat_input, outputs=chat_output)gr.Markdown("---")gr.Markdown("β¨ Created by [== Chuxia ==] | Powered by Hugging Face Transformers")# ===============================# Launch the app# ===============================if __name__ == "__main__":demo.launch()
app.py+81-55