jaifar530 commited on
Commit
b576153
·
unverified ·
1 Parent(s): a156f3c

added the model

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py CHANGED
@@ -6,3 +6,120 @@ st.title("Smart Detection System of AI-Generated Text Models")
6
  #subtitle
7
  st.markdown("## This is a POC repo for Smart Detection System of AI Generated Text Models project, it is a pre-trained model that detect the probablities of using any of the known LLM (chatgpt3, chatgpt4, GoogleBard, HuggingfaceChat)##")
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  #subtitle
7
  st.markdown("## This is a POC repo for Smart Detection System of AI Generated Text Models project, it is a pre-trained model that detect the probablities of using any of the known LLM (chatgpt3, chatgpt4, GoogleBard, HuggingfaceChat)##")
8
 
9
+ import os
10
+ import requests
11
+ import pickle
12
+ import pandas as pd
13
+ import nltk
14
+ import spacy
15
+ from nltk.corpus import stopwords
16
+ from nltk.tokenize import word_tokenize, sent_tokenize
17
+ import numpy as np
18
+ nltk.download('punkt')
19
+ nltk.download('stopwords')
20
+ nltk.download('averaged_perceptron_tagger')
21
+
22
+ # Check if the file exists
23
+ if not os.path.isfile('RandomForestClassifier.pkl'):
24
+ # Download the zip file if it doesn't exist
25
+ url = 'https://jaifar.net/RandomForestClassifier.pkl'
26
+ headers = {
27
+ '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',
28
+ }
29
+
30
+ response = requests.get(url, headers=headers)
31
+
32
+ # Save the file
33
+ with open('RandomForestClassifier.pkl', 'wb') as file:
34
+ file.write(response.content)
35
+
36
+ # At this point, the pickle file should exist, either it was already there, or it has been downloaded and extracted.
37
+ with open('RandomForestClassifier.pkl', 'rb') as file:
38
+ clf_loaded = pickle.load(file)
39
+
40
+
41
+
42
+ # Loading a SpaCy model for Named Entity Recognition and Lemmatization
43
+ nlp = spacy.load('en_core_web_sm')
44
+
45
+ # # Your input paragraph
46
+ # input_paragraph = "Your paragraph here..."
47
+
48
+ # # Read the paragraph from a text file
49
+ # with open('paragraph.txt', 'r') as file:
50
+ # input_paragraph = file.read()
51
+
52
+ input_paragraph = st.text_area("Input your text here")
53
+
54
+ df = pd.DataFrame(columns=["paragraph"])
55
+ df = df.append({"paragraph": input_paragraph}, ignore_index=True)
56
+
57
+
58
+
59
+ # Variable to control number of words to retrieve
60
+ num_words = 500
61
+
62
+ # Retrieving only the first num_words words of the paragraph
63
+ input_paragraph = ' '.join(word_tokenize(input_paragraph)[:num_words])
64
+
65
+ # Extracting features
66
+ def extract_features(text):
67
+ words = word_tokenize(text)
68
+ sentences = sent_tokenize(text)
69
+ doc = nlp(text)
70
+
71
+ avg_word_length = sum(len(word) for word in words) / len(words)
72
+ avg_sent_length = sum(len(sent) for sent in sentences) / len(sentences)
73
+ punctuation_count = len([char for char in text if char in '.,;:?!'])
74
+ stopword_count = len([word for word in words if word in stopwords.words('english')])
75
+ lemma_count = len(set(token.lemma_ for token in doc))
76
+ named_entity_count = len(doc.ents)
77
+
78
+ tagged_words = nltk.pos_tag(words)
79
+ pos_counts = nltk.FreqDist(tag for (word, tag) in tagged_words)
80
+ pos_features = {
81
+ 'pos_IN': pos_counts['IN'],
82
+ 'pos_DT': pos_counts['DT'],
83
+ 'pos_NN': pos_counts['NN'],
84
+ 'pos_,': pos_counts[','],
85
+ 'pos_VBZ': pos_counts['VBZ'],
86
+ 'pos_WDT': pos_counts['WDT'],
87
+ 'pos_TO': pos_counts['TO'],
88
+ 'pos_VB': pos_counts['VB'],
89
+ 'pos_VBG': pos_counts['VBG'],
90
+ 'pos_.': pos_counts['.'],
91
+ 'pos_JJ': pos_counts['JJ'],
92
+ 'pos_NNS': pos_counts['NNS'],
93
+ 'pos_RB': pos_counts['RB'],
94
+ 'pos_CC': pos_counts['CC'],
95
+ 'pos_VBN': pos_counts['VBN'],
96
+ }
97
+
98
+ features = {
99
+ 'avg_word_length': avg_word_length,
100
+ 'avg_sent_length': avg_sent_length,
101
+ 'punctuation_count': punctuation_count,
102
+ 'stopword_count': stopword_count,
103
+ 'lemma_count': lemma_count,
104
+ 'named_entity_count': named_entity_count,
105
+ }
106
+ features.update(pos_features)
107
+
108
+ return pd.Series(features)
109
+ #return pd.DataFrame(features)
110
+
111
+
112
+ # Creates a button named 'Press me'
113
+ press_me_button = st.button("Press me")
114
+
115
+ if press_me_button:
116
+ # Display the text entered by the user
117
+
118
+ input_features = df['paragraph'].apply(extract_features)
119
+ predicted_llm = clf_loaded.predict(input_features)
120
+ st.write(f"Predicted LLM: {predicted_llm[0]}")
121
+
122
+ # Get the features of the input paragraph
123
+ #input_features = extract_features(input_paragraph)
124
+
125
+