Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,77 @@
|
|
1 |
-
from
|
|
|
|
|
2 |
from medical_simplifier import MedicalTextSimplifier
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
try:
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
except Exception as e:
|
24 |
-
|
25 |
-
|
26 |
-
@app.route('/', methods=['GET'])
|
27 |
-
def home():
|
28 |
-
return jsonify({
|
29 |
-
'message': 'Medical Text Simplifier API is running.',
|
30 |
-
'usage': 'POST text to /simplify to get simplified medical explanation.'
|
31 |
-
})
|
32 |
|
33 |
-
if __name__ ==
|
34 |
-
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
import uvicorn
|
4 |
from medical_simplifier import MedicalTextSimplifier
|
5 |
+
import logging
|
6 |
|
7 |
+
# Configure logging
|
8 |
+
logging.basicConfig(level=logging.INFO)
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
|
11 |
+
app = FastAPI(
|
12 |
+
title="Medical Text Simplifier API",
|
13 |
+
description="API to simplify medical text by explaining medical terms in plain language",
|
14 |
+
version="1.0.0"
|
15 |
+
)
|
16 |
|
17 |
+
# Initialize the simplifier (this will load the models)
|
18 |
+
try:
|
19 |
+
simplifier = MedicalTextSimplifier()
|
20 |
+
logger.info("Medical text simplifier initialized successfully")
|
21 |
+
except Exception as e:
|
22 |
+
logger.error(f"Failed to initialize simplifier: {e}")
|
23 |
+
simplifier = None
|
24 |
|
25 |
+
class TextInput(BaseModel):
|
26 |
+
text: str
|
27 |
+
|
28 |
+
class SimplificationResponse(BaseModel):
|
29 |
+
original_text: str
|
30 |
+
medical_terms_explained: dict
|
31 |
+
|
32 |
+
@app.get("/")
|
33 |
+
async def root():
|
34 |
+
return {
|
35 |
+
"message": "Medical Text Simplifier API",
|
36 |
+
"status": "running",
|
37 |
+
"endpoints": ["/simplify", "/health"]
|
38 |
+
}
|
39 |
+
|
40 |
+
@app.get("/health")
|
41 |
+
async def health_check():
|
42 |
+
if simplifier is None:
|
43 |
+
raise HTTPException(status_code=503, detail="Model not loaded")
|
44 |
+
return {"status": "healthy", "models_loaded": True}
|
45 |
+
|
46 |
+
@app.post("/simplify", response_model=SimplificationResponse)
|
47 |
+
async def simplify_text(input_data: TextInput):
|
48 |
+
if simplifier is None:
|
49 |
+
raise HTTPException(status_code=503, detail="Model not loaded")
|
50 |
+
|
51 |
+
if not input_data.text.strip():
|
52 |
+
raise HTTPException(status_code=400, detail="Text input cannot be empty")
|
53 |
+
|
54 |
try:
|
55 |
+
# Get medical terms
|
56 |
+
medical_terms = simplifier.identify_medical_terms(input_data.text)
|
57 |
+
|
58 |
+
# Create dictionary to store unique terms and their explanations
|
59 |
+
unique_terms = {}
|
60 |
+
|
61 |
+
for item in medical_terms:
|
62 |
+
term = item['term'].lower() # Convert to lowercase for comparison
|
63 |
+
if term not in unique_terms:
|
64 |
+
explanation = simplifier.generate_simplified_explanation(item['term'], input_data.text)
|
65 |
+
unique_terms[term] = explanation
|
66 |
+
|
67 |
+
return SimplificationResponse(
|
68 |
+
original_text=input_data.text,
|
69 |
+
medical_terms_explained=unique_terms
|
70 |
+
)
|
71 |
+
|
72 |
except Exception as e:
|
73 |
+
logger.error(f"Error processing text: {e}")
|
74 |
+
raise HTTPException(status_code=500, detail="Error processing text")
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
if __name__ == "__main__":
|
77 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|