Spaces:
Runtime error
Runtime error
| import os | |
| import requests | |
| from flask import Flask, request, jsonify | |
| from flask_cors import CORS | |
| from datetime import datetime | |
| app = Flask(__name__) | |
| CORS(app) # HTML frontend ko call karne ki permission | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| GROQ_URL = "https://api.groq.com/openai/v1/chat/completions" | |
| def ask_groq(prompt): | |
| if not GROQ_API_KEY: | |
| return "❌ API key missing. Set GROQ_API_KEY in secrets." | |
| headers = { | |
| "Authorization": f"Bearer {GROQ_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| data = { | |
| "model": "llama-3.1-8b-instant", | |
| "messages": [ | |
| {"role": "system", "content": "You are an expert travel planner. Provide detailed itinerary, food tips, budget breakdown."}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| } | |
| try: | |
| resp = requests.post(GROQ_URL, json=data, headers=headers, timeout=15) | |
| resp_json = resp.json() | |
| return resp_json["choices"][0]["message"]["content"] | |
| except Exception as e: | |
| return f"⚠️ AI error: {str(e)}" | |
| def get_weather_html(city): | |
| try: | |
| url = f"https://wttr.in/{city}?format=j1" | |
| r = requests.get(url, timeout=5) | |
| if r.status_code == 200: | |
| data = r.json() | |
| days = data["weather"][:3] | |
| html = "<ul style='margin:0; padding-left:1.2rem;'>" | |
| for d in days: | |
| date = d["date"] | |
| temp = d["avgtempC"] | |
| desc = d["hourly"][4]["weatherDesc"][0]["value"] | |
| html += f"<li><strong>{date}</strong>: {temp}°C, {desc}</li>" | |
| html += "</ul>" | |
| return html | |
| else: | |
| return "<p>⚠️ Weather service unavailable.</p>" | |
| except: | |
| return "<p>⚠️ Could not fetch weather.</p>" | |
| def hotels_html(dest): | |
| return f""" | |
| <p>✅ Recommended platforms: | |
| <a href='https://www.booking.com' target='_blank'>Booking.com</a> | | |
| <a href='https://www.airbnb.com' target='_blank'>Airbnb</a> | |
| </p> | |
| <p>🏨 Popular areas in {dest}: city center, near transport hubs.</p> | |
| """ | |
| def map_html(dest): | |
| return f""" | |
| <iframe | |
| width='100%' height='220' style='border-radius:12px; border:none;' | |
| src='https://www.google.com/maps?q={dest}&output=embed'> | |
| </iframe> | |
| """ | |
| def plan_trip(): | |
| data = request.json | |
| start = data.get("start", "your city") | |
| dest = data.get("destination", "") | |
| start_date = data.get("start_date", "Not specified") | |
| end_date = data.get("end_date", "Not specified") | |
| budget = data.get("budget", "10,000 - 20,000") | |
| activities = data.get("activities", "sightseeing, local food") | |
| travel_style = data.get("travel_style", "Cultural & City") | |
| if not dest: | |
| return jsonify({"error": "Destination missing"}), 400 | |
| prompt = f""" | |
| Plan a trip from {start} to {dest}. | |
| Travel dates: {start_date} to {end_date} | |
| Budget: ₹{budget} | |
| Interests/activities: {activities} | |
| Travel style: {travel_style} | |
| Give me: | |
| - Day-by-day itinerary (5–7 days) | |
| - Must-visit places | |
| - Local food recommendations | |
| - Budget tips | |
| - Any special notes | |
| """ | |
| ai_plan = ask_groq(prompt) | |
| weather_html_content = get_weather_html(dest) | |
| hotels_html_content = hotels_html(dest) | |
| map_html_content = map_html(dest) | |
| return jsonify({ | |
| "plan": ai_plan, | |
| "weather_html": weather_html_content, | |
| "hotels_html": hotels_html_content, | |
| "map_html": map_html_content | |
| }) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) |