|
import os |
|
import gradio as gr |
|
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline |
|
from huggingface_hub import login |
|
|
|
|
|
HF_TOKEN = os.getenv("HF_TOKEN") |
|
|
|
|
|
if HF_TOKEN: |
|
login(HF_TOKEN) |
|
else: |
|
raise ValueError("π¨ Hugging Face token not found! Please add it to Secrets in your Hugging Face Space.") |
|
|
|
|
|
model_name = "TheBloke/Mistral-7B-Instruct-v0.1-AWQ" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name, token=HF_TOKEN) |
|
model = AutoModelForCausalLM.from_pretrained( |
|
model_name, token=HF_TOKEN, device_map="auto", trust_remote_code=True |
|
) |
|
|
|
chatbot = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0) |
|
|
|
def generate_itinerary(destination, start_date, end_date, traveler_type, companion): |
|
"""Generates an AI-powered travel itinerary based on user preferences""" |
|
|
|
prompt = f""" |
|
You are a professional travel assistant. Create a **day-by-day itinerary** for a trip to {destination}. |
|
- π
**Dates**: {start_date} to {end_date} |
|
- π **Traveler Type**: {traveler_type} |
|
- π§³ **Companion**: {companion} |
|
- Provide **3 activities per day**: π
Morning, βοΈ Afternoon, π Night. |
|
- Include **real attractions, famous landmarks, hidden gems, and local experiences**. |
|
- Keep responses **structured, short, and engaging**. |
|
|
|
π **Example Format**: |
|
**Day 1**: |
|
- π
**Morning**: Visit [Famous Landmark] in {destination} |
|
- βοΈ **Afternoon**: Try [Local Food] at [Best Restaurant] |
|
- π **Night**: Experience [Cultural/Nightlife Activity] |
|
""" |
|
|
|
response = chatbot(prompt, max_length=250, do_sample=True, temperature=0.6, top_p=0.85) |
|
return response[0]['generated_text'] |
|
|
|
|
|
interface = gr.Interface( |
|
fn=generate_itinerary, |
|
inputs=[ |
|
gr.Textbox(label="Destination"), |
|
gr.Textbox(label="Start Date (YYYY-MM-DD)"), |
|
gr.Textbox(label="End Date (YYYY-MM-DD)"), |
|
gr.Dropdown(["Art", "Food", "Nature", "Culture", "Nightlife"], label="Traveler Type"), |
|
gr.Dropdown(["Alone", "With Family", "With Friends", "With Partner"], label="Companion Type"), |
|
], |
|
outputs="text", |
|
title="βοΈ AI Travel Assistant", |
|
description="Enter your details, and let AI plan your perfect trip!" |
|
) |
|
|
|
interface.launch() |
|
|