Spaces:
Sleeping
Sleeping
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) | |