File size: 16,699 Bytes
b4ae0dd
03d9d99
cd5b591
e8f977c
b4ae0dd
 
 
 
 
 
b576153
 
41af9bf
b576153
 
10c6916
b576153
 
 
10c6916
 
 
 
fa5527f
10c6916
 
 
9a042e3
10c6916
b576153
 
 
 
5edecda
fa5527f
 
 
5edecda
 
 
10c6916
5edecda
 
fa5527f
 
b4ae0dd
 
fa5527f
 
6a24ad8
fa5527f
 
 
 
b4ae0dd
 
 
 
 
8e3d89c
 
10c6916
4516b32
 
 
 
b576153
10c6916
 
 
b576153
10c6916
 
b576153
 
e014439
b4ae0dd
e014439
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1084e8e
 
b4ae0dd
 
1084e8e
b4ae0dd
 
 
 
10c6916
b4ae0dd
 
 
10c6916
b4ae0dd
 
10c6916
b4ae0dd
 
10c6916
b4ae0dd
 
10c6916
b4ae0dd
 
10c6916
b4ae0dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b576153
5edecda
 
b4ae0dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
004e551
 
 
2517f51
b4ae0dd
 
 
 
 
 
 
f7ff8ac
b4ae0dd
dbff27c
b4ae0dd
 
dbff27c
b4ae0dd
 
dbff27c
b4ae0dd
 
a5e0d40
b4ae0dd
 
dbff27c
b4ae0dd
 
a5e0d40
b4ae0dd
 
 
 
 
8cfc361
5edecda
b4ae0dd
5edecda
b4ae0dd
 
 
5edecda
 
b4ae0dd
5edecda
b4ae0dd
 
5edecda
b4ae0dd
 
5edecda
b4ae0dd
 
5edecda
b4ae0dd
 
5edecda
b4ae0dd
5edecda
 
a1276b6
b4ae0dd
6995f13
b4ae0dd
6995f13
b4ae0dd
 
 
a1276b6
b4ae0dd
a1276b6
b4ae0dd
 
6995f13
b4ae0dd
 
6995f13
5edecda
b4ae0dd
6995f13
b4ae0dd
 
6995f13
b4ae0dd
 
 
 
 
a1276b6
b4ae0dd
5edecda
b4ae0dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5edecda
 
 
 
 
5006f20
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489

import streamlit as st
#subtitle 
st.markdown("version: 1.2")
#title 
st.title("Smart Detection System of AI-Generated Text Models")

#subtitle 
st.markdown("This is a POC for Smart Detection System of AI Generated Text Models project (:blue[MSc Data Analytics]), it is a pre-trained model that detect the probablities of using any of the known LLM (chatgpt3, chatgpt4, GoogleBard, HuggingfaceChat)")

import os
import requests
# import pickle
import pandas as pd
import nltk
import spacy
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize, sent_tokenize
import numpy as np
############
from nltk.stem import WordNetLemmatizer
from nltk import ne_chunk, pos_tag, word_tokenize
from nltk.tree import Tree
from joblib import dump, load
nltk.download('wordnet')
nltk.download('maxent_ne_chunker')
nltk.download('words')

#######
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('averaged_perceptron_tagger')

# Check if the file exists
if not os.path.isfile('RandomForestClassifier.joblib'):

    url = 'https://jaifar.net/RandomForestClassifier.joblib'
    headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
    }

    response = requests.get(url, headers=headers)


    with open('RandomForestClassifier.joblib', 'wb') as file:
        file.write(response.content)


# Load the model from the file
clf_loaded = load('RandomForestClassifier.joblib')

# # At this point, the pickle file should exist, either it was already there, or it has been downloaded and extracted.
# with open('RandomForestClassifier.pkl', 'rb') as file:
#     clf_loaded = pickle.load(file)

input_paragraph = st.text_area("Input your text here")
words_counts = word_tokenize(input_paragraph)
final_words = len(words_counts)
st.write('Words counts: ', final_words)



