File size: 14,488 Bytes
735110c
 
 
39dc557
735110c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f43ef9
 
 
39dc557
3f43ef9
39dc557
3f43ef9
 
 
39dc557
 
 
 
 
3f43ef9
 
 
 
 
 
 
 
 
 
39dc557
3f43ef9
39dc557
3f43ef9
 
39dc557
3f43ef9
 
 
39dc557
3f43ef9
 
 
 
 
 
 
 
735110c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f43ef9
735110c
 
 
 
 
 
 
 
 
 
 
 
 
3f43ef9
735110c
 
 
3f43ef9
 
 
 
 
39dc557
 
3f43ef9
 
 
 
 
 
 
 
735110c
3f43ef9
 
 
 
 
735110c
 
 
 
 
 
 
 
 
 
 
 
 
 
3f43ef9
735110c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f43ef9
735110c
3f43ef9
 
735110c
 
 
 
 
 
 
 
 
 
3f43ef9
 
 
39dc557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
735110c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f43ef9
 
 
 
 
 
 
735110c
 
3f43ef9
 
 
39dc557
3f43ef9
 
 
 
 
 
 
 
39dc557
 
735110c
 
39dc557
 
 
3f43ef9
 
 
 
 
 
 
 
735110c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39dc557
3f43ef9
39dc557
 
 
3f43ef9
39dc557
 
 
 
 
3f43ef9
39dc557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f43ef9
735110c
 
 
 
 
 
3f43ef9
735110c
 
 
 
 
 
3f43ef9
735110c
 
 
 
 
 
 
 
3f43ef9
735110c
 
 
 
 
 
 
 
 
 
8ef02ad
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
"""
Entry point for the Tourism Recommendation System Gradio app.
This file should be named 'app.py' for Hugging Face Spaces deployment.
Modified to use proper browser-based location detection.
"""

import gradio as gr
import json
import warnings
import tempfile
import os
from crew import tourismAgent

warnings.filterwarnings("ignore", category=SyntaxWarning, module="pysbd")

def process_location(location_input):
    """
    Process location input to handle both coordinate and descriptive formats.
    """
    if not location_input or location_input.strip() == "":
        return None
    
    location_input = location_input.strip()
    
    # Check if location is in lat,lng format
    if ',' in location_input:
        parts = location_input.split(',')
        if len(parts) == 2:
            try:
                lat = float(parts[0].strip())
                lng = float(parts[1].strip())
                return {
                    "latitude": lat, 
                    "longitude": lng,
                    "source": "manual"
                }
            except ValueError:
                pass
    
    # Treat as descriptive location
    return {
        "description": location_input,
        "source": "manual"
    }

def parse_browser_location(location_data):
    """
    Parse location data received from browser geolocation API.
    Expected format: "lat,lng,accuracy" or JSON string with coordinates
    """
    if not location_data or location_data.strip() == "" or location_data == "null":
        return None
    
    try:
        # Handle error messages
        if location_data.startswith('ERROR:'):
            print(f"Browser location error: {location_data}")
            return None
            
        # Try to parse as JSON first (in case of more detailed data)
        if location_data.startswith('{'):
            data = json.loads(location_data)
            return {
                "latitude": data.get('latitude'),
                "longitude": data.get('longitude'),
                "accuracy": data.get('accuracy'),
                "source": "browser"
            }
        else:
            # Parse as simple "lat,lng,accuracy" format
            parts = location_data.split(',')
            if len(parts) >= 2:
                lat = float(parts[0].strip())
                lng = float(parts[1].strip())
                accuracy = float(parts[2].strip()) if len(parts) > 2 else None
                return {
                    "latitude": lat,
                    "longitude": lng,
                    "accuracy": accuracy,
                    "source": "browser"
                }
    except (json.JSONDecodeError, ValueError, IndexError) as e:
        print(f"Error parsing browser location: {e}")
        return None
    
    return None

def validate_api_key(api_key):
    """
    Validate if the provided API key has the correct format.
    """
    if not api_key or api_key.strip() == "":
        return False, "API key is required"
    
    # Basic Anthropic API key format validation
    api_key = api_key.strip()
    if not api_key.startswith("sk-ant-"):
        return False, "Invalid API key format. Anthropic API keys should start with 'sk-ant-'"
    
    if len(api_key) < 50:
        return False, "API key appears to be too short"
    
    return True, "API key format is valid"

