Alishbah commited on
Commit
77f8cb9
·
verified ·
1 Parent(s): ecdb175

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -118
app.py CHANGED
@@ -14,38 +14,30 @@ THEMES = {
14
  "midnight": {
15
  "primaryColor": "#262730",
16
  "backgroundColor": "#0E0F19",
17
- "secondaryBackgroundColor": "#262730",
18
  "textColor": "#FAFAFA",
19
- "font": "sans-serif",
20
  "welcomeTextColor": "#808080",
21
- "tileColor": "#404040",
22
  },
23
  "forest": {
24
  "primaryColor": "#38761d",
25
  "backgroundColor": "#228B22",
26
- "secondaryBackgroundColor": "#568259",
27
  "textColor": "#E0FFFF",
28
- "font": "sans-serif",
29
  "welcomeTextColor": "#E0FFFF",
30
- "tileColor": "#3D59AB",
31
  },
32
  "beach": {
33
  "primaryColor": "#F4D03F",
34
  "backgroundColor": "#A1D1E8",
35
- "secondaryBackgroundColor": "#F7CAC9",
36
  "textColor": "#6A5149",
37
- "font": "sans-serif",
38
  "welcomeTextColor": "#795548",
39
- "tileColor": "#C3B091",
40
  },
41
  "sunset": {
42
  "primaryColor": "#FFB347",
43
  "backgroundColor": "#E07A5F",
44
- "secondaryBackgroundColor": "#81B29A",
45
  "textColor": "#F4F1DE",
46
- "font": "sans-serif",
47
  "welcomeTextColor": "#F2CC8F",
48
- "tileColor": "#3D405B",
49
  },
50
  }
51
 
@@ -56,7 +48,7 @@ def apply_theme(theme):
56
  body {{
57
  color: {theme["textColor"]};
58
  background-color: {theme["backgroundColor"]};
59
- font-family: {theme["font"]};
60
  }}
61
  .welcome-text {{
62
  color: {theme["welcomeTextColor"]};
@@ -65,6 +57,13 @@ def apply_theme(theme):
65
  text-align: center;
66
  margin-bottom: 20px;
67
  }}
 
 
 
 
 
 
 
68
  .stSlider > div > div {{
69
  color: {theme["textColor"]} !important;
70
  }}
@@ -74,122 +73,22 @@ def apply_theme(theme):
74
  .stMetricValue {{
75
  color: {theme["textColor"]} !important;
76
  }}
77
- .output-box {{
78
- background-color: {theme["tileColor"]};
79
- color: {theme["textColor"]};
80
- padding: 10px;
81
- border-radius: 5px;
82
- margin-top: 20px;
83
- }}
84
  .stFileUploader > div > div:nth-child(1) > div > button {{
85
  background-color: {theme["primaryColor"]};
86
  color: {theme["textColor"]};
87
  }}
88
  </style>