# df = pd.DataFrame(columns=["paragraph"])
# df = df.append({"paragraph": input_paragraph}, ignore_index=True)

df = pd.DataFrame([input_paragraph], columns=["paragraph"])



# Variable to control number of words to retrieve
num_words = 500

# Retrieving only the first num_words words of the paragraph
input_paragraph = ' '.join(word_tokenize(input_paragraph)[:num_words])

# Extracting features
def extract_features(text):
    words = word_tokenize(text)
    sentences = sent_tokenize(text)

    avg_word_length = sum(len(word) for word in words if word.isalpha()) / len(words)
    avg_sent_length = sum(len(sent) for sent in sentences) / len(sentences)
    punctuation_count = len([char for char in text if char in '.,;:?!'])
    stopword_count = len([word for word in words if word in stopwords.words('english')])

    lemmatizer = WordNetLemmatizer()
    lemma_count = len(set(lemmatizer.lemmatize(word) for word in words))

    named_entity_count = len([chunk for chunk in ne_chunk(pos_tag(words)) if isinstance(chunk, Tree)])

    tagged_words = nltk.pos_tag(words)
    pos_counts = nltk.FreqDist(tag for (word, tag) in tagged_words)
    pos_features = {
        'pos_IN': pos_counts['IN'],
        'pos_DT': pos_counts['DT'],
        'pos_NN': pos_counts['NN'],
        'pos_,': pos_counts[','],
        'pos_VBZ': pos_counts['VBZ'],
        'pos_WDT': pos_counts['WDT'],
        'pos_TO': pos_counts['TO'],
        'pos_VB': pos_counts['VB'],
        'pos_VBG': pos_counts['VBG'],
        'pos_.': pos_counts['.'],
        'pos_JJ': pos_counts['JJ'],
        'pos_NNS': pos_counts['NNS'],
        'pos_RB': pos_counts['RB'],
        'pos_CC': pos_counts['CC'],
        'pos_VBN': pos_counts['VBN'],
    }

    features = {
        'avg_word_length': avg_word_length,
        'avg_sent_length': avg_sent_length,
        'punctuation_count': punctuation_count,
        'stopword_count': stopword_count,
        'lemma_count': lemma_count,
        'named_entity_count': named_entity_count,
    }
    features.update(pos_features)

    return pd.Series(features)


# Creates a button named 'Press me'
press_me_button = st.button("Which Model Used?")

if press_me_button:
    input_features = df['paragraph'].apply(extract_features)
    predicted_llm = clf_loaded.predict(input_features)
    #st.write(f"Predicted LLM: {predicted_llm[0]}")

    predicted_proba = clf_loaded.predict_proba(input_features)
    probabilities = predicted_proba[0]
    labels = clf_loaded.classes_

    # Create a mapping from old labels to new labels
    label_mapping = {1: 'gpt3', 2: 'gpt4', 3: 'googlebard', 4: 'huggingface'}

    # Apply the mapping to the labels
    new_labels = [label_mapping[label] for label in labels]

    # Create a dictionary that maps new labels to probabilities
    prob_dict = {k: v for k, v in zip(new_labels, probabilities)}

    # Convert probabilities to percentages and sort the dictionary in descending order
    prob_dict = {k: f'{v*100:.2f}%' for k, v in sorted(prob_dict.items(), key=lambda item: item[1], reverse=True)}

    # Print the dictionary
    #st.write(prob_dict)

    # Create a progress bar and a bar chart for each LLM
    for llm, prob in prob_dict.items():
        st.write(llm + ': ' + prob)
        st.progress(float(prob.strip('%'))/100)

# import streamlit as st
# import os
# import requests
# import pickle
# import pandas as pd
# import nltk
# import spacy
# from nltk.corpus import stopwords
# from nltk.tokenize import word_tokenize, sent_tokenize
# import numpy as np
# ############
# from nltk.stem import WordNetLemmatizer
# from nltk import ne_chunk, pos_tag, word_tokenize
# from nltk.tree import Tree
# from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
# nltk.download('wordnet')
# nltk.download('maxent_ne_chunker')
# nltk.download('words')

