-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.py
83 lines (69 loc) · 2.52 KB
/
app.py
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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import httpx
from pydantic import BaseModel
import os
from dotenv import load_dotenv
import random
app = FastAPI()
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Load environment variables
load_dotenv()
# Get API key from environment variable
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:
raise ValueError("OPENAI_API_KEY not found in environment variables")
class SessionResponse(BaseModel):
session_id: str
token: str
class WeatherResponse(BaseModel):
temperature: float
unit: str
@app.get("/session")
async def get_session():
async with httpx.AsyncClient() as client:
response = await client.post(
'https://api.openai.com/v1/realtime/sessions',
headers={
'Authorization': f'Bearer {OPENAI_API_KEY}',
'Content-Type': 'application/json'
},
json={
"model": "gpt-4o-realtime-preview-2024-12-17",
"voice": "echo"
}
)
return response.json()
@app.get("/weather/{location}")
async def get_weather(location: str):
# First get coordinates for the location
try:
async with httpx.AsyncClient() as client:
# Get coordinates for location
geocoding_response = await client.get(
f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=1"
)
geocoding_data = geocoding_response.json()
if not geocoding_data.get("results"):
return {"error": f"Could not find coordinates for {location}"}
lat = geocoding_data["results"][0]["latitude"]
lon = geocoding_data["results"][0]["longitude"]
# Get weather data
weather_response = await client.get(
f"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t=temperature_2m"
)
weather_data = weather_response.json()
temperature = weather_data["current"]["temperature_2m"]
return WeatherResponse(temperature=temperature, unit="celsius")
except Exception as e:
return {"error": f"Could not get weather data: {str(e)}"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8888)