Spaces:
Sleeping
Sleeping
Update app.py
Browse files123445678910111213141511121318192021222324252627282930313233341415161739404142181920214722232425515253542627285829303132626364333435363738394041424344694546714748495051767778798081825253848554558788899091929394959697565799100101102103104105106107108585960111616211311463646511766676869707172731 unmodified line757677128129130131787980818283841388586import spacesimport traceback
from fastapi import FastAPIfrom fastapi.middleware.cors import CORSMiddlewarefrom pydantic import BaseModel
from rag_inference import answer_queryfrom encryption_utils import encrypt_message, decrypt_message
app = FastAPI( title="Court Companion Chatbot API", description="AI-powered Pakistani criminal law assistant", version="1.0.0",)app = FastAPI()
# ============================================================# 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", "message": "Court Companion API is running" }
# ============================================================# Health# ============================================================
@app.get("/health")def health(): return { "status": "healthy", "status": "healthy" }
# ============================================================# Ask# ============================================================@spaces.GPUdef run_chat(question: str):
answer = answer_query(question)
return answer
@app.post("/ask")def ask(req: QueryRequest):
try: raw_input = req.question question = req.question.strip()
if not raw_input or not raw_input.strip(): if not question: 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 encrypted input first try: decrypted_question = decrypt_message(raw_input) print("Input decrypted successfully.") question = decrypt_message(question) except Exception: decrypted_question = raw_input print("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." pass
answer = str(answer).encode( "utf-8", errors="ignore", ).decode("utf-8")
print("Answer generated successfully.")
# ---------------------------------------------------- # Encrypt response # ---------------------------------------------------- answer = run_chat(question)
try: encrypted_answer = encrypt_message(answer) answer = encrypt_message(answer) except Exception: print("Encryption failed. Returning plain answer.") encrypted_answer = answer pass
return { "answer": encrypted_answer "answer": answer }
except Exception as e:
traceback.print_exc()
return {1 unmodified line }
# ============================================================# Hugging Face Spaces entry point# ============================================================
if __name__ == "__main__":
import uvicorn
uvicorn.run( app, host="0.0.0.0", port=7860, port=7860 )app.py+24-77