faisalshah012003 commited on
Commit
5441c26
·
verified ·
1 Parent(s): 76e63cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -26
app.py CHANGED
@@ -1,34 +1,77 @@
1
- from flask import Flask, request, jsonify
 
 
2
  from medical_simplifier import MedicalTextSimplifier
 
3
 
4
- # Initialize Flask app
5
- app = Flask(__name__)
 
6
 
7
- # Initialize your simplifier once at startup
8
- simplifier = MedicalTextSimplifier()
 
 
 
9
 
10
- @app.route('/simplify', methods=['POST'])
11
- def simplify():
12
- data = request.get_json()
13
- if not data or 'text' not in data:
14
- return jsonify({'error': 'Missing "text" in request body'}), 400
 
 
15
 
16
- text = data['text']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  try:
18
- simplified = simplifier.simplify_text(text)
19
- return jsonify({
20
- 'original_text': text,
21
- 'simplified_text': simplified
22
- })
 
 
 
 
 
 
 
 
 
 
 
 
23
  except Exception as e:
24
- return jsonify({'error': str(e)}), 500
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__ == '__main__':
34
- app.run(host='0.0.0.0', port=50010)
 
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)