def get_tourism_recommendations(interests, location_input, browser_location, api_key):
    """
    Get tourism recommendations based on user interests and location.
    """
    try:
        # Validate API key first
        is_valid, validation_message = validate_api_key(api_key)
        if not is_valid:
            return f"❌ API Key Error: {validation_message}\n\nPlease enter a valid Anthropic API key to use this service."
        
        # Validate inputs
        if not interests or interests.strip() == "":
            return "❌ Please provide your interests or preferences for places to visit."
        
        # Process location - prioritize browser location over manual input
        location = None
        location_info = ""
        
        # First try browser location
        if browser_location and browser_location.strip():
            browser_loc = parse_browser_location(browser_location)
            if browser_loc:
                location = browser_loc
                accuracy_info = f" (Β±{int(browser_loc.get('accuracy', 0))}m)" if browser_loc.get('accuracy') else ""
                location_info = f"πŸ“ Your location: {browser_loc['latitude']:.4f}, {browser_loc['longitude']:.4f}{accuracy_info}"
        
        # Fallback to manual location input if browser location failed
        if not location and location_input:
            manual_loc = process_location(location_input)
            if manual_loc:
                location = manual_loc
                if 'description' in manual_loc:
                    location_info = f"πŸ“ Using location: {manual_loc['description']}"
                else:
                    location_info = f"πŸ“ Using coordinates: {manual_loc['latitude']:.4f}, {manual_loc['longitude']:.4f}"
        
        # If no location provided
        if not location:
            location_info = "πŸ“ No specific location provided - using general recommendations"
        
        # Prepare inputs for the CrewAI
        inputs = {
            "interests": interests.strip(),
            "location": location,
        }
        
        # Initialize tourism agent with the provided API key
        agent = tourismAgent(api_key=api_key.strip())
        
        # Run the tourism agent
        result = agent.crew().kickoff(inputs=inputs)
        
        # Add location info to the result
        result_with_location = f"{location_info}\n\n{result}"
        
        return result_with_location
        
    except Exception as e:
        error_msg = f"An error occurred while getting recommendations: {str(e)}"
        return f"❌ {error_msg}"

