Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,14 +4,16 @@ from fastapi.responses import FileResponse, StreamingResponse
|
|
| 4 |
from pydantic import BaseModel
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
from
|
| 8 |
from advisor_bot import generate_advice
|
| 9 |
from chatbot_advisor import ask_chatbot
|
| 10 |
from utility import STT, TTS
|
| 11 |
|
| 12 |
-
app = FastAPI()
|
| 13 |
|
| 14 |
-
#
|
|
|
|
|
|
|
|
|
|
| 15 |
app.add_middleware(
|
| 16 |
CORSMiddleware,
|
| 17 |
allow_origins=["*"],
|
|
@@ -20,7 +22,11 @@ app.add_middleware(
|
|
| 20 |
allow_headers=["*"],
|
| 21 |
)
|
| 22 |
|
| 23 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
class CreditRiskInput(BaseModel):
|
| 26 |
age: int
|
|
@@ -56,36 +62,21 @@ class TTSRequest(BaseModel):
|
|
| 56 |
text: str
|
| 57 |
|
| 58 |
|
| 59 |
-
#
|
| 60 |
|
| 61 |
@app.get("/")
|
| 62 |
def root():
|
| 63 |
-
return {"
|
| 64 |
-
|
| 65 |
|
| 66 |
-
@app.get("/home")
|
| 67 |
-
def home():
|
| 68 |
-
return {"message": "Credit Risk API is running."}
|
| 69 |
|
| 70 |
-
|
| 71 |
-
# ---------------- CREDIT RISK ---------------- #
|
| 72 |
|
| 73 |
@app.post("/predict_credit_risk", response_model=CreditRiskOutput)
|
| 74 |
def predict_credit_risk(input_data: CreditRiskInput):
|
| 75 |
try:
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
input_data.loan_amount,
|
| 80 |
-
input_data.loan_tenure_months,
|
| 81 |
-
input_data.avg_dpd_per_delinquency,
|
| 82 |
-
input_data.delinquency_ratio,
|
| 83 |
-
input_data.credit_utilization_ratio,
|
| 84 |
-
input_data.num_open_accounts,
|
| 85 |
-
input_data.residence_type,
|
| 86 |
-
input_data.loan_purpose,
|
| 87 |
-
input_data.loan_type
|
| 88 |
-
)
|
| 89 |
|
| 90 |
advisor_reply = generate_advice(
|
| 91 |
probability=probability,
|
|
@@ -104,7 +95,7 @@ def predict_credit_risk(input_data: CreditRiskInput):
|
|
| 104 |
raise HTTPException(status_code=500, detail=str(e))
|
| 105 |
|
| 106 |
|
| 107 |
-
#
|
| 108 |
|
| 109 |
@app.post("/chat")
|
| 110 |
async def chat(message_data: ChatMessage):
|
|
@@ -123,7 +114,7 @@ async def chat(message_data: ChatMessage):
|
|
| 123 |
return StreamingResponse(event_generator(), media_type="text/plain")
|
| 124 |
|
| 125 |
|
| 126 |
-
#
|
| 127 |
|
| 128 |
@app.post("/tts")
|
| 129 |
async def generate_tts(request: TTSRequest):
|
|
@@ -146,11 +137,11 @@ async def generate_tts(request: TTSRequest):
|
|
| 146 |
raise HTTPException(status_code=500, detail=str(e))
|
| 147 |
|
| 148 |
|
| 149 |
-
#
|
| 150 |
|
| 151 |
@app.post("/stt")
|
| 152 |
async def transcribe_audio(file: UploadFile = File(...)):
|
| 153 |
try:
|
| 154 |
return await STT(file)
|
| 155 |
except Exception as e:
|
| 156 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 4 |
from pydantic import BaseModel
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
from inference.predictor import CreditRiskPredictor
|
| 8 |
from advisor_bot import generate_advice
|
| 9 |
from chatbot_advisor import ask_chatbot
|
| 10 |
from utility import STT, TTS
|
| 11 |
|
|
|
|
| 12 |
|
| 13 |
+
# ================== APP INIT ================== #
|
| 14 |
+
|
| 15 |
+
app = FastAPI(title="RiskGuard AI - Credit Risk Engine")
|
| 16 |
+
|
| 17 |
app.add_middleware(
|
| 18 |
CORSMiddleware,
|
| 19 |
allow_origins=["*"],
|
|
|
|
| 22 |
allow_headers=["*"],
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# ================== LOAD MODEL ON START ================== #
|
| 26 |
+
|
| 27 |
+
predictor = CreditRiskPredictor()
|
| 28 |
+
|
| 29 |
+
# ================== SCHEMAS ================== #
|
| 30 |
|
| 31 |
class CreditRiskInput(BaseModel):
|
| 32 |
age: int
|
|
|
|
| 62 |
text: str
|
| 63 |
|
| 64 |
|
| 65 |
+
# ================== HEALTH ================== #
|
| 66 |
|
| 67 |
@app.get("/")
|
| 68 |
def root():
|
| 69 |
+
return {"status": "RiskGuard AI API is running."}
|
|
|
|
| 70 |
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# ================== CREDIT RISK ================== #
|
|
|
|
| 73 |
|
| 74 |
@app.post("/predict_credit_risk", response_model=CreditRiskOutput)
|
| 75 |
def predict_credit_risk(input_data: CreditRiskInput):
|
| 76 |
try:
|
| 77 |
+
input_dict = input_data.dict()
|
| 78 |
+
|
| 79 |
+
probability, credit_score, rating = predictor.predict(input_dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
advisor_reply = generate_advice(
|
| 82 |
probability=probability,
|
|
|
|
| 95 |
raise HTTPException(status_code=500, detail=str(e))
|
| 96 |
|
| 97 |
|
| 98 |
+
# ================== CHAT STREAM ================== #
|
| 99 |
|
| 100 |
@app.post("/chat")
|
| 101 |
async def chat(message_data: ChatMessage):
|
|
|
|
| 114 |
return StreamingResponse(event_generator(), media_type="text/plain")
|
| 115 |
|
| 116 |
|
| 117 |
+
# ================== TTS ================== #
|
| 118 |
|
| 119 |
@app.post("/tts")
|
| 120 |
async def generate_tts(request: TTSRequest):
|
|
|
|
| 137 |
raise HTTPException(status_code=500, detail=str(e))
|
| 138 |
|
| 139 |
|
| 140 |
+
# ================== STT ================== #
|
| 141 |
|
| 142 |
@app.post("/stt")
|
| 143 |
async def transcribe_audio(file: UploadFile = File(...)):
|
| 144 |
try:
|
| 145 |
return await STT(file)
|
| 146 |
except Exception as e:
|
| 147 |
+
raise HTTPException(status_code=500, detail=str(e))
|