Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,147 +0,0 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException, UploadFile, File
|
| 2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
from fastapi.responses import FileResponse, StreamingResponse
|
| 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=["*"],
|
| 20 |
-
allow_credentials=True,
|
| 21 |
-
allow_methods=["*"],
|
| 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
|
| 33 |
-
income: float
|
| 34 |
-
loan_amount: float
|
| 35 |
-
loan_tenure_months: int
|
| 36 |
-
avg_dpd_per_delinquency: float
|
| 37 |
-
delinquency_ratio: float
|
| 38 |
-
credit_utilization_ratio: float
|
| 39 |
-
num_open_accounts: int
|
| 40 |
-
residence_type: str
|
| 41 |
-
loan_purpose: str
|
| 42 |
-
loan_type: str
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
class CreditRiskOutput(BaseModel):
|
| 46 |
-
probability: float
|
| 47 |
-
credit_score: int
|
| 48 |
-
rating: str
|
| 49 |
-
advisor_response: str | None = None
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
class ChatMessage(BaseModel):
|
| 53 |
-
thread_id: str
|
| 54 |
-
message: str
|
| 55 |
-
probability: float
|
| 56 |
-
credit_score: int
|
| 57 |
-
rating: str
|
| 58 |
-
advisor_reply: str
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
class TTSRequest(BaseModel):
|
| 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,
|
| 83 |
-
credit_score=credit_score,
|
| 84 |
-
rating=rating
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
return CreditRiskOutput(
|
| 88 |
-
probability=probability,
|
| 89 |
-
credit_score=credit_score,
|
| 90 |
-
rating=rating,
|
| 91 |
-
advisor_response=advisor_reply
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
except Exception as e:
|
| 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):
|
| 102 |
-
|
| 103 |
-
async def event_generator():
|
| 104 |
-
for chunk in ask_chatbot(
|
| 105 |
-
message_data.probability,
|
| 106 |
-
message_data.credit_score,
|
| 107 |
-
message_data.rating,
|
| 108 |
-
message_data.advisor_reply,
|
| 109 |
-
message_data.message,
|
| 110 |
-
message_data.thread_id
|
| 111 |
-
):
|
| 112 |
-
yield chunk
|
| 113 |
-
|
| 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):
|
| 121 |
-
try:
|
| 122 |
-
if not request.text.strip():
|
| 123 |
-
raise HTTPException(status_code=400, detail="Text is empty")
|
| 124 |
-
|
| 125 |
-
audio_path = await TTS(text=request.text)
|
| 126 |
-
|
| 127 |
-
if not os.path.exists(audio_path):
|
| 128 |
-
raise HTTPException(status_code=500, detail="Audio file not created")
|
| 129 |
-
|
| 130 |
-
return FileResponse(
|
| 131 |
-
path=audio_path,
|
| 132 |
-
media_type="audio/mpeg",
|
| 133 |
-
filename="speech.mp3"
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
except Exception as e:
|
| 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))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|