This commit is contained in:
smfahim25 2025-04-10 11:16:52 +06:00
parent dcf83bbf81
commit bc30973ae5

View file

@ -497,53 +497,109 @@ async def check_facts(request: FactCheckRequest):
""" """
Fetch fact check results and generate a comprehensive report. Fetch fact check results and generate a comprehensive report.
Handles both query-based and URL-based fact checking. Handles both query-based and URL-based fact checking.
Always returns a 200 response with appropriate content, never an error.
""" """
url_text = None try:
query_result = None url_text = None
url_result = None query_result = None
url_result = None
# If URL is provided, try to extract text # If URL is provided, try to extract text
if request.url: if request.url:
url_text = await process_url_content(request.url) try:
if not url_text and not request.query: url_text = await process_url_content(request.url)
# Only return early if URL text extraction failed and no query provided except Exception as e:
return UnverifiedFactCheckResponse( logger.error(f"Error extracting text from URL: {str(e)}")
claim=f"URL check requested: {request.url}", url_text = None
verdict=VerdictEnum.UNVERIFIED,
confidence=ConfidenceEnum.LOW, if not url_text and not request.query:
sources=[], # Only return early if URL text extraction failed and no query provided
evidence="Unable to extract text from the provided URL.", return UnverifiedFactCheckResponse(
explanation="The system could not process the content from the provided URL. The URL might be invalid or inaccessible.", claim=f"URL check requested: {request.url}",
additional_context="Please provide a valid URL or a text query for fact-checking." verdict=VerdictEnum.UNVERIFIED,
) confidence=ConfidenceEnum.LOW,
sources=[],
# If URL text was successfully extracted, process it evidence="No fact check results found",
if url_text: explanation="The system encountered errors while processing the fact checks.",
logger.info(f"Processing fact check for extracted text: {url_text}") additional_context="Please try again with different input or contact support if the issue persists."
url_result = await process_fact_check(url_text) )
# If URL text was successfully extracted, process it
if url_text:
logger.info(f"Processing fact check for extracted text: {url_text}")
try:
url_result = await process_fact_check(url_text)
except Exception as e:
logger.error(f"Error processing fact check for URL text: {str(e)}")
url_result = UnverifiedFactCheckResponse(
claim=f"URL: {request.url}",
verdict=VerdictEnum.UNVERIFIED,
confidence=ConfidenceEnum.LOW,
sources=[],
evidence="No fact check results found",
explanation="The system encountered errors while processing the fact checks.",
additional_context="Please try again with different input or contact support if the issue persists."
)
# Process query if provided # Process query if provided
if request.query: if request.query:
query_result = await process_fact_check(request.query) try:
query_result = await process_fact_check(request.query)
except Exception as e:
logger.error(f"Error processing fact check for query: {str(e)}")
query_result = UnverifiedFactCheckResponse(
claim=request.query,
verdict=VerdictEnum.UNVERIFIED,
confidence=ConfidenceEnum.LOW,
sources=[],
evidence="No fact check results found",
explanation="The system encountered errors while processing the fact checks.",
additional_context="Please try again with different input or contact support if the issue persists."
)
# If both results are available, combine them # If both results are available, combine them
if query_result and url_result and url_text: if query_result and url_result and url_text:
return await combine_fact_reports(request.query, url_text, try:
query_result.dict(), url_result.dict()) return await combine_fact_reports(request.query, url_text,
query_result.dict(), url_result.dict())
except Exception as e:
logger.error(f"Error combining fact reports: {str(e)}")
return UnverifiedFactCheckResponse(
claim=request.query or f"URL: {request.url}",
verdict=VerdictEnum.UNVERIFIED,
confidence=ConfidenceEnum.LOW,
sources=[],
evidence="No fact check results found",
explanation="The system encountered errors while processing the fact checks.",
additional_context="Please try again with different input or contact support if the issue persists."
)
# If only one result is available # If only one result is available
if query_result: if query_result:
return query_result return query_result
if url_result: if url_result:
return url_result return url_result
# If no valid results # If no valid results
return UnverifiedFactCheckResponse( return UnverifiedFactCheckResponse(
claim=request.query or f"URL: {request.url}", claim=request.query or f"URL: {request.url}",
verdict=VerdictEnum.UNVERIFIED, verdict=VerdictEnum.UNVERIFIED,
confidence=ConfidenceEnum.LOW, confidence=ConfidenceEnum.LOW,
sources=[], sources=[],
evidence="No fact check results found", evidence="No fact check results found",
explanation="The system encountered errors while processing the fact checks.", explanation="The system encountered errors while processing the fact checks.",
additional_context="Please try again with different input or contact support if the issue persists." additional_context="Please try again with different input or contact support if the issue persists."
) )
except Exception as e:
# Catch-all exception handler to ensure we always return a 200 response
logger.error(f"Unexpected error in check_facts: {str(e)}")
return UnverifiedFactCheckResponse(
claim=request.query or f"URL: {request.url}",
verdict=VerdictEnum.UNVERIFIED,
confidence=ConfidenceEnum.LOW,
sources=[],
evidence="No fact check results found",
explanation="The system encountered errors while processing the fact checks.",
additional_context="Please try again with different input or contact support if the issue persists."
)