89
- """, unsafe_allow_html=True)
90
-
91
- # Functions for file processing
92
- def extract_text_from_pdf(pdf_file):
93
- resource_manager = PDFResourceManager()
94
- output_string = io.StringIO()
95
- laparams = LAParams()
96
- device = TextConverter(resource_manager, output_string, laparams=laparams)
97
- interpreter = PDFPageInterpreter(resource_manager, device)
98
-
99
- for page in PDFPage.get_pages(pdf_file, caching=True, check_extractable=True):
100
- interpreter.process_page(page)
101
- text = output_string.getvalue()
102
- device.close()
103
- output_string.close()
104
- return text
105
-
106
- def extract_text_from_docx(docx_file):
107
- doc = Document(docx_file)
108
- full_text = []
109
- for paragraph in doc.paragraphs:
110
- full_text.append(paragraph.text)
111
- return '\n'.join(full_text)
112
-
113
- # Split text into chunks
114
- def split_text_into_chunks(text, tokenizer, max_length=512):
115
- chunks = []
116
- tokens = tokenizer.tokenize(text)
117
- for i in range(0, len(tokens), max_length):
118
- chunk_tokens = tokens[i:i + max_length]
119
- chunk_text = tokenizer.convert_tokens_to_string(chunk_tokens)
120
- chunks.append(chunk_text)
121
- return chunks
122
 
123
- # Load AI detection model with a different model and threshold
124
- @st.cache_resource
125
- def load_ai_detection_model(model_name="Hello-SimpleAI/chatgpt-detector-roberta"):
126
- try:
127
- ai_detection = pipeline("text-classification", model=model_name, truncation=True, max_length=512)
128
- return ai_detection
129
- except Exception as e:
130
- st.error(f"Error loading AI detection model: {e}")
131
- return None
132
 
133
- # Detect AI content with a threshold
134
- def detect_ai_content(text_chunks, ai_detection_model, ai_threshold=0.4):
135
- try:
136
- ai_percentages = []
137
- for chunk in text_chunks:
138
- result = ai_detection_model(chunk)
139
- ai_label = result[0]['label']
140
- ai_score = result[0]['score']
141
-
142
- # Apply threshold
143
- if ai_label == 'AI' and ai_score > ai_threshold:
144
- ai_percentages.append(ai_score)
145
- elif ai_label == 'Human' and ai_score < (1 - ai_threshold):
146
- ai_percentages.append(0) # Treat as human
147
- else:
148
- ai_percentages.append(0) # Inconclusive
149
-
150
- return ai_percentages
151
- except Exception as e:
152
- st.error(f"Error during AI content detection: {e}")
153
- return None
154
-
155
- # Load plagiarism model
156
- @st.cache_resource
157
- def load_plagiarism_model(model_name="jpwahle/longformer-base-plagiarism-detection"):
158
- try:
159
- tokenizer = AutoTokenizer.from_pretrained(model_name)
160
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
161
- return tokenizer, model
162
- except Exception as e:
163
- st.error(f"Error loading plagiarism detection model: {e}")
164
- return None
165
-
166
- # Plagiarism check
167
- def plagiarism_check(text_chunks, tokenizer, model):
168
- try:
169
- plagiarized_count = 0
170
- for chunk in text_chunks:
171
- inputs = tokenizer(chunk, return_tensors="pt", truncation=True, padding=True, max_length=512)
172
- with torch.no_grad():
173
- outputs = model(**inputs)
174
- predicted_class = torch.argmax(outputs.logits, dim=-1).item()
175
- if predicted_class == 1:
176
- plagiarized_count += 1
177
-
178
- plagiarism_percentage = (plagiarized_count / len(text_chunks)) * 100
179
- return plagiarism_percentage
180
- except Exception as e:
181
- st.error(f"Error during plagiarism detection: {e}")
182
- return None
183
-
184
- # Streamlit app with styling and multiple file uploads
185
  def main():
186
  # --- Random Theme Selection ---
187
  selected_theme = random.choice(list(THEMES.values()))
188
  apply_theme(selected_theme)
189
 
190
- # Set page configuration and background color
191
- st.set_page_config(page_title="AI & Plagiarism Detection", page_icon="🔍")
192
-
193
  # Title with slider effect
194
  slider_value = st.slider("AI Plagiarism Detection Tool", min_value=0, max_value=100, value=50)
195
 
@@ -240,7 +139,7 @@ def main():
240
 
241
  # Plagiarism Check
242
  if tokenizer and plagiarism_model:
243
- plagiarism_percentage = plagiarism_check(text_chunks, tokenizer, model)
244
  else:
245
  plagiarism_percentage = None
246
 
@@ -265,5 +164,7 @@ def main():
265
  else:
266
  st.write("Plagiarism Detection not available")
267
 
 
 
268
  if __name__ == "__main__":
269
  main()
 
14
  "midnight": {
15
  "primaryColor": "#262730",
16
  "backgroundColor": "#0E0F19",
17
+ "tileColor": "#404040",
18
  "textColor": "#FAFAFA",
 
19
  "welcomeTextColor": "#808080",
 
20
  },
21
  "forest": {
22
  "primaryColor": "#38761d",
23
  "backgroundColor": "#228B22",
24
+ "tileColor": "#3D59AB",
25
  "textColor": "#E0FFFF",
 
26
  "welcomeTextColor": "#E0FFFF",
 
27
  },
28
  "beach": {
29
  "primaryColor": "#F4D03F",
30
  "backgroundColor": "#A1D1E8",
31
+ "tileColor": "#C3B091",
32
  "textColor": "#6A5149",
 
33
  "welcomeTextColor": "#795548",
 
34
  },
35
  "sunset": {
36
  "primaryColor": "#FFB347",
37
  "backgroundColor": "#E07A5F",
38
+ "tileColor": "#3D405B",
39
  "textColor": "#F4F1DE",
 
40
  "welcomeTextColor": "#F2CC8F",
 
41
  },
42
  }
43
 
 
48
  body {{
49
  color: {theme["textColor"]};
50
  background-color: {theme["backgroundColor"]};
51
+ font-family: sans-serif;
52
  }}
53
  .welcome-text {{
54
  color: {theme["welcomeTextColor"]};
 
57
  text-align: center;
58
  margin-bottom: 20px;
59
  }}
60
+ .output-box {{
61
+ background-color: {theme["tileColor"]};
62
+ color: {theme["textColor"]};
63
+ padding: 10px;
64
+ border-radius: 5px;
65
+ margin-top: 20px;
66
+ }}
67
  .stSlider > div > div {{
68
  color: {theme["textColor"]} !important;
69
  }}
 
73
  .stMetricValue {{
74
  color: {theme["textColor"]} !important;
75
  }}
 
 
 
 
 
 
 
76
  .stFileUploader > div > div:nth-child(1) > div > button {{
77
  background-color: {theme["primaryColor"]};
78
  color: {theme["textColor"]};
79
  }}
80
  </style>
81
+ """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
+ # --- Streamlit Page Configuration ---
84
+ st.set_page_config(page_title="AI & Plagiarism Detection", page_icon="🔍")
 
 
 
 
 
 
 
85
 
86
+ # --- Main Function ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  def main():
88
  # --- Random Theme Selection ---
89
  selected_theme = random.choice(list(THEMES.values()))
90
  apply_theme(selected_theme)
91
 
 
 
 
92
  # Title with slider effect
93
  slider_value = st.slider("AI Plagiarism Detection Tool", min_value=0, max_value=100, value=50)
94
 
 
139
 
140
  # Plagiarism Check
141
  if tokenizer and plagiarism_model:
142
+ plagiarism_percentage = plagiarism_check(text_chunks, tokenizer, plagiarism_model)
143
  else:
144
  plagiarism_percentage = None
145
 
 
164
  else:
165
  st.write("Plagiarism Detection not available")
166
 
167
+ # Functions for file processing and model loading remain unchanged...
168
+
169
  if __name__ == "__main__":
170
  main()