File size: 10,844 Bytes
c57c848
 
 
 
a2b0083
 
 
c57c848
 
 
dad3685
 
a2b0083
dad3685
 
 
a2b0083
dad3685
c57c848
a08a74d
a2b0083
a08a74d
 
 
c57c848
 
 
a2b0083
c57c848
a2b0083
c57c848
 
 
 
 
 
 
 
e09232a
a08a74d
3710a04
 
a08a74d
04b9742
f97e9a9
 
 
c57c848
dad3685
 
a08a74d
dad3685
34b93b5
 
a08a74d
34b93b5
a08a74d
34b93b5
f97e9a9
 
 
 
72241b4
f97e9a9
 
 
 
 
 
 
 
 
 
 
 
 
 
18e11fc
 
f97e9a9
34b93b5
 
 
25dea08
db8f51d
25dea08
9be0260
0fd14a7
04b9742
0b2db5d
cd3b61f
0b2db5d
 
 
 
 
 
 
cd3b61f
25dea08
0fd14a7
25dea08
da1ebea
2160e26
 
b55fefb
 
2160e26
 
 
 
b55fefb
2160e26
 
 
911b4eb
db8f51d
 
18e11fc
c57c848
 
 
5e5a50a
 
899a753
da1ebea
fcf78e8
5b36832
d8fb0cb
 
 
 
 
020ecdf
5b36832
 
 
 
 
fcf78e8
899a753
da1ebea
5e5a50a
fcf78e8
c57c848
020ecdf
25dea08
 
76a6ea9
25dea08
d89e652
76a6ea9
d89e652
020ecdf
 
c57c848
0b2db5d
c57c848
b5eddd2
fcf78e8
 
 
25dea08
c57c848
25dea08
c57c848
db8f51d
c57c848
328a8f1
db8f51d
 
da1ebea
9054596
c57c848
25dea08
da1ebea
c57c848
25dea08
c57c848
25dea08
da1ebea
25dea08
da1ebea
 
 
 
 
 
 
25dea08
da1ebea
 
 
 
 
 
 
0b2db5d
 
 
 
 
 
 
 
 
911b4eb
 
 
 
 
cd3b61f
911b4eb
 
a08a74d
12c1c6b
a08a74d
12c1c6b
911b4eb
c57c848
911b4eb
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

model1_path = "modernbert.bin"
model2_path = "https://huggingface.co/mihalykiss/modernbert_2/resolve/main/Model_groups_3class_seed12"
model3_path = "https://huggingface.co/mihalykiss/modernbert_2/resolve/main/Model_groups_3class_seed22"
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

tokenizer = AutoTokenizer.from_pretrained("answerdotai/ModernBERT-base")

model_1 = AutoModelForSequenceClassification.from_pretrained("answerdotai/ModernBERT-base", num_labels=41)
model_1.load_state_dict(torch.load(model1_path, map_location=device))
model_1.to(device).eval()

model_2 = AutoModelForSequenceClassification.from_pretrained("answerdotai/ModernBERT-base", num_labels=41)
model_2.load_state_dict(torch.hub.load_state_dict_from_url(model2_path, map_location=device))
model_2.to(device).eval()

model_3 = AutoModelForSequenceClassification.from_pretrained("answerdotai/ModernBERT-base", num_labels=41)
model_3.load_state_dict(torch.hub.load_state_dict_from_url(model3_path, map_location=device))
model_3.to(device).eval()


label_mapping = {
    0: '13B', 1: '30B', 2: '65B', 3: '7B', 4: 'GLM130B', 5: 'bloom_7b',
    6: 'bloomz', 7: 'cohere', 8: 'davinci', 9: 'dolly', 10: 'dolly-v2-12b',
    11: 'flan_t5_base', 12: 'flan_t5_large', 13: 'flan_t5_small',
    14: 'flan_t5_xl', 15: 'flan_t5_xxl', 16: 'gemma-7b-it', 17: 'gemma2-9b-it',
    18: 'gpt-3.5-turbo', 19: 'gpt-35', 20: 'gpt4', 21: 'gpt4o',
    22: 'gpt_j', 23: 'gpt_neox', 24: 'human', 25: 'llama3-70b', 26: 'llama3-8b',
    27: 'mixtral-8x7b', 28: 'opt_1.3b', 29: 'opt_125m', 30: 'opt_13b',
    31: 'opt_2.7b', 32: 'opt_30b', 33: 'opt_350m', 34: 'opt_6.7b',
    35: 'opt_iml_30b', 36: 'opt_iml_max_1.3b', 37: 't0_11b', 38: 't0_3b',
    39: 'text-davinci-002', 40: 'text-davinci-003'
}

