-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
79 lines (64 loc) · 2.3 KB
/
main.py
File metadata and controls
79 lines (64 loc) · 2.3 KB
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
import os
import random
from functools import reduce
import modal
import fastapi
from fastapi.staticfiles import StaticFiles
from fastapi.responses import StreamingResponse
import googlemaps
import pastforward
static_dir = "static"
static_dir_remote = f"/root/{static_dir}"
gmp_api_key = "GMP_API_KEY"
image = (
modal.Image.debian_slim()
.pip_install("fastapi[standard]", "googlemaps")
.add_local_python_source("pastforward")
.add_local_dir(static_dir, remote_path=static_dir_remote)
)
app = modal.App(image=image)
def generate_historical_significance(place: pastforward.PastforwardPlace) -> str:
# TODO: Implement https://app.toolhouse.ai/store/web_search
return f"{place.name} is historically significant" if random.random() < 0.5 else ""
@app.function(secrets=[modal.Secret.from_name(gmp_api_key)])
@modal.asgi_app()
def main():
fastapi_app = fastapi.FastAPI()
gmaps = googlemaps.Client(key=os.environ[gmp_api_key])
@fastapi_app.get("/places")
async def places(_r: fastapi.Request, coordinates: str):
gmp_places_nearby_results = gmaps.places_nearby(
location=parse_coordinates(coordinates),
type="point_of_interest",
language="en",
rank_by="distance",
)["results"]
def reduce_places(results, gmp_place):
place = pastforward.from_gmp_place(gmp_place)
historical_significance = generate_historical_significance(place=place)
if historical_significance == "":
return results
return results + [place]
# TODO: Filter places by historical significance via Groq and Toolhouse.
return reduce(reduce_places, gmp_places_nearby_results, [])
@fastapi_app.get("/places/{ref}.jpg")
async def places_photo(_r: fastapi.Request, ref: str):
return StreamingResponse(
gmaps.places_photo(
ref,
{"maxwidth": 400},
),
media_type="image/jpeg",
)
fastapi_app.mount(
"/",
StaticFiles(directory=static_dir_remote, html=True),
name=static_dir,
)
return fastapi_app
def parse_coordinates(s):
try:
lat, lon = map(float, s.replace(" ", "").split(","))
return lat, lon
except (ValueError, AttributeError):
return None