Spaces:
Sleeping
Sleeping
Commit
·
98cc895
1
Parent(s):
adb3838
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import nltk
|
| 2 |
+
nltk.download('punkt')
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
from nltk import sent_tokenize
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
|
| 10 |
+
detector = pipeline(task='text-classification', model='yikang0131/argugpt-detector-sent')
|
| 11 |
+
|
| 12 |
+
def predict_doc(doc):
|
| 13 |
+
sents = sent_tokenize(doc)
|
| 14 |
+
data = {'sentence': [], 'label': [], 'score': []}
|
| 15 |
+
res = []
|
| 16 |
+
for sent in sents:
|
| 17 |
+
label, score = predict_one_sent(sent)
|
| 18 |
+
data['sentence'].append(sent)
|
| 19 |
+
data['score'].append(score)
|
| 20 |
+
if label == 'LABEL_0':
|
| 21 |
+
res.append((sent, 'Human'))
|
| 22 |
+
data['label'].append('Human')
|
| 23 |
+
else:
|
| 24 |
+
res.append((sent, 'Machine'))
|
| 25 |
+
data['label'].append('Machine')
|
| 26 |
+
df = pd.DataFrame(data)
|
| 27 |
+
df.to_csv('result.csv')
|
| 28 |
+
return res, df, 'result.csv'
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def predict_one_sent(sent):
|
| 32 |
+
res = detector(sent)[0]
|
| 33 |
+
return res['label'], res['score']
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=predict_doc,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.Textbox(
|
| 40 |
+
label='Essay input',
|
| 41 |
+
info="Please enter essay in the textbox",
|
| 42 |
+
lines=5
|
| 43 |
+
)
|
| 44 |
+
],
|
| 45 |
+
outputs=[
|
| 46 |
+
gr.HighlightedText(
|
| 47 |
+
label='Labeled Result',
|
| 48 |
+
show_legend=True
|
| 49 |
+
).style(color_map={'Machine': 'red', 'Human': 'green'}),
|
| 50 |
+
gr.DataFrame(
|
| 51 |
+
label='Table with Probability Score',
|
| 52 |
+
max_rows=10
|
| 53 |
+
),
|
| 54 |
+
gr.File(
|
| 55 |
+
label='CSV file storing data with all sentences'
|
| 56 |
+
)
|
| 57 |
+
],
|
| 58 |
+
theme=gr.themes.Base()
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
iface.launch()
|