Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,42 @@
|
|
| 1 |
-
import os
|
|
|
|
| 2 |
from fastapi import FastAPI
|
| 3 |
-
from
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 9 |
AIRTABLE_API_KEY = os.getenv("AIRTABLE_API_KEY")
|
| 10 |
AIRTABLE_BASE_ID = os.getenv("AIRTABLE_BASE_ID")
|
| 11 |
AIRTABLE_TABLE = "Appointments"
|
| 12 |
|
| 13 |
-
# ---- Ephemeral Key ----
|
| 14 |
@app.get("/get-ephemeral-key")
|
| 15 |
def get_ephemeral_key():
|
| 16 |
-
"""Generate an ephemeral key for the client."""
|
| 17 |
url = "https://api.openai.com/v1/realtime/sessions"
|
| 18 |
headers = {
|
| 19 |
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
| 20 |
-
"Content-Type": "application/json"
|
| 21 |
}
|
| 22 |
body = {
|
| 23 |
-
"model": "gpt-4o-realtime-preview"
|
|
|
|
| 24 |
}
|
| 25 |
response = requests.post(url, headers=headers, json=body)
|
| 26 |
data = response.json()
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
return
|
| 30 |
-
|
| 31 |
# ---- Add Appointment ----
|
| 32 |
class Appointment(BaseModel):
|
| 33 |
appointment_id: str
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
from fastapi import FastAPI
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
# Allow frontend access
|
| 9 |
+
app.add_middleware(
|
| 10 |
+
CORSMiddleware,
|
| 11 |
+
allow_origins=["*"], # or your domain only in production
|
| 12 |
+
allow_methods=["*"],
|
| 13 |
+
allow_headers=["*"],
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# 🔑 Environment variables
|
| 17 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 18 |
AIRTABLE_API_KEY = os.getenv("AIRTABLE_API_KEY")
|
| 19 |
AIRTABLE_BASE_ID = os.getenv("AIRTABLE_BASE_ID")
|
| 20 |
AIRTABLE_TABLE = "Appointments"
|
| 21 |
|
| 22 |
+
# ---- Ephemeral Key Endpoint ----
|
| 23 |
@app.get("/get-ephemeral-key")
|
| 24 |
def get_ephemeral_key():
|
| 25 |
+
"""Generate an ephemeral key for the client (valid ~1 min)."""
|
| 26 |
url = "https://api.openai.com/v1/realtime/sessions"
|
| 27 |
headers = {
|
| 28 |
"Authorization": f"Bearer {OPENAI_API_KEY}",
|
| 29 |
+
"Content-Type": "application/json",
|
| 30 |
}
|
| 31 |
body = {
|
| 32 |
+
"model": "gpt-4o-realtime-preview",
|
| 33 |
+
"voice": "verse", # optional, can be "alloy", "verse", etc.
|
| 34 |
}
|
| 35 |
response = requests.post(url, headers=headers, json=body)
|
| 36 |
data = response.json()
|
| 37 |
|
| 38 |
+
# ✅ Return exactly the client_secret
|
| 39 |
+
return data
|
|
|
|
| 40 |
# ---- Add Appointment ----
|
| 41 |
class Appointment(BaseModel):
|
| 42 |
appointment_id: str
|