rohannsinghal commited on
Commit
ac1208e
Β·
1 Parent(s): c4e3eaf

changes to main_api.py

Browse files
Files changed (1) hide show
  1. app/main_api.py +28 -14
app/main_api.py CHANGED
@@ -695,20 +695,34 @@ async def startup_event():
695
  logger.error(f"πŸ’₯ FATAL ERROR: {e}")
696
  raise e
697
 
698
- # --- API MODELS ---
699
 
700
  class SubmissionRequest(BaseModel):
701
  documents: List[str]
702
  questions: List[str]
703
 
704
- class Answer(BaseModel):
705
- question: str
706
- answer: str
 
 
 
 
707
 
708
  class SubmissionResponse(BaseModel):
709
- answers: List[Answer]
 
 
 
 
 
 
 
 
 
 
710
 
711
- # --- MAIN ENDPOINT WITH AUTHENTICATION ---
712
 
713
  @app.post("/hackrx/run", response_model=SubmissionResponse, dependencies=[Depends(verify_bearer_token)])
714
  async def run_submission(request: Request, submission_request: SubmissionRequest = Body(...)):
@@ -752,9 +766,9 @@ async def run_submission(request: Request, submission_request: SubmissionRequest
752
 
753
  if not all_chunks:
754
  logger.error("❌ No chunks processed!")
 
755
  return SubmissionResponse(answers=[
756
- Answer(question=q, answer="Document processing failed.")
757
- for q in submission_request.questions
758
  ])
759
 
760
  # Add to semantic RAG pipeline
@@ -762,24 +776,24 @@ async def run_submission(request: Request, submission_request: SubmissionRequest
762
 
763
  # Answer questions with semantic understanding
764
  logger.info(f"❓ Answering questions with optimized semantics...")
765
- answers = []
766
 
767
  for question in submission_request.questions:
768
  try:
769
  answer = await rag_pipeline.answer_question(question)
770
- answers.append(Answer(question=question, answer=answer))
771
  except Exception as e:
772
  logger.error(f"❌ Question failed: {e}")
773
- answers.append(Answer(question=question, answer="Failed to process question."))
774
 
775
  logger.info("πŸŽ‰ All semantic questions processed!")
776
- return SubmissionResponse(answers=answers)
777
 
778
  except Exception as e:
779
  logger.error(f"πŸ’₯ CRITICAL ERROR: {e}")
 
780
  return SubmissionResponse(answers=[
781
- Answer(question=q, answer=f"System error: {str(e)}")
782
- for q in submission_request.questions
783
  ])
784
 
785
  @app.get("/")
 
695
  logger.error(f"πŸ’₯ FATAL ERROR: {e}")
696
  raise e
697
 
698
+ # --- API MODELS (FIXED FOR HACKATHON) ---
699
 
700
  class SubmissionRequest(BaseModel):
701
  documents: List[str]
702
  questions: List[str]
703
 
704
+ class Config:
705
+ schema_extra = {
706
+ "example": {
707
+ "documents": ["https://example.com/document1.pdf"],
708
+ "questions": ["What is the grace period?", "What are the exclusions?"]
709
+ }
710
+ }
711
 
712
  class SubmissionResponse(BaseModel):
713
+ answers: List[str] # βœ… Fixed: Just strings, not objects
714
+
715
+ class Config:
716
+ schema_extra = {
717
+ "example": {
718
+ "answers": [
719
+ "The grace period is 30 days for premium payment.",
720
+ "The main exclusions include pre-existing diseases for 36 months."
721
+ ]
722
+ }
723
+ }
724
 
725
+ # --- MAIN ENDPOINT WITH AUTHENTICATION (FIXED FORMAT) ---
726
 
727
  @app.post("/hackrx/run", response_model=SubmissionResponse, dependencies=[Depends(verify_bearer_token)])
728
  async def run_submission(request: Request, submission_request: SubmissionRequest = Body(...)):
 
766
 
767
  if not all_chunks:
768
  logger.error("❌ No chunks processed!")
769
+ # βœ… Fixed: Return just strings
770
  return SubmissionResponse(answers=[
771
+ "Document processing failed." for _ in submission_request.questions
 
772
  ])
773
 
774
  # Add to semantic RAG pipeline
 
776
 
777
  # Answer questions with semantic understanding
778
  logger.info(f"❓ Answering questions with optimized semantics...")
779
+ answers = [] # βœ… Fixed: Just collect string answers
780
 
781
  for question in submission_request.questions:
782
  try:
783
  answer = await rag_pipeline.answer_question(question)
784
+ answers.append(answer) # βœ… Fixed: Just append the string answer
785
  except Exception as e:
786
  logger.error(f"❌ Question failed: {e}")
787
+ answers.append("Failed to process question.") # βœ… Fixed: Just string
788
 
789
  logger.info("πŸŽ‰ All semantic questions processed!")
790
+ return SubmissionResponse(answers=answers) # βœ… Fixed: Just the string list
791
 
792
  except Exception as e:
793
  logger.error(f"πŸ’₯ CRITICAL ERROR: {e}")
794
+ # βœ… Fixed: Return just strings
795
  return SubmissionResponse(answers=[
796
+ f"System error: {str(e)}" for _ in submission_request.questions
 
797
  ])
798
 
799
  @app.get("/")