# #######
# nltk.download('punkt')
# nltk.download('stopwords')
# nltk.download('averaged_perceptron_tagger')

# #version
# st.markdown("v1.9")


# # URL of the text file
# url = 'https://jaifar.net/text.txt'

# headers = {
# 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
# }

# response = requests.get(url, headers=headers)

# # Check if the request was successful
# if response.status_code == 200:
#     # Read the content of the file
#     content = response.text

#     # Print the content of the file
#     # print(content)
# else:
#     # Handle the case when the request fails
#     print('Failed to download the file.')



# #title 
# st.title("Smart Detection System of AI-Generated Text Models")

# #subtitle 
# st.markdown("This is a POC for Smart Detection System of AI Generated Text Models project (:blue[MSc Data Analytics]), it is a pre-trained model that detect the probablities of using any of the known LLM (chatgpt3, chatgpt4, GoogleBard, HuggingfaceChat)")

# #input text 
# input_paragraph = st.text_area("Input your text here")
# words_counts = word_tokenize(input_paragraph)
# final_words = len(words_counts)
# st.write('Words counts: ', final_words)

# # Define your options
# options = ["AI vs AI - RandomForest - 88 Samples", "AI vs AI - Ridge - 2000 Samples", "AI vs Human"]

# # Create a dropdown menu with "Option 2" as the default
# # selected_option = st.selectbox('Select an Option', options, index=1)
# selected_option = st.selectbox('Select an Option', options)





# # Check if the file exists
# if not os.path.isfile('AI_vs_AI_Ridge_2000_Samples.pkl'):
#     # Download the zip file if it doesn't exist
#     url = 'https://jaifar.net/AI_vs_AI_Ridge_2000_Samples.pkl'
#     headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
#     }

#     response = requests.get(url, headers=headers)

#     # Save the file
#     with open('AI_vs_AI_Ridge_2000_Samples.pkl', 'wb') as file2:
#         file2.write(response.content)



# # df = pd.DataFrame(columns=["paragraph"])
# # df = df.append({"paragraph": input_paragraph}, ignore_index=True)

# df = pd.DataFrame([input_paragraph], columns=["paragraph"])



# # Variable to control number of words to retrieve
# num_words = 500

# # Retrieving only the first num_words words of the paragraph
# input_paragraph = ' '.join(word_tokenize(input_paragraph)[:num_words])


# # Extracting features
# def extract_features_AI_vs_AI_RandomForest_88_Samples(text):
#     words = word_tokenize(text)
#     sentences = sent_tokenize(text)

#     avg_word_length = sum(len(word) for word in words if word.isalpha()) / len(words)
#     avg_sent_length = sum(len(sent) for sent in sentences) / len(sentences)
#     punctuation_count = len([char for char in text if char in '.,;:?!'])
#     stopword_count = len([word for word in words if word in stopwords.words('english')])

#     lemmatizer = WordNetLemmatizer()
#     lemma_count = len(set(lemmatizer.lemmatize(word) for word in words))

#     named_entity_count = len([chunk for chunk in ne_chunk(pos_tag(words)) if isinstance(chunk, Tree)])

#     tagged_words = nltk.pos_tag(words)
#     pos_counts = nltk.FreqDist(tag for (word, tag) in tagged_words)
#     pos_features = {
#         'pos_IN': pos_counts['IN'],
#         'pos_DT': pos_counts['DT'],
#         'pos_NN': pos_counts['NN'],
#         'pos_,': pos_counts[','],
#         'pos_VBZ': pos_counts['VBZ'],
#         'pos_WDT': pos_counts['WDT'],
#         'pos_TO': pos_counts['TO'],
#         'pos_VB': pos_counts['VB'],
#         'pos_VBG': pos_counts['VBG'],
#         'pos_.': pos_counts['.'],
#         'pos_JJ': pos_counts['JJ'],
#         'pos_NNS': pos_counts['NNS'],
#         'pos_RB': pos_counts['RB'],
#         'pos_CC': pos_counts['CC'],
#         'pos_VBN': pos_counts['VBN'],
#     }

