crew_ai_gradio / app.py
muddasser's picture
Update app.py
add1b67 verified
import gradio as gr
import os
from dotenv import load_dotenv
from groq import Groq
load_dotenv()
# ── Agent Runner ─────────────────────────────────────────────────
def run_agent(client, role, backstory, task):
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{"role": "system", "content": f"You are a {role}. {backstory}"},
{"role": "user", "content": task}
],
temperature=0.7,
max_tokens=1024,
)
return response.choices[0].message.content.strip()
# ── Three Agents Sequentially ────────────────────────────────────
def plan_trip(destination, trip_days, budget_range):
api_key = os.getenv("GROQ_API_KEY")
if not api_key:
return "❌ GROQ_API_KEY not set. Add it in Space Settings β†’ Variables and secrets."
if not destination.strip():
return "⚠️ Please enter a destination."
client = Groq(api_key=api_key)
try:
# Agent 1 β€” Researcher
research = run_agent(
client,
role="Travel Researcher",
backstory="You have deep knowledge of global destinations, cultures, and hidden gems.",
task=(
f"Research {destination} and provide:\n"
f"- Top 5 tourist attractions (1-2 lines each)\n"
f"- 3 hotel recommendations across budget levels\n"
f"- 5 must-try local foods\n"
f"- Best time to visit and general travel tips\n"
f"Be concise and specific."
)
)
# Agent 2 β€” Planner (receives researcher output)
itinerary = run_agent(
client,
role="Travel Planner",
backstory="You craft realistic, enjoyable day-by-day itineraries tailored to traveler preferences.",
task=(
f"Based on this research about {destination}:\n{research}\n\n"
f"Create a {trip_days}-day itinerary with Morning, Afternoon, and Evening activities each day. "
f"Be specific with place names and timings."
)
)
# Agent 3 β€” Budget Analyst (receives itinerary output)
budget = run_agent(
client,
role="Travel Budget Analyst",
backstory="You give accurate, realistic travel cost breakdowns in USD.",
task=(
f"Based on this {trip_days}-day itinerary for {destination}:\n{itinerary}\n\n"
f"Provide a full budget for a {budget_range} traveler (1 person) covering:\n"
f"- Flights (round trip estimate)\n"
f"- Accommodation (per night Γ— {trip_days} nights)\n"
f"- Food & Dining\n"
f"- Activities & Entrance Fees\n"
f"- Local Transport\n"
f"- Miscellaneous\n"
f"- TOTAL ESTIMATED COST\n"
f"Use USD. Be realistic for the {budget_range} level."
)
)
return (
f"# ✈️ {destination} β€” {trip_days}-Day Trip ({budget_range})\n"
f"{'='*60}\n\n"
f"## πŸ” Agent 1: Research\n{research}\n\n"
f"{'='*60}\n\n"
f"## πŸ—“οΈ Agent 2: Itinerary\n{itinerary}\n\n"
f"{'='*60}\n\n"
f"## πŸ’° Agent 3: Budget Breakdown\n{budget}\n"
)
except Exception as e:
return f"❌ Error: {str(e)}"
# ── Gradio UI ─────────────────────────────────────────────────────
with gr.Blocks(theme=gr.themes.Soft(), title="Travel Planner AI") as demo:
gr.Markdown(
"# ✈️ Travel Planner AI\n"
"Three AI agents work sequentially: **Researcher β†’ Planner β†’ Budget Analyst** "
"(powered by Groq + LLaMA3-70b)"
)
with gr.Row():
with gr.Column(scale=1):
destination = gr.Textbox(label="Destination", placeholder="e.g. Paris, Tokyo, Istanbul")
trip_days = gr.Slider(1, 14, value=5, step=1, label="Number of Days")
budget_range = gr.Radio(["Budget", "Mid-range", "Luxury"], value="Mid-range", label="Budget Style")
plan_btn = gr.Button("πŸ—ΊοΈ Plan My Trip", variant="primary")
with gr.Column(scale=2):
output = gr.Textbox(
label="Your AI-Generated Trip Plan",
lines=35,
placeholder="Your trip plan will appear here once the three agents complete their work..."
)
plan_btn.click(fn=plan_trip, inputs=[destination, trip_days, budget_range], outputs=output)
gr.Markdown(
"> πŸ”‘ Powered by [Groq](https://groq.com) (free). "
"Add your `GROQ_API_KEY` in **Space Settings β†’ Variables and secrets**."
)
demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True)