def classify_text(text):
    if not text.strip():
        result_message = (
            f"---- \n"
            f"**Results will appear here...**"
        )
        return result_message

    inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(device)

    with torch.no_grad():
        logits_1 = model_1(**inputs).logits
        logits_2 = model_2(**inputs).logits
        logits_3 = model_3(**inputs).logits

        softmax_1 = torch.softmax(logits_1, dim=1)
        softmax_2 = torch.softmax(logits_2, dim=1)
        softmax_3 = torch.softmax(logits_3, dim=1)

        averaged_probabilities = (softmax_1 + softmax_2 + softmax_3) / 3
        probabilities = averaged_probabilities[0]

    ai_probs = probabilities.clone()
    ai_probs[24] = 0
    ai_total_prob = ai_probs.sum().item() * 100
    human_prob = 100 - ai_total_prob

    ai_argmax_index = torch.argmax(ai_probs).item()
    ai_argmax_model = label_mapping[ai_argmax_index]

    if human_prob > ai_total_prob:
        result_message = (
            f"βœ… - The text is <span class='highlight-human'>**{human_prob:.2f}%** likely <b>Human written</b>.</span>"
        )
    else:
        result_message = (
            f"πŸ€– - The text is <span class='highlight-ai'>**{ai_total_prob:.2f}%** likely <b>AI generated</b>.</span>\n\n"
            f"**Identified AI Model:** {ai_argmax_model}"
        )

    return result_message





title = "AI Text Detector"

description = """


This tool uses the <b>ModernBERT</b> model to identify whether a given text was written by a human or generated by artificial intelligence (AI). It works with a soft voting ensemble using <b>three</b> models, combining their outputs to improve the accuracy.
<br>

<div style="line-height: 1.8;">
βœ… <b>Human Verification:</b> Human-written content is clearly marked.<br>
πŸ” <b>Model Detection:</b> Can identify content from over 40 AI models.<br>
πŸ“ˆ <b>Accuracy:</b> Works best with longer texts for improved precision.
</div>

<br>

Paste your text below to analyze its origin.
"""
bottom_text = "**Developed by SzegedAI**"


AI_texts = [
"Camels are remarkable desert animals known for their unique adaptations to harsh, arid environments. Native to the Middle East, North Africa, and parts of Asia, camels have been essential to human life for centuries, serving as a mode of transportation, a source of food, and even a symbol of endurance and survival. There are two primary species of camels: the dromedary camel, which has a single hump and is commonly found in the Middle East and North Africa, and the Bactrian camel, which has two humps and is native to Central Asia. Their humps store fat, not water, as commonly believed, allowing them to survive long periods without food by metabolizing the stored fat for energy. Camels are highly adapted to desert life. They can go for weeks without water, and when they do drink, they can consume up to 40 gallons in one sitting. Their thick eyelashes, sealable nostrils, and wide, padded feet protect them from sand and help them walk easily on loose desert terrain.",
"Wines are a fascinating reflection of culture, history, and craftsmanship. They embody a rich diversity shaped by the land, climate, and traditions where they are produced. From the bold reds of Bordeaux to the crisp whites of New Zealand, each bottle tells a unique story. What makes wine so special is its ability to connect people. Whether shared at a family dinner, a celebratory event, or a quiet evening with friends, wine enhances experiences and brings people together. The variety of flavors and aromas, influenced by grape type, fermentation techniques, and aging processes, make wine tasting a complex yet rewarding journey for the senses.",
"I find artificial intelligence (AI) to be one of the most transformative and fascinating technologies of our time. Its potential spans a wide range of applications, from automating mundane tasks to revolutionizing industries like healthcare, education, and entertainment. AI has already made significant contributions in fields like language processing, image recognition, and decision-making systems, enabling innovations that were once purely science fiction. However, as powerful as AI can be, it also brings challenges and responsibilities. Ethical considerations, such as bias in data, transparency, and the potential for misuse, need to be carefully addressed to ensure fairness and accountability. The rise of generative AI has also sparked debates about creativity, originality, and intellectual property, making it essential to strike a balance between technological advancement and respecting human contributions."
]

