Spaces:
Sleeping
Sleeping
File size: 2,412 Bytes
942a690 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | # main.py
from fastapi import FastAPI
from pydantic import BaseModel, Field, HttpUrl
from typing import List
# -- 1. Define Pydantic Models for Data Validation --
# This model defines the structure of the incoming request JSON
class RequestPacket(BaseModel):
text: str
url: HttpUrl # Pydantic validates this is a valid URL
image: str # Expecting a base64 encoded string
# These models define the structure of the outgoing response JSON
class Analysis(BaseModel):
isMisinformation: bool
reasoning: str
confidenceScore: float = Field(
...,
ge=0, # Must be greater than or equal to 0
le=1 # Must be less than or equal to 1
)
class Source(BaseModel):
name: str
description: str
class ResponsePacket(BaseModel):
summary: str
analysis: Analysis
sources: List[Source]
# -- 2. Create the FastAPI Application --
app = FastAPI(
title="Backend Checker API",
description="A mock API to validate requests and send simulated responses."
)
# -- 3. Define the API Endpoint --
@app.post("/check", response_model=ResponsePacket)
async def check_request_format(request: RequestPacket):
"""
This endpoint receives a request packet, validates its structure,
and returns a mock analysis response.
"""
# For your debugging: print a confirmation to the server console
print("✅ Request received and successfully validated!")
print(f"Received URL: {request.url}")
print(f"Text length: {len(request.text)}")
print(f"Image string starts with: {request.image[:30]}...")
# -- 4. Create and Return the Mock Response --
mock_response = ResponsePacket(
summary="This is a simulated summary based on the provided text. The analysis suggests the content requires further verification due to conflicting reports from primary sources.",
analysis=Analysis(
isMisinformation=True,
reasoning="The claim contradicts established facts from reputable news organizations and scientific consensus. The provided image appears to be digitally altered.",
confidenceScore=0.88
),
sources=[
Source(name="Verified Source A", description="Provides data that conflicts with the user's text."),
Source(name="FactCheck Organization B", description="Previously debunked a similar claim.")
]
)
return mock_response |