Spaces:
Running
Running
File size: 1,816 Bytes
f36a10a |
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 |
from fastapi import FastAPI, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from final import predict_news, get_gemini_analysis
import os
from tempfile import NamedTemporaryFile
app = FastAPI()
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173"], # Your React app's URL
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Rest of your code remains the same
class NewsInput(BaseModel):
text: str
@app.post("/analyze")
async def analyze_news(news: NewsInput):
prediction = predict_news(news.text)
gemini_analysis = get_gemini_analysis(news.text)
return {
"prediction": prediction,
"detailed_analysis": gemini_analysis
}
@app.post("/detect-deepfake")
async def detect_deepfake(file: UploadFile = File(...)):
try:
# Save uploaded file temporarily
with NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as temp_file:
contents = await file.read()
temp_file.write(contents)
temp_file_path = temp_file.name
# Import functions from testing2.py
from deepfake2.testing2 import predict_image, predict_video
# Use appropriate function based on file type
if file.filename.lower().endswith('.mp4'):
result = predict_video(temp_file_path)
file_type = "video"
else:
result = predict_image(temp_file_path)
file_type = "image"
# Clean up temp file
os.remove(temp_file_path)
return {
"result": result,
"file_type": file_type
}
except Exception as e:
return {"error": str(e)}, 500
|