File size: 2,183 Bytes
f25bf25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse
import requests
import os

app = FastAPI(title="Plagiarism Checker Website")

# Get token from env variable (safer for deployment)
WINSTON_API_TOKEN = os.environ.get("WINSTON_API_TOKEN", "YOUR_DEFAULT_TOKEN")
PLAGIARISM_URL = "https://api.gowinston.ai/v2/plagiarism"


@app.get("/", response_class=HTMLResponse)
async def home():
    return """
    <html>
        <head>
            <title>Plagiarism Checker</title>
        </head>
        <body style="font-family: Arial, sans-serif; margin: 30px;">
            <h1>AI Plagiarism Checker</h1>
            <form action="/check" method="post">
                <textarea name="text" rows="10" cols="70" 
                          placeholder="Paste your text here..."></textarea><br><br>
                <button type="submit">Check Plagiarism</button>
            </form>
        </body>
    </html>
    """


@app.post("/check", response_class=HTMLResponse)
async def check_plagiarism(text: str = Form(...)):
    headers = {
        "Authorization": f"Bearer {WINSTON_API_TOKEN}",
        "Content-Type": "application/json",
    }
    payload = {"text": text}

    try:
        response = requests.post(PLAGIARISM_URL, json=payload, headers=headers, timeout=20)
        response.raise_for_status()
        result = response.json()

        # Calculate plagiarism percentage
        text_word_count = result["result"].get("textWordCounts", 1)
        plag_words = result["result"].get("totalPlagiarismWords", 0)
        plagiarism_percent = round((plag_words / text_word_count) * 100, 2)

    except Exception as e:
        result = {"error": str(e)}
        plagiarism_percent = None

    return f"""
    <html>
        <head>
            <title>Result</title>
        </head>
        <body style="font-family: Arial, sans-serif; margin: 30px;">
            <h1>Plagiarism Result</h1>
            <p><b>Plagiarism Percentage:</b> {plagiarism_percent if plagiarism_percent is not None else 'Error'}%</p>
            <h2>Full API Response:</h2>
            <pre>{result}</pre>
            <a href="/">Go Back</a>
        </body>
    </html>
    """