jaifar530 commited on
Commit
dbe30b4
·
unverified ·
1 Parent(s): 168f32a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -28
app.py CHANGED
@@ -11,6 +11,7 @@ import pickle
11
  import numpy as np
12
  from PIL import Image
13
  from joblib import load
 
14
 
15
 
16
  # Custom headers for the HTTP request
@@ -36,6 +37,19 @@ st.image(banner_image, caption='', use_column_width=True)
36
 
37
  ################ end loading banner image ##################
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  ############# Download Or Check Files/folders exeistince ##############
41
  # Check if the model folder exists
@@ -292,18 +306,8 @@ if press_me_button:
292
  vectorizer = pickle.load(file)
293
 
294
  # Transform the input
295
-
296
  user_input_transformed = vectorizer.transform([new_text])
297
 
298
- # Load the saved models and vectorizer
299
-
300
- # loaded_ridge_classifier = joblib.load(f'{excel_file_to_use}_ridge_model.pkl')
301
- # loaded_extra_trees_classifier = joblib.load(f'{excel_file_to_use}_extra_trees_model.pkl')
302
- # loaded_vectorizer = joblib.load(f'{excel_file_to_use}_vectorizer.pkl')
303
-
304
- # user_input_vectorized = loaded_vectorizer.transform(user_input)
305
-
306
-
307
  # Make predictions
308
  ridge_prediction = ridge_model.predict(user_input_transformed)
309
  extra_trees_prediction = extra_trees_model.predict(user_input_transformed)
@@ -319,31 +323,66 @@ if press_me_button:
319
  "huggingface": "HuggingChat",
320
  "human": "Human-Written"
321
  }
322
- cnn_predicted_author_diplay_name = author_map.get(predicted_author, predicted_author)
323
- ridge_predicted_author_diplay_name = author_map.get(ridge_prediction[0], ridge_prediction[0])
324
- extra_trees_predicted_author_diplay_name = author_map.get(extra_trees_prediction[0], extra_trees_prediction[0])
325
 
 
 
326
  if ridge_prediction == extra_trees_prediction == predicted_author:
327
- st.write(f"Most likely written by: {ridge_predicted_author_diplay_name}")
328
 
 
 
 
 
 
 
 
 
 
329
 
330
- elif ridge_prediction == extra_trees_prediction:
331
- st.write(f"Most likely written by: {ridge_predicted_author_diplay_name}")
332
- st.write(f"2nd Most likely written by: {cnn_predicted_author_diplay_name}")
 
 
 
333
 
334
- elif extra_trees_prediction == predicted_author:
335
- st.write(f"Most likely written by: {extra_trees_predicted_author_diplay_name}")
336
- st.write(f"2nd Most likely written by: {ridge_predicted_author_diplay_name}")
337
 
338
- elif ridge_prediction == predicted_author:
339
- st.write(f"Most likely written by: {ridge_predicted_author_diplay_name}")
340
- st.write(f"2nd Most likely written by: {extra_trees_predicted_author_diplay_name}")
341
 
