Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,134 +1,122 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# ======================================
|
| 20 |
-
# πΉ Load Model (HF Hosted)
|
| 21 |
-
# ======================================
|
| 22 |
-
@st.cache_resource
|
| 23 |
-
def load_model():
|
| 24 |
-
"""
|
| 25 |
-
Load Grok 70B model via Hugging Face pipeline (hosted inference)
|
| 26 |
-
"""
|
| 27 |
-
model_name = "GrokAI/llama-3.3-70b-versatile"
|
| 28 |
-
generator = pipeline(
|
| 29 |
-
"text-generation",
|
| 30 |
-
model=model_name,
|
| 31 |
-
device_map="auto", # uses GPU if available, else CPU
|
| 32 |
-
trust_remote_code=True # required for Grok models
|
| 33 |
)
|
| 34 |
-
return
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
"
|
| 88 |
-
|
| 89 |
-
{
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
#
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from groq import Groq
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
# ββ Agent Runner βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 9 |
+
def run_agent(client, role, backstory, task):
|
| 10 |
+
response = client.chat.completions.create(
|
| 11 |
+
model="llama-3.3-70b-versatile",
|
| 12 |
+
messages=[
|
| 13 |
+
{"role": "system", "content": f"You are a {role}. {backstory}"},
|
| 14 |
+
{"role": "user", "content": task}
|
| 15 |
+
],
|
| 16 |
+
temperature=0.7,
|
| 17 |
+
max_tokens=1024,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
+
return response.choices[0].message.content.strip()
|
| 20 |
+
|
| 21 |
+
# ββ Three Agents Sequentially ββββββββββββββββββββββββββββββββββββ
|
| 22 |
+
def plan_trip(destination, trip_days, budget_range):
|
| 23 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 24 |
+
if not api_key:
|
| 25 |
+
return "β GROQ_API_KEY not set. Add it in Space Settings β Variables and secrets."
|
| 26 |
+
if not destination.strip():
|
| 27 |
+
return "β οΈ Please enter a destination."
|
| 28 |
+
|
| 29 |
+
client = Groq(api_key=api_key)
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
# Agent 1 β Researcher
|
| 33 |
+
research = run_agent(
|
| 34 |
+
client,
|
| 35 |
+
role="Travel Researcher",
|
| 36 |
+
backstory="You have deep knowledge of global destinations, cultures, and hidden gems.",
|
| 37 |
+
task=(
|
| 38 |
+
f"Research {destination} and provide:\n"
|
| 39 |
+
f"- Top 5 tourist attractions (1-2 lines each)\n"
|
| 40 |
+
f"- 3 hotel recommendations across budget levels\n"
|
| 41 |
+
f"- 5 must-try local foods\n"
|
| 42 |
+
f"- Best time to visit and general travel tips\n"
|
| 43 |
+
f"Be concise and specific."
|
| 44 |
+
)
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Agent 2 β Planner (receives researcher output)
|
| 48 |
+
itinerary = run_agent(
|
| 49 |
+
client,
|
| 50 |
+
role="Travel Planner",
|
| 51 |
+
backstory="You craft realistic, enjoyable day-by-day itineraries tailored to traveler preferences.",
|
| 52 |
+
task=(
|
| 53 |
+
f"Based on this research about {destination}:\n{research}\n\n"
|
| 54 |
+
f"Create a {trip_days}-day itinerary with Morning, Afternoon, and Evening activities each day. "
|
| 55 |
+
f"Be specific with place names and timings."
|
| 56 |
+
)
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Agent 3 β Budget Analyst (receives itinerary output)
|
| 60 |
+
budget = run_agent(
|
| 61 |
+
client,
|
| 62 |
+
role="Travel Budget Analyst",
|
| 63 |
+
backstory="You give accurate, realistic travel cost breakdowns in USD.",
|
| 64 |
+
task=(
|
| 65 |
+
f"Based on this {trip_days}-day itinerary for {destination}:\n{itinerary}\n\n"
|
| 66 |
+
f"Provide a full budget for a {budget_range} traveler (1 person) covering:\n"
|
| 67 |
+
f"- Flights (round trip estimate)\n"
|
| 68 |
+
f"- Accommodation (per night Γ {trip_days} nights)\n"
|
| 69 |
+
f"- Food & Dining\n"
|
| 70 |
+
f"- Activities & Entrance Fees\n"
|
| 71 |
+
f"- Local Transport\n"
|
| 72 |
+
f"- Miscellaneous\n"
|
| 73 |
+
f"- TOTAL ESTIMATED COST\n"
|
| 74 |
+
f"Use USD. Be realistic for the {budget_range} level."
|
| 75 |
+
)
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
return (
|
| 79 |
+
f"# βοΈ {destination} β {trip_days}-Day Trip ({budget_range})\n"
|
| 80 |
+
f"{'='*60}\n\n"
|
| 81 |
+
f"## π Agent 1: Research\n{research}\n\n"
|
| 82 |
+
f"{'='*60}\n\n"
|
| 83 |
+
f"## ποΈ Agent 2: Itinerary\n{itinerary}\n\n"
|
| 84 |
+
f"{'='*60}\n\n"
|
| 85 |
+
f"## π° Agent 3: Budget Breakdown\n{budget}\n"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
return f"β Error: {str(e)}"
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
# ββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 93 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Travel Planner AI") as demo:
|
| 94 |
+
|
| 95 |
+
gr.Markdown(
|
| 96 |
+
"# βοΈ Travel Planner AI\n"
|
| 97 |
+
"Three AI agents work sequentially: **Researcher β Planner β Budget Analyst** "
|
| 98 |
+
"(powered by Groq + LLaMA3-70b)"
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
with gr.Row():
|
| 102 |
+
with gr.Column(scale=1):
|
| 103 |
+
destination = gr.Textbox(label="Destination", placeholder="e.g. Paris, Tokyo, Istanbul")
|
| 104 |
+
trip_days = gr.Slider(1, 14, value=5, step=1, label="Number of Days")
|
| 105 |
+
budget_range = gr.Radio(["Budget", "Mid-range", "Luxury"], value="Mid-range", label="Budget Style")
|
| 106 |
+
plan_btn = gr.Button("πΊοΈ Plan My Trip", variant="primary")
|
| 107 |
+
|
| 108 |
+
with gr.Column(scale=2):
|
| 109 |
+
output = gr.Textbox(
|
| 110 |
+
label="Your AI-Generated Trip Plan",
|
| 111 |
+
lines=35,
|
| 112 |
+
placeholder="Your trip plan will appear here once the three agents complete their work..."
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
plan_btn.click(fn=plan_trip, inputs=[destination, trip_days, budget_range], outputs=output)
|
| 116 |
+
|
| 117 |
+
gr.Markdown(
|
| 118 |
+
"> π Powered by [Groq](https://groq.com) (free). "
|
| 119 |
+
"Add your `GROQ_API_KEY` in **Space Settings β Variables and secrets**."
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True)
|