Human_texts = [
"The present book is intended as a text in basic mathematics. As such, it can have multiple use: for a one-year course in the high schools during the third or fourth year (if possible the third, so that calculus can be taken during the fourth year); for a complementary reference in earlier high school grades (elementary algebra and geometry are covered); for a one-semester course at the college level, to review or to get a firm foundation in the basic mathematics necessary to go ahead in calculus, linear algebra, or other topics. Years ago, the colleges used to give courses in β€œ college algebra” and other subjects which should have been covered in high school. More recently, such courses have been thought unnecessary, but some experiences I have had show that they are just as necessary as ever. What is happening is that thecolleges are getting a wide variety of students from high schools, ranging from exceedingly well-prepared ones who have had a good first course in calculus, down to very poorly prepared ones. T",
"Fats are rich in energy, build body cells, support brain development of infants, help body processes, and facilitate the absorption and use of fat-soluble vitamins A, D, E, and K. The major component of lipids is glycerol and fatty acids. According to chemical properties, fatty acids can be divided into saturated and unsaturated fatty acids. Generally lipids containing saturated fatty acids are solid at room temperature and include animal fats (butter, lard, tallow, ghee) and tropical oils (palm,coconut, palm kernel). Saturated fats increase the risk of heart disease."
]

iface = gr.Blocks(css="""
    @import url('https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400;700&display=swap');

    #text_input_box {
        border-radius: 10px;
        border: 2px solid #4CAF50;
        font-size: 18px;
        padding: 15px;
        margin-bottom: 20px;
        width: 60%;
        box-sizing: border-box;
        margin: auto;
    }
    .form.svelte-633qhp {
        background: none;
        border: none;
        box-shadow: none;
    }

    #result_output_box {
        border-radius: 10px;
        border: 2px solid #4CAF50;
        font-size: 18px;
        padding: 15px;
        margin-top: 20px;
        width: 40%;
        box-sizing: border-box;
        text-align: center;
        margin: auto;
    }

    @media (max-width: 768px) {
        #result_output_box {
            width: 100%;
        }
	#text_input_box{
	    width: 100%;
	}
    }

    body {
        font-family: 'Roboto Mono', sans-serif !important;
        padding: 20px;
        display: block;
        justify-content: center;
        align-items: center;
        height: 100vh;
        overflow-y: auto;
    }

    .gradio-container {
        border: 1px solid #4CAF50;
        border-radius: 15px;
        padding: 30px;
        box-shadow: 0px 0px 10px rgba(0,255,0,0.6);
        max-width: 600px;
        margin: auto;
	overflow-y: auto;
    }

    h1 {
        text-align: center;
        font-size: 32px;
        font-weight: bold;
        margin-bottom: 30px;
    }

    .highlight-human {
        color: #4CAF50;
        font-weight: bold;
        background: rgba(76, 175, 80, 0.2);
        padding: 5px;
        border-radius: 8px;
    }

    .highlight-ai {
        color: #FF5733;
        font-weight: bold;
        background: rgba(255, 87, 51, 0.2);
        padding: 5px;
        border-radius: 8px;
    }
    #bottom_text {
        text-align: center;
        margin-top: 50px;
        font-weight: bold;
        font-size: 20px;
    }
    .block.svelte-11xb1hd{
	background: none !important;
    }
""")

with iface:
    gr.Markdown(f"# {title}")
    gr.Markdown(description)
    text_input = gr.Textbox(label="", placeholder="Type or paste your content here...", elem_id="text_input_box", lines=5)
    result_output = gr.Markdown("**Results will appear here...**", elem_id="result_output_box")
    text_input.change(classify_text, inputs=text_input, outputs=result_output)
    with gr.Tab("AI text examples"):
        gr.Examples(AI_texts, inputs=text_input)
    with gr.Tab("Human text examples"):
        gr.Examples(Human_texts, inputs=text_input)
    gr.Markdown(bottom_text, elem_id="bottom_text")

iface.launch(share=True)