Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,15 @@
|
|
| 1 |
-
from
|
|
|
|
|
|
|
|
|
|
| 2 |
from pydantic import BaseModel, Field
|
| 3 |
from typing import List, Optional
|
| 4 |
from datetime import datetime
|
| 5 |
import uvicorn
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
|
| 8 |
-
app =
|
| 9 |
|
| 10 |
# In-memory store for historical scores (replace with database for production)
|
| 11 |
historical_scores = []
|
|
@@ -41,9 +45,12 @@ WEIGHTS = {
|
|
| 41 |
"communication": 0.2
|
| 42 |
}
|
| 43 |
|
| 44 |
-
|
| 45 |
-
async def calculate_score(logs: VendorLog):
|
| 46 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
# Validate month format
|
| 48 |
datetime.strptime(logs.month, "%Y-%m")
|
| 49 |
|
|
@@ -91,28 +98,44 @@ async def calculate_score(logs: VendorLog):
|
|
| 91 |
# Mock certification URL (replace with actual logic)
|
| 92 |
certification_url = f"https://example.com/cert/{logs.vendor_id}"
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
except ValueError as e:
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
except Exception as e:
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
#
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
return {"status": "healthy"}
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 1 |
+
from starlette.applications import Starlette
|
| 2 |
+
from starlette.routing import Route
|
| 3 |
+
from starlette.requests import Request
|
| 4 |
+
from starlette.responses import JSONResponse
|
| 5 |
from pydantic import BaseModel, Field
|
| 6 |
from typing import List, Optional
|
| 7 |
from datetime import datetime
|
| 8 |
import uvicorn
|
| 9 |
import numpy as np
|
| 10 |
+
import json
|
| 11 |
|
| 12 |
+
app = Starlette(debug=True)
|
| 13 |
|
| 14 |
# In-memory store for historical scores (replace with database for production)
|
| 15 |
historical_scores = []
|
|
|
|
| 45 |
"communication": 0.2
|
| 46 |
}
|
| 47 |
|
| 48 |
+
async def calculate_score(request: Request):
|
|
|
|
| 49 |
try:
|
| 50 |
+
# Parse JSON body
|
| 51 |
+
body = await request.json()
|
| 52 |
+
logs = VendorLog(**body)
|
| 53 |
+
|
| 54 |
# Validate month format
|
| 55 |
datetime.strptime(logs.month, "%Y-%m")
|
| 56 |
|
|
|
|
| 98 |
# Mock certification URL (replace with actual logic)
|
| 99 |
certification_url = f"https://example.com/cert/{logs.vendor_id}"
|
| 100 |
|
| 101 |
+
# Prepare response
|
| 102 |
+
response = ScoreResponse(
|
| 103 |
+
vendor_id=logs.vendor_id,
|
| 104 |
+
month=logs.month,
|
| 105 |
+
final_score=round(final_score, 2),
|
| 106 |
+
quality_score=round(quality_score, 2),
|
| 107 |
+
timeliness_score=round(timeliness_score, 2),
|
| 108 |
+
safety_score=round(safety_score, 2),
|
| 109 |
+
communication_score=round(communication_score, 2),
|
| 110 |
+
alert_flag=alert_flag,
|
| 111 |
+
trend_deviation=round(trend_deviation, 2) if trend_deviation is not None else None,
|
| 112 |
+
certification_url=certification_url
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
return JSONResponse(content=response.dict())
|
| 116 |
|
| 117 |
except ValueError as e:
|
| 118 |
+
return JSONResponse(
|
| 119 |
+
content={"detail": f"Invalid input: {str(e)}"},
|
| 120 |
+
status_code=400
|
| 121 |
+
)
|
| 122 |
+
except json.JSONDecodeError:
|
| 123 |
+
return JSONResponse(
|
| 124 |
+
content={"detail": "Invalid JSON format"},
|
| 125 |
+
status_code=400
|
| 126 |
+
)
|
| 127 |
except Exception as e:
|
| 128 |
+
return JSONResponse(
|
| 129 |
+
content={"detail": f"Internal server error: {str(e)}"},
|
| 130 |
+
status_code=500
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
async def health_check(request: Request):
|
| 134 |
+
return JSONResponse(content={"status": "healthy"})
|
| 135 |
|
| 136 |
+
# Define routes
|
| 137 |
+
app.routes.append(Route("/score", endpoint=calculate_score, methods=["POST"]))
|
| 138 |
+
app.routes.append(Route("/health", endpoint=health_check, methods=["GET"]))
|
|
|
|
| 139 |
|
| 140 |
if __name__ == "__main__":
|
| 141 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|