Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from crewai import Agent, Task, Crew | |
| from dotenv import load_dotenv | |
| import os | |
| import traceback | |
| import litellm | |
| # Load .env variables | |
| load_dotenv() | |
| # Check for API key | |
| if not os.getenv("GROQ_API_KEY"): | |
| print("❗ Please set GROQ_API_KEY in your .env file") | |
| # LLM config for Groq Cloud (LiteLLM) | |
| llm_config = { | |
| "model": os.getenv("MODEL_NAME", "groq/mixtral-8x7b-32768"), | |
| "api_key": os.getenv("GROQ_API_KEY"), | |
| "base_url": "https://api.groq.com/openai/v1" | |
| } | |
| # Main trip planning function | |
| def plan_trip(destination, duration, interests): | |
| try: | |
| duration = int(duration) | |
| # Agent 1: Travel Expert | |
| researcher = Agent( | |
| role="Travel Expert", | |
| goal=f"Find the best {interests} attractions in {destination}", | |
| backstory="An experienced travel researcher with deep knowledge of global destinations.", | |
| llm=llm_config, | |
| verbose=True | |
| ) | |
| # Agent 2: Trip Planner | |
| planner = Agent( | |
| role="Itinerary Designer", | |
| goal=f"Create a personalized {duration}-day plan for {destination}", | |
| backstory="Expert in crafting memorable travel itineraries without using symbols, only points.", | |
| llm=llm_config, | |
| verbose=True | |
| ) | |
| # Task 1: Research | |
| research_task = Task( | |
| description=f"List 10+ popular {interests} places to visit in {destination} with details.", | |
| expected_output="Attraction list with highlights and reasons to visit, listed clearly or in table format.", | |
| agent=researcher | |
| ) | |
| # Task 2: Plan based on research | |
| plan_task = Task( | |
| description=f"""Based on the research, design a {duration}-day itinerary for {destination} | |
| focusing on {interests}. Include morning, afternoon, and evening plans with meals and transport suggestions. | |
| Use research findings from previous task.""", | |
| expected_output="Detailed day-by-day itinerary.", | |
| agent=planner | |
| ) | |
| # Create crew | |
| crew = Crew( | |
| agents=[researcher, planner], | |
| tasks=[research_task, plan_task], | |
| verbose=True | |
| ) | |
| result = crew.kickoff() | |
| return result | |
| except Exception as e: | |
| error_msg = f"❌ Error: {str(e)}\n\nTraceback:\n{traceback.format_exc()}" | |
| print(error_msg) | |
| return "⚠️ Something went wrong! Please check inputs or API setup." | |
| # Gradio UI | |
| with gr.Blocks(title="✈️ AI Trip Planner") as demo: | |
| gr.Markdown("## ✈️ AI Trip Planner\nGet your personalized travel itinerary below.") | |
| with gr.Row(): | |
| destination = gr.Textbox(label="Destination", placeholder="e.g. Paris", value="Kyoto") | |
| duration = gr.Number(label="Trip Duration (Days)", value=3, minimum=1, maximum=30) | |
| interests = gr.Textbox(label="Your Interests", placeholder="e.g. culture, food, hiking", value="temples, culture, food") | |
| submit_btn = gr.Button("Generate Itinerary", variant="primary") | |
| output = gr.Textbox(label="Your Itinerary", lines=25, interactive=False) | |
| def safe_trip_interface(dest, days, intrs): | |
| if not dest.strip(): | |
| return "⚠️ Please enter a destination" | |
| try: | |
| return plan_trip(dest, days, intrs) | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}" | |
| submit_btn.click( | |
| fn=safe_trip_interface, | |
| inputs=[destination, duration, interests], | |
| outputs=output, | |
| show_progress=True | |
| ) | |
| # Launch App | |
| try: | |
| demo.launch(server_port=7860, show_error=True) | |
| except Exception as e: | |
| print(f"App launch failed: {e}") | |