342
- else:
343
- st.write("Difficult to predict this text, \nit might fill into one of the below:")
344
- st.write(f"- {ridge_predicted_author_diplay_name}")
345
- st.write(f"- {cnn_predicted_author_diplay_name}")
346
- st.write(f"- {extra_trees_predicted_author_diplay_name}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
 
348
  # with st.expander("What is this project about?"):
349
  # st.write("""
 
11
  import numpy as np
12
  from PIL import Image
13
  from joblib import load
14
+ import math
15
 
16
 
17
  # Custom headers for the HTTP request
 
37
 
38
  ################ end loading banner image ##################
39
 
40
+ def get_author_display_name(predicted_author, ridge_prediction, extra_trees_prediction):
41
+ author_map = {
42
+ "googlebard": "Google Bard",
43
+ "gpt3": "ChatGPT-3",
44
+ "gpt4": "ChatGPT-4",
45
+ "huggingface": "HuggingChat",
46
+ "human": "Human-Written"
47
+ }
48
+ cnn_predicted_author_display_name = author_map.get(predicted_author, predicted_author)
49
+ ridge_predicted_author_display_name = author_map.get(ridge_prediction[0], ridge_prediction[0])
50
+ extra_trees_predicted_author_display_name = author_map.get(extra_trees_prediction[0], extra_trees_prediction[0])
51
+
52
+ return cnn_predicted_author_display_name, ridge_predicted_author_display_name, extra_trees_predicted_author_display_name
53
 
54
  ############# Download Or Check Files/folders exeistince ##############
55
  # Check if the model folder exists
 
306
  vectorizer = pickle.load(file)
307
 
308
  # Transform the input
 
309
  user_input_transformed = vectorizer.transform([new_text])
310
 
 
 
 
 
 
 
 
 
 
311
  # Make predictions
312
  ridge_prediction = ridge_model.predict(user_input_transformed)
313
  extra_trees_prediction = extra_trees_model.predict(user_input_transformed)
 
323
  "huggingface": "HuggingChat",
324
  "human": "Human-Written"
325
  }
326
+ # cnn_name = author_map.get(predicted_author, predicted_author)
327
+ # ridge_name = author_map.get(ridge_prediction[0], ridge_prediction[0])
328
+ # extra_trees_name = author_map.get(extra_trees_prediction[0], extra_trees_prediction[0])
329
 
330
+ cnn_name, ridge_name, extra_trees_name = get_author_display_name(predicted_author, ridge_prediction, extra_trees_prediction)
331
+
332
  if ridge_prediction == extra_trees_prediction == predicted_author:
333
+ st.write(f"Most likely written by: {ridge_name}")
334
 
335
+ else:
336
+ # Repeat the text with a space at the end of each iteration
337
+ repeated_text = ""
338
+ max_word_count = 500
339
+ amplify = 1
340
+ if word_count >= max_word_count:
341
+ amplify = 2
342
+ else
343
+ amplify = math.ceil(max_word_count / word_count)
344
 
345
+ for _ in range(amplify):
346
+ repeated_text += new_text + " "
347
+
348
+ new_text = repeated_text
349
+
350
+ ## Repeat ML
351
 
352
+ # Transform the input
353
+ user_input_transformed = vectorizer.transform([new_text])
 
354
 
355
+ # Make predictions
356
+ ridge_prediction = ridge_model.predict(user_input_transformed)
357
+ extra_trees_prediction = extra_trees_model.predict(user_input_transformed)
358
 
359
+ ### Repeat DL
360
+ predicted_author, author_probabilities = predict_author(new_text, loaded_model, tokenizer, label_encoder)
361
+ sorted_probabilities = sorted(author_probabilities.items(), key=lambda x: x[1], reverse=True)
362
+
363
+ # Get disply name
364
+ cnn_name, ridge_name, extra_trees_name = get_author_display_name(predicted_author, ridge_prediction, extra_trees_prediction)
365
+
366
+ if ridge_prediction == extra_trees_prediction == predicted_author:
367
+ st.write(f"Most likely written by: {ridge_name}")
368
+
369
+ elif ridge_prediction == extra_trees_prediction:
370
+ st.write(f"Most likely written by: {ridge_name}")
371
+ st.write(f"2nd Most likely written by: {cnn_name}")
372
+
373
+ elif extra_trees_prediction == predicted_author:
374
+ st.write(f"Most likely written by: {extra_trees_name}")
375
+ st.write(f"2nd Most likely written by: {ridge_name}")
376
+
377
+ elif ridge_prediction == predicted_author:
378
+ st.write(f"Most likely written by: {ridge_name}")
379
+ st.write(f"2nd Most likely written by: {extra_trees_name}")
380
+
381
+ else:
382
+ st.write("Difficult to predict this text, \nit might fill into one of the below:")
383
+ st.write(f"- {ridge_name}")
384
+ st.write(f"- {cnn_name}")
385
+ st.write(f"- {extra_trees_name}")
386
 
387
  # with st.expander("What is this project about?"):
388
  # st.write("""