Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +54 -0
- requirements.txt +38 -0
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
2 |
+
from fastapi import FastAPI, HTTPException
|
3 |
+
from pydantic import BaseModel
|
4 |
+
import torch
|
5 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
6 |
+
|
7 |
+
|
8 |
+
roberta_model = AutoModelForSequenceClassification.from_pretrained("roberta-base")
|
9 |
+
roberta_tokenizer = AutoTokenizer.from_pretrained("roberta-base")
|
10 |
+
|
11 |
+
# Load BERT model and tokenizer
|
12 |
+
bert_model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
|
13 |
+
bert_tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
14 |
+
|
15 |
+
app = FastAPI()
|
16 |
+
|
17 |
+
class TextData(BaseModel):
|
18 |
+
text: str
|
19 |
+
|
20 |
+
# Helper function to make predictions and convert to 0 (human) or 100 (AI)
|
21 |
+
def predict_text(model, tokenizer, text):
|
22 |
+
# Preprocess the text
|
23 |
+
inputs = tokenizer(text, truncation=True, padding='max_length', max_length=128, return_tensors='pt')
|
24 |
+
|
25 |
+
# Move to the correct device (GPU/CPU)
|
26 |
+
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
27 |
+
model.to(device)
|
28 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
29 |
+
|
30 |
+
# Get model predictions
|
31 |
+
with torch.no_grad():
|
32 |
+
outputs = model(**inputs)
|
33 |
+
|
34 |
+
# Convert logits to probabilities
|
35 |
+
logits = outputs.logits
|
36 |
+
probabilities = torch.softmax(logits, dim=-1)
|
37 |
+
predicted_class = torch.argmax(probabilities, dim=-1).item()
|
38 |
+
#ai_prob = probabilities[0][1].item() * 100
|
39 |
+
#print(ai_prob)
|
40 |
+
# Return 0 for human, 100 for AI
|
41 |
+
return 100 if predicted_class == 1 else 0
|
42 |
+
|
43 |
+
# Endpoint to predict with RoBERTa
|
44 |
+
@app.post("/predict_copyleaks_V1")
|
45 |
+
def predict_roberta(data: TextData):
|
46 |
+
predicted_value = predict_text(roberta_model, roberta_tokenizer, data.text)
|
47 |
+
return {"text": data.text, "Score": predicted_value}
|
48 |
+
|
49 |
+
# Endpoint to predict with BERT
|
50 |
+
@app.post("/predict_copyleaks_V2")
|
51 |
+
def predict_bert(data: TextData):
|
52 |
+
predicted_value = predict_text(bert_model, bert_tokenizer, data.text)
|
53 |
+
return {"text": data.text, "Score": predicted_value}
|
54 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
annotated-types==0.7.0
|
2 |
+
anyio==4.6.0
|
3 |
+
certifi==2024.8.30
|
4 |
+
charset-normalizer==3.3.2
|
5 |
+
click==8.1.7
|
6 |
+
colorama==0.4.6
|
7 |
+
fastapi==0.115.0
|
8 |
+
filelock==3.16.1
|
9 |
+
fsspec==2024.9.0
|
10 |
+
h11==0.14.0
|
11 |
+
httptools==0.6.1
|
12 |
+
huggingface-hub==0.25.1
|
13 |
+
idna==3.10
|
14 |
+
Jinja2==3.1.4
|
15 |
+
MarkupSafe==2.1.5
|
16 |
+
mpmath==1.3.0
|
17 |
+
networkx==3.3
|
18 |
+
numpy==2.1.1
|
19 |
+
packaging==24.1
|
20 |
+
pydantic==2.9.2
|
21 |
+
pydantic_core==2.23.4
|
22 |
+
python-dotenv==1.0.1
|
23 |
+
PyYAML==6.0.2
|
24 |
+
regex==2024.9.11
|
25 |
+
requests==2.32.3
|
26 |
+
safetensors==0.4.5
|
27 |
+
sniffio==1.3.1
|
28 |
+
starlette==0.38.6
|
29 |
+
sympy==1.13.3
|
30 |
+
tokenizers==0.20.0
|
31 |
+
torch==2.4.1
|
32 |
+
tqdm==4.66.5
|
33 |
+
transformers==4.45.1
|
34 |
+
typing_extensions==4.12.2
|
35 |
+
urllib3==2.2.3
|
36 |
+
uvicorn==0.31.0
|
37 |
+
watchfiles==0.24.0
|
38 |
+
websockets==13.1
|