# Create the Gradio interface
with gr.Blocks(
    title="🌍 Tourism Recommendation System",
    theme=gr.themes.Soft(),
    css="""
    .gradio-container {
        max-width: 800px !important;
        margin: 0 auto !important;
    }
    .header {
        text-align: center;
        margin-bottom: 30px;
    }
    .location-section {
        background-color: #e8f4f8;
        padding: 15px;
        border-radius: 8px;
        margin: 10px 0;
        border-left: 4px solid #17a2b8;
    }
    .api-key-section {
        background-color: #fff3cd;
        padding: 15px;
        border-radius: 8px;
        margin: 15px 0;
        border-left: 4px solid #ffc107;
    }
    .location-button {
        margin: 10px 0;
    }
    .status-success {
        color: #28a745;
        background-color: #d4edda;
        border: 1px solid #c3e6cb;
        padding: 10px;
        border-radius: 5px;
        margin: 5px 0;
    }
    .status-error {
        color: #721c24;
        background-color: #f8d7da;
        border: 1px solid #f5c6cb;
        padding: 10px;
        border-radius: 5px;
        margin: 5px 0;
    }
    """
) as demo:
    
    # Header
    gr.Markdown(
        """
        # 🌍 Tourism Recommendation System
        
        Get personalized travel recommendations based on your interests and location!
        Simply describe what kind of places you'd like to visit, and optionally provide your location.
        """,
        elem_classes=["header"]
    )
    
    with gr.Row():
        with gr.Column(scale=1):
            # API Key section
            gr.Markdown("## πŸ”‘ API Configuration")
            
            with gr.Group():
                api_key_input = gr.Textbox(
                    label="Anthropic API Key",
                    placeholder="Enter your Anthropic API key (sk-ant-...)",
                    type="password",
                    info="Your API key is required to use Claude. Get one from: https://console.anthropic.com/"
                )
            
            # Input section
            gr.Markdown("## πŸ“ Your Preferences")
            
            interests_input = gr.Textbox(
                label="What kind of places interest you?",
                placeholder="e.g., historic places, museums, beaches, adventure spots, local cuisine, art galleries...",
                lines=3,
                max_lines=5
            )
            
            # Location options
            gr.Markdown("## πŸ“ Location Settings")
            
            with gr.Group(elem_classes=["location-section"]):
                gr.Markdown(
                    """
                    **🎯 For Best Recommendations:** Use your browser location for accurate, location-based suggestions.
                    
                    Your browser will ask for permission to access your location. This data stays private and is only used for recommendations.
                    """
                )
                
                # Browser location button and status
                with gr.Row():
                    get_location_btn = gr.Button(
                        "πŸ“ Get My Current Location",
                        variant="secondary",
                        elem_classes=["location-button"]
                    )
                
                # Hidden field to store browser location data
                browser_location = gr.Textbox(
                    label="Browser Location Data",
                    visible=False,
                    interactive=False,
                    value=""
                )
                
                location_status = gr.Markdown(
                    "Click 'Get My Current Location' to detect your position using your device's GPS/location services.",
                    elem_classes=["status-info"]
                )
                
                gr.Markdown("**Alternative:** Manually specify your location below:")
                
                location_input = gr.Textbox(
                    label="Manual Location Entry",
                    placeholder="e.g., 'New York, NY' or '40.7128,-74.0060' (latitude,longitude)",
                    info="Enter city name or coordinates. Browser location takes priority if both are provided."
                )
            
            # Submit button
            submit_btn = gr.Button(
                "πŸ” Get Recommendations",
                variant="primary",
                size="lg"
            )
            
        with gr.Column(scale=1):
            # Output section
            gr.Markdown("## 🎯 Recommendations")
            
            output = gr.Textbox(
                label="Your Personalized Recommendations",
                placeholder="Your recommendations will appear here...",
                lines=20,
                max_lines=30,
                show_copy_button=True
            )
    
    # JavaScript function to get location - this runs in the user's browser, not on the server
    get_location_btn.click(
        fn=None,
        inputs=[],
        outputs=[browser_location, location_status],
        js="""
        () => {
            return new Promise((resolve) => {
                if (!navigator.geolocation) {
                    resolve(["", "❌ **Location Error:** Geolocation is not supported by your browser."]);
                    return;
                }
                
                // Show loading status immediately
                const loadingStatus = "πŸ”„ **Getting your location...** Please allow location access when prompted.";
                
                navigator.geolocation.getCurrentPosition(
                    function(position) {
                        const lat = position.coords.latitude;
                        const lng = position.coords.longitude;
                        const accuracy = Math.round(position.coords.accuracy);
                        
                        // Format: lat,lng,accuracy for easy parsing
                        const locationData = `${lat},${lng},${accuracy}`;
                        const statusMsg = `βœ… **Location detected successfully!**\\nπŸ“ Coordinates: ${lat.toFixed(4)}, ${lng.toFixed(4)}\\n🎯 Accuracy: Β±${accuracy} meters`;
                        
                        resolve([locationData, statusMsg]);
                    },
                    function(error) {
                        let errorMsg = "❌ **Location Error:** ";
                        switch(error.code) {
                            case error.PERMISSION_DENIED:
                                errorMsg += "You denied location access. Please enable location permissions in your browser settings and try again.";
                                break;
                            case error.POSITION_UNAVAILABLE:
                                errorMsg += "Location information is unavailable. Please check your GPS/location services.";
                                break;
                            case error.TIMEOUT:
                                errorMsg += "Location request timed out. Please try again.";
                                break;
                            default:
                                errorMsg += "An unknown error occurred while getting your location.";
                                break;
                        }
                        resolve(["", errorMsg]);
                    },
                    {
                        enableHighAccuracy: true,
                        timeout: 15000,  // 15 seconds
                        maximumAge: 300000  // 5 minutes cache
                    }
                );
            });
        }
        """
    )
    
    # Submit button click
    submit_btn.click(
        get_tourism_recommendations,
        inputs=[interests_input, location_input, browser_location, api_key_input],
        outputs=[output]
    )
    
    # Enter key support
    interests_input.submit(
        get_tourism_recommendations,
        inputs=[interests_input, location_input, browser_location, api_key_input],
        outputs=[output]
    )
    
    # Footer
    gr.Markdown(
        """
        ---
        <div style="text-align: center; color: #666;">
            <small>Powered by CrewAI β€’ Built with Gradio β€’ Secure API handling β€’ Browser-based location detection</small>
        </div>
        """
    )

# Launch the interface
if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        debug=True)