Nexus_NLP_model / tapp.py
HeheBoi0769's picture
Rename app.py to tapp.py
e19c1b7 verified
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