#     features = {
#         'avg_word_length': avg_word_length,
#         'avg_sent_length': avg_sent_length,
#         'punctuation_count': punctuation_count,
#         'stopword_count': stopword_count,
#         'lemma_count': lemma_count,
#         'named_entity_count': named_entity_count,
#     }
#     features.update(pos_features)

#     return pd.Series(features)



# # Extracting features for AI_vs_AI_Ridge_2000_Samples
# def extract_features_AI_vs_AI_Ridge_2000_Samples(text):
    
#     words = word_tokenize(text)
#     sentences = sent_tokenize(text)

#     avg_word_length = sum(len(word) for word in words if word.isalpha()) / len(words)
#     avg_sent_length = sum(len(sent) for sent in sentences) / len(sentences)
#     punctuation_count = len([char for char in text if char in '.,;:?!'])
#     stopword_count = len([word for word in words if word in stopwords.words('english')])

#     lemmatizer = WordNetLemmatizer()
#     lemma_count = len(set(lemmatizer.lemmatize(word) for word in words))

#     named_entity_count = len([chunk for chunk in ne_chunk(pos_tag(words)) if isinstance(chunk, Tree)])

#     tagged_words = nltk.pos_tag(words)
#     pos_counts = nltk.FreqDist(tag for (word, tag) in tagged_words)
#     pos_features = {
#         'pos_IN': pos_counts['IN'],
#         'pos_DT': pos_counts['DT'],
#         'pos_NN': pos_counts['NN'],
#         'pos_,': pos_counts[','],
#         'pos_VBZ': pos_counts['VBZ'],
#         'pos_WDT': pos_counts['WDT'],
#         'pos_TO': pos_counts['TO'],
#         'pos_VB': pos_counts['VB'],
#         'pos_PRP': pos_counts['PRP'],
#         'pos_VBP': pos_counts['VBP'],
#         'pos_VBG': pos_counts['VBG'],
#         'pos_.': pos_counts['.'],
#         'pos_JJ': pos_counts['JJ'],
#         'pos_NNS': pos_counts['NNS'],
#         'pos_RB': pos_counts['RB'],
#         'pos_PRP$': pos_counts['PRP$'],
#         'pos_CC': pos_counts['CC'],
#         'pos_MD': pos_counts['MD'],
#         'pos_VBN': pos_counts['VBN'],
#         'pos_NNP': pos_counts['NNP'],
#     }

#     features = {
#         'avg_word_length': avg_word_length,
#         'avg_sent_length': avg_sent_length,
#         'punctuation_count': punctuation_count,
#         'stopword_count': stopword_count,
#         'lemma_count': lemma_count,
#         'named_entity_count': named_entity_count,
#     }
#     # features.update(pos_features)
#     features = pd.concat([features, pd.DataFrame(pos_features, index=[0])], axis=1)

#     return pd.Series(features)

# # Function from Code(2)
# def add_vectorized_features(df):
#     vectorizer = CountVectorizer()
#     tfidf_vectorizer = TfidfVectorizer()
#     X_bow = vectorizer.fit_transform(df['paragraph'])
#     X_tfidf = tfidf_vectorizer.fit_transform(df['paragraph'])
#     df_bow = pd.DataFrame(X_bow.toarray(), columns=vectorizer.get_feature_names_out())
#     df_tfidf = pd.DataFrame(X_tfidf.toarray(), columns=tfidf_vectorizer.get_feature_names_out())
#     df = pd.concat([df, df_bow, df_tfidf], axis=1)
#     return df


# # Function define AI_vs_AI_RandomForest_88_Samples
# def AI_vs_AI_RandomForest_88_Samples(df):
    



