Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231 unmodified line125126127128129130131132133134135136137138139import tracebackfrom fastapi import FastAPIfrom fastapi.middleware.cors import CORSMiddlewarefrom pydantic import BaseModelfrom rag_inference import answer_queryfrom encryption_utils import encrypt_message, decrypt_messageapp = FastAPI(title="Court Companion Chatbot API",description="AI-powered Pakistani criminal law assistant",version="1.0.0",)# ============================================================# CORS# ============================================================app.add_middleware(CORSMiddleware,allow_origins=["*"],allow_credentials=True,allow_methods=["*"],allow_headers=["*"],)# ============================================================# Request Model# ============================================================class QueryRequest(BaseModel):question: str# ============================================================# Root# ============================================================@app.get("/")def root():return {"status": "ok","message": "Court Companion Chatbot API is running",}# ============================================================# Health# ============================================================@app.get("/health")def health():return {"status": "healthy",}# ============================================================# Ask# ============================================================@app.post("/ask")def ask(req: QueryRequest):try:raw_input = req.questionif not raw_input or not raw_input.strip():return {"answer": "ERROR: Question cannot be empty."}print("Received question.")# ----------------------------------------------------# Try to decrypt the incoming question.# If it is not encrypted, use it as normal text.# ----------------------------------------------------try:decrypted_question = decrypt_message(raw_input)print("Input decrypted successfully.")except Exception:decrypted_question = raw_inputprint("Input was not encrypted. Using plain text.")# ----------------------------------------------------# Generate answer using RAG# ----------------------------------------------------answer = answer_query(decrypted_question)if not answer:answer = "No answer could be generated."answer = str(answer).encode("utf-8",errors="ignore",).decode("utf-8")print("Answer generated successfully.")# ----------------------------------------------------# Encrypt response# ----------------------------------------------------try:encrypted_answer = encrypt_message(answer)except Exception:print("Encryption failed. Returning plain answer.")encrypted_answer = answerreturn {"answer": encrypted_answer}except Exception as e:traceback.print_exc()return {1 unmodified line}# ============================================================# Hugging Face Spaces entry point# ============================================================if __name__ == "__main__":import uvicornuvicorn.run(app,host="0.0.0.0",port=7860,)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172731 unmodified line757677787980818283848586import spacesimport tracebackfrom fastapi import FastAPIfrom pydantic import BaseModelfrom rag_inference import answer_queryfrom encryption_utils import encrypt_message, decrypt_messageapp = FastAPI()class QueryRequest(BaseModel):question: str@app.get("/")def root():return {"status": "ok","message": "Court Companion API is running"}@app.get("/health")def health():return {"status": "healthy"}@spaces.GPUdef run_chat(question: str):answer = answer_query(question)return answer@app.post("/ask")def ask(req: QueryRequest):try:question = req.question.strip()if not question:return {"answer": "ERROR: Question cannot be empty."}# Try encrypted input firsttry:question = decrypt_message(question)except Exception:passanswer = run_chat(question)try:answer = encrypt_message(answer)except Exception:passreturn {"answer": answer}except Exception as e:traceback.print_exc()return {1 unmodified line}if __name__ == "__main__":import uvicornuvicorn.run(app,host="0.0.0.0",port=7860)
app.py+24-77