-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
79 lines (59 loc) · 2.07 KB
/
app.py
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
69
70
71
72
73
74
75
76
77
78
from fastapi import FastAPI, HTTPException, Request, Form
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from src.plagiarism_checker import plagiarism_checker
from dotenv import load_dotenv
import os
load_dotenv()
# AUTH_KEYS = json.loads(os.getenv('AUTH_KEYS'))
AUTH_KEY1 = os.getenv('AUTH_KEY1')
AUTH_KEY2 = os.getenv('AUTH_KEY2')
app = FastAPI()
templates = Jinja2Templates(directory="templates")
max_text_len = 1000
def validate_api_key(api_key: str):
if api_key != AUTH_KEY1 and api_key != AUTH_KEY2:
raise HTTPException(status_code=403, detail="Invalid API key")
else:
return True
def validate_input_text(text: str):
if len(text)>max_text_len:
raise HTTPException(status_code=403, detail="Input Text limit exceeded.")
else:
return True
class TextInput(BaseModel):
text: str
api_key: str
origins = [
"http://localhost",
"http://localhost:8080",
"https://vinmahajan.github.io",
"https://vinmahajan.github.io/VINM/",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/api/check-plagiarism")
async def plagiarism_api(input_data: TextInput):
validate_api_key(input_data.api_key)
validate_input_text(input_data.text)
result=plagiarism_checker(input_data.text)
return result
@app.get("/", response_class=HTMLResponse)
async def get_form(request: Request):
return templates.TemplateResponse("index.html", {"request": request, "result": None})
@app.post("/", response_class=HTMLResponse)
async def process_form(request: Request, api_key: str = Form(...), text: str = Form(...)):
# Check if API key is valid
if api_key != AUTH_KEY1 and api_key != AUTH_KEY2:
result = "Invalid API Key"
else:
# Process the text
result=plagiarism_checker(text)
return templates.TemplateResponse("index.html", {"request": request, "result": result})