File size: 3,576 Bytes
0e1351c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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>
    """

@app.route("/api/plan", methods=["POST"])
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)