from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel, Field from typing import List, Optional import os import asyncio from engine import run_plagiarism_check app = FastAPI(title="Plagiarism Engine", version="1.0.0") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["POST", "GET", "OPTIONS"], allow_headers=["*"], ) class CheckRequest(BaseModel): text: str = Field(..., min_length=100, max_length=15000) include_academic: bool = True include_web: bool = True max_sources: int = Field(10, ge=3, le=20) class MatchSource(BaseModel): url: str title: Optional[str] = None matched_text: str similarity: float source_type: str # "web" | "academic" class FlaggedSentence(BaseModel): sentence: str sentence_index: int best_similarity: float sources: List[MatchSource] class CheckResponse(BaseModel): overall_plag_percent: float flagged_sentences: List[FlaggedSentence] total_sentences: int queries_made: int error: Optional[str] = None @app.get("/") async def root(): return {"status": "ok", "service": "plagiarism-engine"} @app.get("/health") async def health(): return {"status": "healthy"} @app.post("/check", response_model=CheckResponse) async def check(req: CheckRequest): try: result = await run_plagiarism_check( text=req.text, include_academic=req.include_academic, include_web=req.include_web, max_sources=req.max_sources, ) return result except Exception as e: raise HTTPException(status_code=500, detail=f"Plagiarism check failed: {str(e)}")