Spaces:
Sleeping
Sleeping
File size: 4,376 Bytes
f21a673 |
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
import gradio as gr
from transformers import DebertaTokenizer, DebertaForSequenceClassification
from transformers import pipeline
save_path_abstract = '/home/raj.tomar/Downloads/MGT/experiments/fine-tuned-deberta/'
model_abstract = DebertaForSequenceClassification.from_pretrained(save_path_abstract)
tokenizer_abstract = DebertaTokenizer.from_pretrained(save_path_abstract)
classifier_abstract = pipeline('text-classification', model=model_abstract, tokenizer=tokenizer_abstract)
save_path_essay = '/home/raj.tomar/Downloads/MGT/experiments/fine-tuned-deberta/'
model_essay = DebertaForSequenceClassification.from_pretrained(save_path_essay)
tokenizer_essay = DebertaTokenizer.from_pretrained(save_path_essay)
classifier_essay = pipeline('text-classification', model=model_essay, tokenizer=tokenizer_essay)
def update(name, uploaded_file, radio_input):
if uploaded_file is not None:
return f"{name}, you uploaded a file named {uploaded_file.name}."
else:
if radio_input == 'Scientific Abstract':
data = classifier_abstract(name)[0]['label']
if data == 'LABEL_0':
return "human_text"
if data == 'LABEL_1':
return "machine_text"
if data == 'LABEL_2':
return "human-written | machine-polished"
if data == 'LABEL_3':
return "machine-generated | machine-humanized"
else:
if radio_input == 'Student Essay':
data = classifier_essay(name)[0]['label']
if data == 'LABEL_0':
return "human_text"
if data == 'LABEL_1':
return "machine_text"
if data == 'LABEL_2':
return "human-written | machine-polished"
if data == 'LABEL_3':
return "machine-generated | machine-humanized"
# return "Hold on!"
with gr.Blocks() as demo:
gr.Markdown(
"""
<style>
.gr-button-secondary {
width: 100px;
height: 30px;
padding: 5px;
}
.gr-row {
display: flex;
align-items: center;
gap: 10px;
}
.gr-block {
padding: 20px;
}
.gr-markdown p {
font-size: 16px;
}
</style>
<span style='font-family: Arial, sans-serif; font-size: 20px;'>Was this text written by <strong>human</strong> or <strong>AI</strong>?</span>
<p style='font-family: Arial, sans-serif;'>Try detecting one of our sample texts:</p>
"""
)
with gr.Row():
for sample in ["Machine-Generated", "Human-Written", "Machine-Humanized", "Machine - Polished"]:
gr.Button(sample, variant="outline")
with gr.Row():
radio_button = gr.Radio(['Scientific Abstract', 'Student Essay'], label = 'Text Type', info = 'We have specialized models that work on domain-specific text.')
with gr.Row():
input_text = gr.Textbox(placeholder="Paste your text here...", label="", lines=10)
file_input = gr.File(label="Upload File")
#file_input = gr.File(label="", visible=False) # Hide the actual file input
with gr.Row():
check_button = gr.Button("Check Origin", variant="primary")
clear_button = gr.ClearButton([input_text, file_input, radio_button], variant='stop')
#upload_button = gr.Button("Upload File", variant="secondary")
out = gr.Textbox(label="OUTPUT", placeholder="", lines=2)
clear_button.add(out)
check_button.click(fn=update, inputs=[input_text, file_input, radio_button], outputs=out)
#upload_button.click(lambda: None, inputs=[], outputs=[]).then(fn=update, inputs=[input_text, file_input], outputs=out)
# Adding JavaScript to simulate file input click
gr.Markdown(
"""
<script>
document.addEventListener("DOMContentLoaded", function() {
const uploadButton = Array.from(document.getElementsByTagName('button')).find(el => el.innerText === "Upload File");
if (uploadButton) {
uploadButton.onclick = function() {
document.querySelector('input[type="file"]').click();
};
}
});
</script>
"""
)
demo.launch(share=True)
|