#     input_features = df['paragraph'].apply(extract_features_AI_vs_AI_RandomForest_88_Samples)
#     # try:
#     #     predicted_llm = clf_loaded.predict(input_features)
#     #     st.write(f"Predicted LLM: {predicted_llm[0]}")
#     #     predicted_proba = clf_loaded.predict_proba(input_features)
#     # except Exception as e:
#     #     st.write(f"An error occurred: {str(e)}")

#     # labels = clf_loaded.classes_

#     # # Create a mapping from old labels to new labels
#     # label_mapping = {1: 'gpt3', 2: 'gpt4', 3: 'googlebard', 4: 'huggingface'}

#     # # Apply the mapping to the labels
#     # new_labels = [label_mapping[label] for label in labels]

#     # # Create a dictionary that maps new labels to probabilities
#     # prob_dict = {k: v for k, v in zip(new_labels, probabilities)}

#     # # Convert probabilities to percentages and sort the dictionary in descending order
#     # prob_dict = {k: f'{v*100:.2f}%' for k, v in sorted(prob_dict.items(), key=lambda item: item[1], reverse=True)}

#     # # Print the dictionary
#     # #st.write(prob_dict)

#     # # Create a progress bar and a bar chart for each LLM
#     # for llm, prob in prob_dict.items():
#     #     st.write(llm + ': ' + prob)
#     #     st.progress(float(prob.strip('%'))/100)
#     return 


# def AI_vs_AI_Ridge_2000_Samples(df):

#     # At this point, the pickle file should exist, either it was already there, or it has been downloaded and extracted.
#     with open('AI_vs_AI_Ridge_2000_Samples.pkl', 'rb') as file2:
#         clf_loaded = pickle.load(file2)

    
#     input_features = df['paragraph'].apply(extract_features_AI_vs_AI_Ridge_2000_Samples)

#     # Here, input_features is a DataFrame, not a Series
#     input_features = pd.concat(input_features.values, ignore_index=True)

#     # Add new vectorized features
#     df = add_vectorized_features(df)

#     # Concatenate input_features and df along columns
#     final_features = pd.concat([input_features, df], axis=1)

#     predicted_llm = clf_loaded.predict(final_features)
#     st.write(f"Predicted LLM: {predicted_llm[0]}")

#     return



# # Check if the file exists
# if not os.path.isfile('RandomForestClassifier.pkl'):
# # Download the zip file if it doesn't exist
#     url = 'https://jaifar.net/RandomForestClassifier.pkl'
#     headers = {
#         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
#     }

#     response = requests.get(url, headers=headers)

#     # Save the file
#     try:
#         with open('RandomForestClassifier.pkl', 'wb') as file:
#             file.write(response.content)
#     except Exception as e:
#         st.write(f"An error occurred while writing RandomForestClassifier.pkl: {str(e)}") 

# try:
#     with open('RandomForestClassifier.pkl', 'rb') as file:
#         clf_loaded = pickle.load(file)
# except Exception as e:
#     st.write(f"An error occurred while loading RandomForestClassifier.pkl: {str(e)}")

# # Creates a button
# press_me_button = st.button("Which Model Used?")

# if press_me_button:

#     input_features = df['paragraph'].apply(extract_features_AI_vs_AI_RandomForest_88_Samples)
    
#     try:
#         predicted_llm = clf_loaded.predict(input_features)
#         st.write(f"Predicted LLM: {predicted_llm[0]}")
#         predicted_proba = clf_loaded.predict_proba(input_features)
#     except Exception as e:
#         st.write(f"An error occurred: {str(e)}")

#     # # Use the selected option to control the flow of your application
#     # if selected_option == "AI vs AI - RandomForest - 88 Samples":
#     #     AI_vs_AI_RandomForest_88_Samples(df)

#     # elif selected_option == "AI vs AI - Ridge - 2000 Samples":
#     #     AI_vs_AI_Ridge_2000_Samples(df)

#     # elif selected_option == "AI vs Human":
#     #     st.write("You selected AI vs Human!")