| from fastapi import FastAPI, Request, Form |
| from fastapi.responses import HTMLResponse |
| import requests |
| import os |
|
|
| app = FastAPI(title="Plagiarism Checker Website") |
|
|
| |
| 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() |
|
|
| |
| 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> |
| """ |
|
|