Spaces:
Sleeping
Sleeping
ngocminhta
commited on
Commit
•
f496397
1
Parent(s):
ce8a7d4
add app.py
Browse files- app.py +117 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
save_path_abstract = 'ngocminhta/RoBERTa-MGT-Test'
|
6 |
+
model_abstract = AutoModelForSequenceClassification.from_pretrained(save_path_abstract)
|
7 |
+
tokenizer_abstract = AutoTokenizer.from_pretrained(save_path_abstract)
|
8 |
+
|
9 |
+
classifier_abstract = pipeline('text-classification', model=model_abstract, tokenizer=tokenizer_abstract)
|
10 |
+
|
11 |
+
save_path_essay = 'ngocminhta/RoBERTa-MGT-Test'
|
12 |
+
model_essay = AutoModelForSequenceClassification.from_pretrained(save_path_essay)
|
13 |
+
tokenizer_essay = AutoTokenizer.from_pretrained(save_path_essay)
|
14 |
+
|
15 |
+
classifier_essay = pipeline('text-classification', model=model_essay, tokenizer=tokenizer_essay)
|
16 |
+
|
17 |
+
def update(name, uploaded_file, radio_input):
|
18 |
+
scores = []
|
19 |
+
labels = ["human", "llm", "machine-polished", "machine-humanized"]
|
20 |
+
|
21 |
+
if uploaded_file is not None:
|
22 |
+
return f"{name}, you uploaded a file named {uploaded_file.name}."
|
23 |
+
else:
|
24 |
+
if radio_input == 'Scientific Abstract':
|
25 |
+
data = classifier_abstract(name)[0]
|
26 |
+
for i in range(4):
|
27 |
+
scores.append(data[i]['score'])
|
28 |
+
return f"""
|
29 |
+
Predicted label: {labels[scores.index(max(scores))]}.
|
30 |
+
|
31 |
+
Scores:
|
32 |
+
- Human: {scores[0]}
|
33 |
+
- LLM: {scores[1]}
|
34 |
+
- Machine-polised: {scores[2]}
|
35 |
+
- Machine-humanized: {scores[3]}"""
|
36 |
+
|
37 |
+
elif radio_input == 'Student Essay':
|
38 |
+
data = classifier_essay(name)[0]
|
39 |
+
for i in range(4):
|
40 |
+
scores.append(data[i]['score'])
|
41 |
+
return f"""
|
42 |
+
Predicted label: {labels[scores.index(max(scores))]}.
|
43 |
+
|
44 |
+
Scores:
|
45 |
+
- Human: {scores[0]}
|
46 |
+
- LLM: {scores[1]}
|
47 |
+
- Machine-polised: {scores[2]}
|
48 |
+
- Machine-humanized: {scores[3]}"""
|
49 |
+
|
50 |
+
with gr.Blocks() as demo:
|
51 |
+
gr.Markdown(
|
52 |
+
"""
|
53 |
+
<style>
|
54 |
+
.gr-button-secondary {
|
55 |
+
width: 100px;
|
56 |
+
height: 30px;
|
57 |
+
padding: 5px;
|
58 |
+
}
|
59 |
+
.gr-row {
|
60 |
+
display: flex;
|
61 |
+
align-items: center;
|
62 |
+
gap: 10px;
|
63 |
+
}
|
64 |
+
.gr-block {
|
65 |
+
padding: 20px;
|
66 |
+
}
|
67 |
+
.gr-markdown p {
|
68 |
+
font-size: 16px;
|
69 |
+
}
|
70 |
+
</style>
|
71 |
+
<span style='font-family: Arial, sans-serif; font-size: 20px;'>Was this text written by <strong>human</strong> or <strong>AI</strong>?</span>
|
72 |
+
<p style='font-family: Arial, sans-serif;'>Try detecting one of our sample texts:</p>
|
73 |
+
"""
|
74 |
+
)
|
75 |
+
|
76 |
+
with gr.Row():
|
77 |
+
for sample in ["Machine-Generated", "Human-Written", "Machine-Humanized", "Machine - Polished"]:
|
78 |
+
gr.Button(sample, variant="outline")
|
79 |
+
|
80 |
+
with gr.Row():
|
81 |
+
radio_button = gr.Radio(['Scientific Abstract', 'Student Essay'], label = 'Text Type', info = 'We have specialized models that work on domain-specific text.')
|
82 |
+
|
83 |
+
with gr.Row():
|
84 |
+
input_text = gr.Textbox(placeholder="Paste your text here...", label="", lines=10)
|
85 |
+
file_input = gr.File(label="Upload File")
|
86 |
+
|
87 |
+
#file_input = gr.File(label="", visible=False) # Hide the actual file input
|
88 |
+
|
89 |
+
with gr.Row():
|
90 |
+
check_button = gr.Button("Check Origin", variant="primary")
|
91 |
+
clear_button = gr.ClearButton([input_text, file_input, radio_button], variant='stop')
|
92 |
+
#upload_button = gr.Button("Upload File", variant="secondary")
|
93 |
+
|
94 |
+
out = gr.Textbox(label="OUTPUT", placeholder="", lines=2)
|
95 |
+
clear_button.add(out)
|
96 |
+
|
97 |
+
check_button.click(fn=update, inputs=[input_text, file_input, radio_button], outputs=out)
|
98 |
+
#upload_button.click(lambda: None, inputs=[], outputs=[]).then(fn=update, inputs=[input_text, file_input], outputs=out)
|
99 |
+
|
100 |
+
# Adding JavaScript to simulate file input click
|
101 |
+
gr.Markdown(
|
102 |
+
"""
|
103 |
+
<script>
|
104 |
+
document.addEventListener("DOMContentLoaded", function() {
|
105 |
+
const uploadButton = Array.from(document.getElementsByTagName('button')).find(el => el.innerText === "Upload File");
|
106 |
+
if (uploadButton) {
|
107 |
+
uploadButton.onclick = function() {
|
108 |
+
document.querySelector('input[type="file"]').click();
|
109 |
+
};
|
110 |
+
}
|
111 |
+
});
|
112 |
+
</script>
|
113 |
+
"""
|
114 |
+
)
|
115 |
+
|
116 |
+
demo.launch(share=True)
|
117 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|