Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,15 +1,24 @@
|
|
| 1 |
# main.py
|
| 2 |
from fastapi import FastAPI
|
| 3 |
-
from pydantic import BaseModel, Field, HttpUrl
|
| 4 |
-
from typing import List
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
# -- 1. Define Pydantic Models for Data Validation --
|
| 7 |
|
| 8 |
# This model defines the structure of the incoming request JSON
|
| 9 |
class RequestPacket(BaseModel):
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# These models define the structure of the outgoing response JSON
|
| 15 |
class Analysis(BaseModel):
|
|
@@ -39,7 +48,8 @@ app = FastAPI(
|
|
| 39 |
# This tells the API to accept requests from your frontend
|
| 40 |
origins = [
|
| 41 |
"https://your-firebase-app-name.web.app", # Your production frontend URL
|
| 42 |
-
"http://localhost:3000",
|
|
|
|
| 43 |
]
|
| 44 |
|
| 45 |
app.add_middleware(
|
|
@@ -57,22 +67,24 @@ async def check_request_format(request: RequestPacket):
|
|
| 57 |
This endpoint receives a request packet, validates its structure,
|
| 58 |
and returns a mock analysis response.
|
| 59 |
"""
|
| 60 |
-
# For your debugging: print a confirmation to the server console
|
| 61 |
print("✅ Request received and successfully validated!")
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
#
|
| 67 |
mock_response = ResponsePacket(
|
| 68 |
-
summary="This is a simulated summary
|
| 69 |
analysis=Analysis(
|
| 70 |
isMisinformation=True,
|
| 71 |
-
reasoning="The claim contradicts established facts
|
| 72 |
confidenceScore=0.88
|
| 73 |
),
|
| 74 |
sources=[
|
| 75 |
-
Source(name="Verified Source A", description="Provides data
|
| 76 |
Source(name="FactCheck Organization B", description="Previously debunked a similar claim.")
|
| 77 |
]
|
| 78 |
)
|
|
|
|
| 1 |
# main.py
|
| 2 |
from fastapi import FastAPI
|
| 3 |
+
from pydantic import BaseModel, Field, HttpUrl,root_validator, validator
|
| 4 |
+
from typing import List, Optional
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
# -- 1. Define Pydantic Models for Data Validation --
|
| 7 |
|
| 8 |
# This model defines the structure of the incoming request JSON
|
| 9 |
class RequestPacket(BaseModel):
|
| 10 |
+
# 1. Make fields optional by using Optional[<type>] and setting default to None
|
| 11 |
+
text: Optional[str] = None
|
| 12 |
+
url: Optional[HttpUrl] = None
|
| 13 |
+
image: Optional[str] = None
|
| 14 |
+
|
| 15 |
+
# 2. Add a validator to ensure at least one field is provided
|
| 16 |
+
@root_validator(pre=False, skip_on_failure=True)
|
| 17 |
+
def check_at_least_one_field_present(cls, values):
|
| 18 |
+
"""Ensure at least one of text, url, or image is provided."""
|
| 19 |
+
if not any(values.values()):
|
| 20 |
+
raise ValueError('At least one of the fields (text, url, image) must be provided.')
|
| 21 |
+
return values
|
| 22 |
|
| 23 |
# These models define the structure of the outgoing response JSON
|
| 24 |
class Analysis(BaseModel):
|
|
|
|
| 48 |
# This tells the API to accept requests from your frontend
|
| 49 |
origins = [
|
| 50 |
"https://your-firebase-app-name.web.app", # Your production frontend URL
|
| 51 |
+
"http://localhost:3000",
|
| 52 |
+
"https://6000-firebase-studio-1758293511500.cluster-6dx7corvpngoivimwvvljgokdw.cloudworkstations.dev/"
|
| 53 |
]
|
| 54 |
|
| 55 |
app.add_middleware(
|
|
|
|
| 67 |
This endpoint receives a request packet, validates its structure,
|
| 68 |
and returns a mock analysis response.
|
| 69 |
"""
|
|
|
|
| 70 |
print("✅ Request received and successfully validated!")
|
| 71 |
+
if request.text:
|
| 72 |
+
print(f"Text received (length): {len(request.text)}")
|
| 73 |
+
if request.url:
|
| 74 |
+
print(f"URL received: {request.url}")
|
| 75 |
+
if request.image:
|
| 76 |
+
print(f"Image data received, starts with: {request.image[:30]}...")
|
| 77 |
|
| 78 |
+
# The mock response logic remains the same
|
| 79 |
mock_response = ResponsePacket(
|
| 80 |
+
summary="This is a simulated summary for the flexible input provided.",
|
| 81 |
analysis=Analysis(
|
| 82 |
isMisinformation=True,
|
| 83 |
+
reasoning="The claim contradicts established facts.",
|
| 84 |
confidenceScore=0.88
|
| 85 |
),
|
| 86 |
sources=[
|
| 87 |
+
Source(name="Verified Source A", description="Provides conflicting data."),
|
| 88 |
Source(name="FactCheck Organization B", description="Previously debunked a similar claim.")
|
| 89 |
]
|
| 90 |
)
|