Spaces:
Running
Running
Upload 2 files
Browse files- requirements.txt +88 -0
- travel_analytics.py +130 -0
requirements.txt
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiohappyeyeballs==2.4.0
|
2 |
+
aiohttp==3.10.5
|
3 |
+
aiosignal==1.3.1
|
4 |
+
annotated-types==0.7.0
|
5 |
+
anyio==4.4.0
|
6 |
+
astor==0.8.1
|
7 |
+
attrs==24.2.0
|
8 |
+
beautifulsoup4==4.12.3
|
9 |
+
certifi==2024.7.4
|
10 |
+
charset-normalizer==3.3.2
|
11 |
+
click==8.1.7
|
12 |
+
contourpy==1.2.1
|
13 |
+
cycler==0.12.1
|
14 |
+
Cython==3.0.11
|
15 |
+
decorator==5.1.1
|
16 |
+
deprecation==2.1.0
|
17 |
+
docx==0.2.4
|
18 |
+
Faker==28.1.0
|
19 |
+
fastapi==0.112.2
|
20 |
+
fire==0.6.0
|
21 |
+
fonttools==4.53.1
|
22 |
+
frozenlist==1.4.1
|
23 |
+
gotrue==2.7.0
|
24 |
+
h11==0.14.0
|
25 |
+
h2==4.1.0
|
26 |
+
hpack==4.0.0
|
27 |
+
httpcore==1.0.5
|
28 |
+
httpx==0.27.0
|
29 |
+
hyperframe==6.0.1
|
30 |
+
idna==3.8
|
31 |
+
imageio==2.35.1
|
32 |
+
imgaug==0.4.0
|
33 |
+
kiwisolver==1.4.5
|
34 |
+
lazy_loader==0.4
|
35 |
+
lmdb==1.5.1
|
36 |
+
lxml==5.3.0
|
37 |
+
matplotlib==3.9.2
|
38 |
+
multidict==6.0.5
|
39 |
+
networkx==3.3
|
40 |
+
numpy==1.26.4
|
41 |
+
opencv-contrib-python==4.10.0.84
|
42 |
+
opencv-python==4.10.0.84
|
43 |
+
opt-einsum==3.3.0
|
44 |
+
optional==0.0.1
|
45 |
+
packaging==24.1
|
46 |
+
paddleocr==2.8.1
|
47 |
+
paddlepaddle==2.6.1
|
48 |
+
pandas==2.2.2
|
49 |
+
pdf2image==1.17.0
|
50 |
+
pillow==10.4.0
|
51 |
+
postgrest==0.16.11
|
52 |
+
protobuf==5.27.3
|
53 |
+
pyclipper==1.3.0.post5
|
54 |
+
pydantic==2.8.2
|
55 |
+
pydantic_core==2.20.1
|
56 |
+
pyparsing==3.1.4
|
57 |
+
PyPDF2==3.0.1
|
58 |
+
python-dateutil==2.9.0.post0
|
59 |
+
python-docx==1.1.2
|
60 |
+
python-dotenv==1.0.1
|
61 |
+
pytz==2024.1
|
62 |
+
PyYAML==6.0.2
|
63 |
+
rapidfuzz==3.9.6
|
64 |
+
realtime==2.0.2
|
65 |
+
regex==2024.7.24
|
66 |
+
requests==2.32.3
|
67 |
+
scikit-image==0.24.0
|
68 |
+
scipy==1.14.1
|
69 |
+
setuptools==73.0.1
|
70 |
+
shapely==2.0.6
|
71 |
+
six==1.16.0
|
72 |
+
sniffio==1.3.1
|
73 |
+
soupsieve==2.6
|
74 |
+
starlette==0.38.2
|
75 |
+
storage3==0.7.7
|
76 |
+
StrEnum==0.4.15
|
77 |
+
supabase==2.7.3
|
78 |
+
supafunc==0.5.1
|
79 |
+
termcolor==2.4.0
|
80 |
+
tifffile==2024.8.24
|
81 |
+
tiktoken==0.7.0
|
82 |
+
tqdm==4.66.5
|
83 |
+
typing_extensions==4.12.2
|
84 |
+
tzdata==2024.1
|
85 |
+
urllib3==2.2.2
|
86 |
+
uvicorn==0.30.6
|
87 |
+
websockets==12.0
|
88 |
+
yarl==1.9.4
|
travel_analytics.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from supabase import create_client, Client
|
3 |
+
from typing import List, Dict
|
4 |
+
from statistics import mean
|
5 |
+
from collections import Counter
|
6 |
+
import os
|
7 |
+
from datetime import datetime
|
8 |
+
import json
|
9 |
+
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
# Initialize Supabase client
|
13 |
+
url: str = os.getenv('SUPABASE_URL')
|
14 |
+
key: str = os.getenv('SUPABASE_KEY')
|
15 |
+
supabase: Client = create_client(url, key)
|
16 |
+
|
17 |
+
@app.get("/travel-analytics")
|
18 |
+
async def get_travel_analytics():
|
19 |
+
try:
|
20 |
+
# Fetch data from Supabase
|
21 |
+
response = supabase.table("receipt_radar_structured_data_duplicate_duplicate").select("metadata, total_cost, brand_category").eq('user_id',"4b7fa719-244e-4ae8-9025-651a9a41e43f").execute()
|
22 |
+
|
23 |
+
# Extract metadata from the response
|
24 |
+
metadata_list: List[Dict] = [json.loads(row['metadata']) for row in response.data if row['metadata']]
|
25 |
+
total_costs = [float(row['total_cost']) for row in response.data if row.get('total_cost')]
|
26 |
+
print(metadata_list)
|
27 |
+
print(total_costs)
|
28 |
+
# Initialize variables for analytics
|
29 |
+
total_trips = len(metadata_list)
|
30 |
+
trip_durations = []
|
31 |
+
domestic_trips = 0
|
32 |
+
international_trips = 0
|
33 |
+
destination_types = Counter()
|
34 |
+
booking_lead_times = []
|
35 |
+
accommodation_spending = []
|
36 |
+
transport_spending = []
|
37 |
+
activities_spending = []
|
38 |
+
domestic_departure_countries = Counter()
|
39 |
+
international_departure_countries = Counter()
|
40 |
+
domestic_arrival_countries = Counter()
|
41 |
+
international_arrival_countries = Counter()
|
42 |
+
all_dates = []
|
43 |
+
|
44 |
+
# Process each metadata entry
|
45 |
+
for metadata in metadata_list:
|
46 |
+
# Trip duration
|
47 |
+
if 'check_in_date' in metadata and 'check_out_date' in metadata:
|
48 |
+
print("inside 1st")
|
49 |
+
check_in_date = metadata['check_in_date']
|
50 |
+
check_out_date = metadata['check_out_date']
|
51 |
+
duration = (check_out_date - check_in_date).days
|
52 |
+
trip_durations.append(duration)
|
53 |
+
all_dates.append(check_in_date)
|
54 |
+
all_dates.append(check_out_date)
|
55 |
+
|
56 |
+
# Domestic vs International
|
57 |
+
if 'departure_country' in metadata and 'arrival_country' in metadata:
|
58 |
+
print("inside 2nd")
|
59 |
+
departure_country = metadata['departure_country']
|
60 |
+
arrival_country = metadata['arrival_country']
|
61 |
+
|
62 |
+
if departure_country == arrival_country:
|
63 |
+
print("inside 2nd")
|
64 |
+
domestic_trips += 1
|
65 |
+
domestic_departure_countries[departure_country] += 1
|
66 |
+
domestic_arrival_countries[arrival_country] += 1
|
67 |
+
else:
|
68 |
+
international_trips += 1
|
69 |
+
international_departure_countries[departure_country] += 1
|
70 |
+
international_arrival_countries[arrival_country] += 1
|
71 |
+
|
72 |
+
# Destination type (simplified)
|
73 |
+
if 'destination_type' in metadata:
|
74 |
+
print("inside 3rd")
|
75 |
+
destination_types[metadata['destination_type']] += 1
|
76 |
+
|
77 |
+
# Booking lead time
|
78 |
+
if 'date_of_purchase' in metadata and 'departure_date' in metadata:
|
79 |
+
print("inside 4th")
|
80 |
+
lead_time = (datetime.strptime(metadata['departure_date'],"%d-%m-%Y") - datetime.strptime(metadata['date_of_purchase'],"%d-%m-%Y")).days
|
81 |
+
booking_lead_times.append(lead_time)
|
82 |
+
|
83 |
+
# Spending
|
84 |
+
if 'accommodation_cost' in metadata:
|
85 |
+
accommodation_spending.append(metadata['accommodation_cost'])
|
86 |
+
if 'transport_cost' in metadata:
|
87 |
+
transport_spending.append(metadata['transport_cost'])
|
88 |
+
if 'activities_cost' in metadata:
|
89 |
+
activities_spending.append(metadata['activities_cost'])
|
90 |
+
|
91 |
+
# Calculate the number of years covered by the data
|
92 |
+
if all_dates:
|
93 |
+
min_date = min(all_dates)
|
94 |
+
max_date = max(all_dates)
|
95 |
+
date_range_years = (max_date - min_date).days / 365.25
|
96 |
+
else:
|
97 |
+
date_range_years = 1 # Default to 1 year if no dates are available
|
98 |
+
|
99 |
+
# Calculate analytics
|
100 |
+
analytics = {
|
101 |
+
"travel_frequency": {
|
102 |
+
"trips_per_year": total_trips / date_range_years,
|
103 |
+
"average_trip_duration": mean(trip_durations) if trip_durations else None,
|
104 |
+
"domestic_vs_international": f"{domestic_trips}:{international_trips}",
|
105 |
+
"domestic_departure_countries": dict(domestic_departure_countries),
|
106 |
+
"international_departure_countries": dict(international_departure_countries),
|
107 |
+
"domestic_arrival_countries": dict(domestic_arrival_countries),
|
108 |
+
"international_arrival_countries": dict(international_arrival_countries),
|
109 |
+
},
|
110 |
+
"destination_preferences": {
|
111 |
+
"popular_types": dict(destination_types.most_common(5))
|
112 |
+
},
|
113 |
+
"booking_patterns": {
|
114 |
+
"average_lead_time": mean(booking_lead_times) if booking_lead_times else None
|
115 |
+
},
|
116 |
+
"travel_expenditure": {
|
117 |
+
"average_accommodation_cost": mean(accommodation_spending) if accommodation_spending else None,
|
118 |
+
"average_transport_cost": mean(transport_spending) if transport_spending else None,
|
119 |
+
"average_activities_cost": mean(activities_spending) if activities_spending else None
|
120 |
+
}
|
121 |
+
}
|
122 |
+
|
123 |
+
return analytics
|
124 |
+
|
125 |
+
except Exception as e:
|
126 |
+
raise HTTPException(status_code=500, detail=str(e))
|
127 |
+
|
128 |
+
# if __name__ == "__main__":
|
129 |
+
# import uvicorn
|
130 |
+
# uvicorn.run(app, host="0.0.0.0", port=8000,reload="--reload")
|