File size: 4,066 Bytes
f496397
 
 
 
3f5a66d
f496397
 
 
8c909a8
f496397
546eeb2
f496397
 
 
8c909a8
f496397
 
3f5a66d
f496397
 
 
 
 
 
d1aeffe
cf939e6
f496397
8c909a8
f496397
 
 
d1aeffe
cf939e6
f496397
8c909a8
f496397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43139ce
f496397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import pipeline

save_path_abstract = 'ngocminhta/MGT-RoBERTa-combined-labels'
model_abstract = AutoModelForSequenceClassification.from_pretrained(save_path_abstract)
tokenizer_abstract = AutoTokenizer.from_pretrained(save_path_abstract)

classifier_abstract = pipeline('text-classification', model=model_abstract, tokenizer=tokenizer_abstract, return_all_scores=True, truncation=True, max_length=512)

save_path_essay = 'Bimarshad/distilbert.essays'
model_essay = AutoModelForSequenceClassification.from_pretrained(save_path_essay)
tokenizer_essay = AutoTokenizer.from_pretrained(save_path_essay)

classifier_essay = pipeline('text-classification', model=model_essay, tokenizer=tokenizer_essay, return_all_scores=True, truncation=True, max_length=512)

def update(name, uploaded_file, radio_input):
    labels = ["Human-written", "Machine-generated", "Human-written, Machine-polished"] #, "Machine-generated, Machine-humanized"]

    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]
            scores = []
            for i in range(3):
                scores.append(data[i]['score'])
            return dict(zip(labels,scores))

        elif radio_input == 'Student Essay':
            data = classifier_essay(name)[0]
            scores = []
            for i in range(3):
                scores.append(data[i]['score'])
            return dict(zip(labels,scores))

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.Label(label="Results")
    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)