-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
71 lines (58 loc) · 1.87 KB
/
Copy pathmain.py
File metadata and controls
71 lines (58 loc) · 1.87 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
import json
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def load_db():
try:
with open("ems_protocols.json", "r") as f:
data = json.load(f)
# Support both old flat format and new nested format
return data.get("protocols", data)
except FileNotFoundError:
print("⚠️ DB not found.")
return {}
PROTOCOL_DB = load_db()
class RadioRequest(BaseModel):
protocol_id: str
mode: str
@app.get("/protocols")
async def get_all_protocols():
return [
{
"id": key,
"title": val["title"],
"category": val.get("category", "Uncategorized")
}
for key, val in PROTOCOL_DB.items()
]
@app.post("/generate-segment")
async def generate_radio_segment(request: RadioRequest):
item = PROTOCOL_DB.get(request.protocol_id)
if not item:
if not PROTOCOL_DB: raise HTTPException(status_code=404, detail="DB Empty")
item = PROTOCOL_DB[next(iter(PROTOCOL_DB))]
# Format the text for better reading
raw_text = item.get("raw_text", "")
# Simple cleanup for the radio script
# Remove excessive newlines
script_body = raw_text.replace("\n", " ").replace(" ", " ")
# Custom Intro
if item.get("category") == "Formulary":
intro = f"Formulary Drug: {item['title']}."
else:
intro = f"You are listening to the {request.mode.upper()} breakdown of {item['title']}."
full_script = f"{intro}\n\n{script_body}"
return {
"title": item["title"],
"mode": request.mode,
"audio_url": "",
"script_text": full_script
}