sachin commited on
Commit
79ebe50
·
1 Parent(s): 1a0c6cf
Files changed (1) hide show
  1. src/server/main.py +17 -8
src/server/main.py CHANGED
@@ -476,30 +476,39 @@ async def extract_text(
476
  logger.error(f"Invalid JSON response from external API: {str(e)}")
477
  raise HTTPException(status_code=500, detail="Invalid response format from external API")
478
 
 
 
 
 
479
  @app.post("/v1/visual_query",
480
  response_model=VisualQueryResponse,
481
  summary="Visual Query with Image",
482
- description="Process a visual query with a text query, image, and language codes provided in a JSON body named 'data'.",
483
  tags=["Chat"],
484
  responses={
485
  200: {"description": "Query response", "model": VisualQueryResponse},
486
  400: {"description": "Invalid query or language codes"},
487
- 422: {"description": "Validation error in request body"},
488
  504: {"description": "Visual query service timeout"}
489
  })
490
  async def visual_query(
491
  request: Request,
492
- data: str = Form(..., description="JSON string containing query, src_lang, and tgt_lang"),
493
  file: UploadFile = File(..., description="Image file to analyze")
494
  ):
495
  # Parse and validate JSON data
496
  try:
497
- import json
498
- visual_query_request = VisualQueryRequest.parse_raw(data)
499
  logger.info(f"Received visual query JSON: {data}")
 
 
 
 
 
 
500
  except Exception as e:
501
- logger.error(f"Failed to parse JSON data: {str(e)}")
502
- raise HTTPException(status_code=422, detail=f"Invalid JSON data: {str(e)}")
503
 
504
  if not visual_query_request.query.strip():
505
  raise HTTPException(status_code=400, detail="Query cannot be empty")
@@ -550,7 +559,7 @@ async def visual_query(
550
  except ValueError as e:
551
  logger.error(f"Invalid JSON response: {str(e)}")
552
  raise HTTPException(status_code=500, detail="Invalid response format from visual query service")
553
-
554
  from enum import Enum
555
 
556
  class SupportedLanguage(str, Enum):
 
476
  logger.error(f"Invalid JSON response from external API: {str(e)}")
477
  raise HTTPException(status_code=500, detail="Invalid response format from external API")
478
 
479
+ from fastapi import FastAPI, File, HTTPException, Request, UploadFile, Form
480
+ from pydantic import ValidationError
481
+ import json
482
+
483
  @app.post("/v1/visual_query",
484
  response_model=VisualQueryResponse,
485
  summary="Visual Query with Image",
486
+ description="Process a visual query with a text query, image, and language codes provided in a JSON body named 'data'. The JSON must contain 'query', 'src_lang', and 'tgt_lang' fields.",
487
  tags=["Chat"],
488
  responses={
489
  200: {"description": "Query response", "model": VisualQueryResponse},
490
  400: {"description": "Invalid query or language codes"},
491
+ 422: {"description": "Validation error in request body or invalid JSON"},
492
  504: {"description": "Visual query service timeout"}
493
  })
494
  async def visual_query(
495
  request: Request,
496
+ data: str = Form(..., description="JSON string containing query, src_lang, and tgt_lang (e.g., {\"query\": \"describe the image\", \"src_lang\": \"en\", \"tgt_lang\": \"en\"})"),
497
  file: UploadFile = File(..., description="Image file to analyze")
498
  ):
499
  # Parse and validate JSON data
500
  try:
501
+ visual_query_request = VisualQueryRequest.model_validate_json(data)
 
502
  logger.info(f"Received visual query JSON: {data}")
503
+ except json.JSONDecodeError as e:
504
+ logger.error(f"Invalid JSON format: {str(e)}")
505
+ raise HTTPException(status_code=422, detail=f"Invalid JSON format: {str(e)}")
506
+ except ValidationError as e:
507
+ logger.error(f"Validation error in JSON data: {str(e)}")
508
+ raise HTTPException(status_code=422, detail=f"Invalid data: {str(e)}")
509
  except Exception as e:
510
+ logger.error(f"Unexpected error parsing JSON data: {str(e)}")
511
+ raise HTTPException(status_code=422, detail=f"Failed to parse JSON data: {str(e)}")
512
 
513
  if not visual_query_request.query.strip():
514
  raise HTTPException(status_code=400, detail="Query cannot be empty")
 
559
  except ValueError as e:
560
  logger.error(f"Invalid JSON response: {str(e)}")
561
  raise HTTPException(status_code=500, detail="Invalid response format from visual query service")
562
+
563
  from enum import Enum
564
 
565
  class SupportedLanguage(str, Enum):