ritvik77 commited on
Commit
9da8c23
·
verified ·
1 Parent(s): a020bae

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -0
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import TypedDict, Annotated, List, Union
2
+ from langchain_core.messages import HumanMessage, AIMessage
3
+ from langchain_core.prompts import ChatPromptTemplate
4
+ from langchain_groq import ChatGroq
5
+ import requests
6
+ import gradio as gr
7
+
8
+
9
+ class PlannerState(TypedDict):
10
+ messages: Annotated[List[Union[HumanMessage, AIMessage]], "The messages in the conversation"]
11
+ city: str
12
+ interests: List[str]
13
+ itinerary: str
14
+
15
+
16
+ # Initialize the LLM with better engagement
17
+ llm = ChatGroq(
18
+ temperature=0.7,
19
+ groq_api_key="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",
20
+ model_name="llama-3.3-70b-versatile"
21
+ )
22
+
23
+ # Prompt for generating an itinerary
24
+ itinerary_prompt = ChatPromptTemplate.from_messages([
25
+ ("system", "You are a smart travel agent who creates engaging, fun, and optimized day trip itineraries for {city}. \
26
+ Tailor recommendations based on the user's interests: {interests}. \
27
+ Include hidden gems, famous spots, and local cuisine. Keep it structured, with timestamps."),
28
+ ("human", "Plan my perfect day trip!")
29
+ ])
30
+
31
+ # Prompt for generating a fun fact
32
+ fun_fact_prompt = ChatPromptTemplate.from_messages([
33
+ ("system", "You are a knowledgeable travel guide. Share an interesting and unique fun fact about {city} that most travelers don’t know."),
34
+ ("human", "Tell me a fun fact about {city}!")
35
+ ])
36
+
37
+
38
+ # Function to collect city input
39
+ def input_city(city: str, state: PlannerState) -> PlannerState:
40
+ return {
41
+ **state,
42
+ "city": city,
43
+ "messages": state["messages"] + [HumanMessage(content=f"City: {city}")]
44
+ }
45
+
46
+
47
+ # Function to collect interests input
48
+ def input_interests(interests: str, state: PlannerState) -> PlannerState:
49
+ interest_list = [interest.strip() for interest in interests.split(",")]
50
+ return {
51
+ **state,
52
+ "interests": interest_list,
53
+ "messages": state["messages"] + [HumanMessage(content=f"Interests: {', '.join(interest_list)}")]
54
+ }
55
+
56
+
57
+ # Function to create the itinerary
58
+ def create_itinerary(state: PlannerState) -> str:
59
+ response = llm.invoke(itinerary_prompt.format_messages(city=state["city"], interests=', '.join(state["interests"])))
60
+ return response.content
61
+
62
+
63
+ # Function to fetch real-time weather data
64
+ def get_weather(city: str) -> str:
65
+ api_key = "b787841c42e84b32b70235141251102"
66
+ url = f"http://api.weatherapi.com/v1/current.json?key={api_key}&q={city}&aqi=no"
67
+ try:
68
+ response = requests.get(url)
69
+ data = response.json()
70
+ if "current" in data:
71
+ weather_desc = data["current"]["condition"]["text"]
72
+ temp = data["current"]["temp_c"]
73
+ return f"Current weather in {city}: {weather_desc}, {temp}°C"
74
+ else:
75
+ return "Weather data unavailable."
76
+ except:
77
+ return "Error fetching weather."
78
+
79
+
80
+ # Function to generate a fun fact about the city
81
+ def fun_fact(city: str) -> str:
82
+ response = llm.invoke(fun_fact_prompt.format_messages(city=city))
83
+ return response.content
84
+
85
+
86
+ # Gradio function to generate travel plan
87
+ def travel_planner(city: str, interests: str):
88
+ # Initialize state
89
+ state = {
90
+ "messages": [],
91
+ "city": "",
92
+ "interests": [],
93
+ "itinerary": "",
94
+ }
95
+
96
+ # Update state with user inputs
97
+ state = input_city(city, state)
98
+ state = input_interests(interests, state)
99
+
100
+ # Generate itinerary, weather, and fun fact
101
+ itinerary = create_itinerary(state)
102
+ weather = get_weather(city)
103
+ fact = fun_fact(city)
104
+
105
+ return itinerary, weather, fact
106
+
107
+
108
+ # Build the Gradio interface with enhanced UI
109
+ interface = gr.Interface(
110
+ fn=travel_planner,
111
+ inputs=[
112
+ gr.Textbox(label="Enter the city for your trip", placeholder="e.g., Paris, Tokyo, New York"),
113
+ gr.Textbox(label="Enter your interests (comma-separated)", placeholder="e.g., history, food, adventure"),
114
+ ],
115
+ outputs=[
116
+ gr.Textbox(label="Generated Itinerary", interactive=False),
117
+ gr.Textbox(label="Current Weather", interactive=False),
118
+ gr.Textbox(label="Fun Fact", interactive=False)
119
+ ],
120
+ theme="soft",
121
+ title="✈️ AI Travel Planner",
122
+ description="Plan your next adventure! Get a custom itinerary, weather forecast, and a fun fact!",
123
+ live=True,
124
+ )
125
+
126
+ # Launch the Gradio application
127
+ interface.launch()