FastAPI-Test / main.py
PercivalFletcher's picture
Upload 3 files
942a690 verified
raw
history blame
2.41 kB
# 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