karthi311 commited on
Commit
af6eb72
Β·
verified Β·
1 Parent(s): 12bdbb1

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
+
6
+ # Load Hugging Face Model
7
+ tokenizer = AutoTokenizer.from_pretrained("unsloth/Llama-3.2-1B", trust_remote_code=True)
8
+ model = AutoModelForCausalLM.from_pretrained("unsloth/Llama-3.2-1B", trust_remote_code=True)
9
+
10
+ def generate_text(prompt):
11
+ inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=1024)
12
+ with torch.no_grad():
13
+ outputs = model.generate(**inputs, max_length=1024, pad_token_id=tokenizer.eos_token_id)
14
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
15
+
16
+ # Function to fetch Wikipedia summary
17
+ def search_travel_info(destination):
18
+ url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{destination}"
19
+ response = requests.get(url)
20
+ if response.status_code == 200:
21
+ data = response.json()
22
+ return data.get("extract", "No information found.")
23
+ return "No results found."
24
+
25
+ # Function to generate travel itinerary
26
+ def generate_itinerary(start_location, budget, duration, destination, purpose, preferences):
27
+ search_results = search_travel_info(destination)
28
+
29
+ # System Prompt
30
+ system_prompt = "You are an expert travel guide. Your goal is to create a well-structured, detailed itinerary based on the user's preferences."
31
+
32
+ # User Prompt
33
+ user_prompt = f"""
34
+ {system_prompt}
35
+
36
+ ### 🏷️ **Traveler Information**:
37
+ - **Budget**: {budget}
38
+ - **Purpose of Travel**: {purpose}
39
+ - **Preferences**: {preferences}
40
+
41
+ ### πŸš† **Day-wise Itinerary**:
42
+ - πŸ“ Day-by-day activities, including morning, afternoon, and evening plans
43
+ - 🎭 Must-visit attractions (famous landmarks + hidden gems)
44
+ - 🍽️ Local cuisines and top dining recommendations
45
+ - 🏨 Best places to stay (based on budget)
46
+ - πŸš— Transportation options (from {start_location} to {destination} and local travel)
47
+
48
+ ### πŸ“Œ **Additional Considerations**:
49
+ - 🌎 Cultural experiences, festivals, or seasonal events
50
+ - πŸ›οΈ Shopping and souvenir recommendations
51
+ - πŸ”Ή Safety tips, best times to visit, and local customs
52
+ - πŸ—ΊοΈ Alternative plans for bad weather days
53
+
54
+ ### ℹ️ **Additional Information from External Sources**:
55
+ {search_results}
56
+
57
+ Make sure the itinerary is engaging, practical, and customized based on the user’s budget and preferences.
58
+ """
59
+
60
+ # Generate Response
61
+ return generate_text(user_prompt)
62
+
63
+ # Streamlit UI
64
+ st.title("AI-Powered Travel Planner")
65
+ st.write("Plan your next trip with AI!")
66
+
67
+ start_location = st.text_input("Starting Location")
68
+ destination = st.text_input("Destination")
69
+ budget = st.selectbox("Select Budget", ["Low", "Moderate", "Luxury"])
70
+ duration = st.number_input("Trip Duration (days)", min_value=1, max_value=30, value=3)
71
+ purpose = st.text_area("Purpose of Trip")
72
+ preferences = st.text_area("Your Preferences (e.g., adventure, food, history)")
73
+
74
+ if st.button("Generate Itinerary"):
75
+ if start_location and destination and purpose and preferences:
76
+ itinerary = generate_itinerary(start_location, budget, duration, destination, purpose, preferences)
77
+ st.subheader("Your AI-Generated Itinerary:")
78
+ st.write(itinerary)
79
+ else:
80
+ st.warning("Please fill in all fields.")