Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from langchain.agents import AgentExecutor, Tool
|
4 |
+
from langchain.prompts import PromptTemplate
|
5 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
6 |
+
from dotenv import load_dotenv # Optional for .env files
|
7 |
+
|
8 |
+
# Load environment variables from .env file
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
# Get Hugging Face token from environment variable
|
12 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
13 |
+
if not HF_TOKEN:
|
14 |
+
raise ValueError("Hugging Face API token not found. Set HF_TOKEN in your environment variables.")
|
15 |
+
|
16 |
+
# Define the Hugging Face model endpoint
|
17 |
+
llm = HuggingFaceEndpoint(
|
18 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
19 |
+
huggingfacehub_api_token=HF_TOKEN.strip(),
|
20 |
+
temperature=0.7,
|
21 |
+
max_new_tokens=150,
|
22 |
+
)
|
23 |
+
|
24 |
+
# Define specialized agents
|
25 |
+
|
26 |
+
def flight_booking(input):
|
27 |
+
return f"Searching flights for: {input}"
|
28 |
+
|
29 |
+
flight_tool = Tool(
|
30 |
+
name="Flight Booking Tool",
|
31 |
+
func=flight_booking,
|
32 |
+
description="Finds and books airline flights"
|
33 |
+
)
|
34 |
+
|
35 |
+
def hotel_booking(input):
|
36 |
+
return f"Searching hotels for: {input}"
|
37 |
+
|
38 |
+
hotel_tool = Tool(
|
39 |
+
name="Hotel Booking Tool",
|
40 |
+
func=hotel_booking,
|
41 |
+
description="Searches and books accommodations"
|
42 |
+
)
|
43 |
+
|
44 |
+
def transportation_booking(input):
|
45 |
+
return f"Arranging transportation for: {input}"
|
46 |
+
|
47 |
+
transport_tool = Tool(
|
48 |
+
name="Transportation Booking Tool",
|
49 |
+
func=transportation_booking,
|
50 |
+
description="Handles rental cars, shuttles, or trains"
|
51 |
+
)
|
52 |
+
|
53 |
+
def activity_booking(input):
|
54 |
+
return f"Finding activities for: {input}"
|
55 |
+
|
56 |
+
activity_tool = Tool(
|
57 |
+
name="Activity Booking Tool",
|
58 |
+
func=activity_booking,
|
59 |
+
description="Books activities, tours, and events"
|
60 |
+
)
|
61 |
+
|
62 |
+
tools = [flight_tool, hotel_tool, transport_tool, activity_tool]
|
63 |
+
|
64 |
+
# Define a prompt for the planner agent
|
65 |
+
planner_prompt = PromptTemplate(
|
66 |
+
template="You are a travel planner. Your task is to coordinate tools to plan a trip based on user input: {input}",
|
67 |
+
input_variables=["input"]
|
68 |
+
)
|
69 |
+
|
70 |
+
# Create the agent
|
71 |
+
planner_agent = AgentExecutor.from_agent_and_tools(
|
72 |
+
llm=llm,
|
73 |
+
tools=tools,
|
74 |
+
verbose=True
|
75 |
+
)
|
76 |
+
|
77 |
+
# Define the Gradio function
|
78 |
+
def plan_trip(user_input):
|
79 |
+
"""
|
80 |
+
Processes user input through the planner agent and returns the output.
|
81 |
+
"""
|
82 |
+
try:
|
83 |
+
output = planner_agent.run(user_input)
|
84 |
+
return output
|
85 |
+
except Exception as e:
|
86 |
+
return f"Error: {e}"
|
87 |
+
|
88 |
+
# Create the Gradio interface
|
89 |
+
with gr.Blocks() as travel_planner_app:
|
90 |
+
gr.Markdown("## AI Travel Planner")
|
91 |
+
gr.Markdown("Enter your trip details below, and the AI will help you plan your travel itinerary!")
|
92 |
+
|
93 |
+
with gr.Row():
|
94 |
+
user_input = gr.Textbox(
|
95 |
+
label="Trip Details",
|
96 |
+
placeholder="E.g., Plan a trip to Paris from May 10 to May 15 for a family of 4."
|
97 |
+
)
|
98 |
+
submit_button = gr.Button("Plan Trip")
|
99 |
+
output = gr.Textbox(label="Travel Plan")
|
100 |
+
|
101 |
+
submit_button.click(plan_trip, inputs=user_input, outputs=output)
|
102 |
+
|
103 |
+
# Launch the Gradio app
|
104 |
+
if __name__ == "__main__":
|
105 |
+
travel_planner_app.launch()
|