PirateXX commited on
Commit
27c3fda
·
1 Parent(s): 8181252

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -6
app.py CHANGED
@@ -3,7 +3,7 @@ from transformers import RobertaForSequenceClassification, RobertaTokenizer, Rob
3
  import torch
4
  import gradio as gr
5
  import os
6
-
7
  app = Flask(__name__)
8
 
9
  ACCESS_TOKEN = os.environ["ACCESS_TOKEN"]
@@ -13,6 +13,24 @@ model = RobertaForSequenceClassification.from_pretrained("PirateXX/ChatGPT-Text-
13
  model_name = "roberta-base"
14
  tokenizer = RobertaTokenizer.from_pretrained(model_name, map_location=torch.device('cpu'))
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def predict(query, device="cpu"):
17
  tokens = tokenizer.encode(query)
18
  all_tokens = len(tokens)
@@ -28,14 +46,24 @@ def predict(query, device="cpu"):
28
  fake, real = probs.detach().cpu().flatten().numpy().tolist()
29
  return {"Real": real, "Fake": fake}
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  demo = gr.Interface(
32
- fn=predict,
33
  inputs=gr.Textbox(placeholder="Copy and paste here..."),
34
- outputs="label" ,
35
  interpretation="default",
36
  examples=["Cristiano Ronaldo is a Portuguese professional soccer player who currently plays as a forward for Manchester United and the Portugal national team. He is widely considered one of the greatest soccer players of all time, having won numerous awards and accolades throughout his career. Ronaldo began his professional career with Sporting CP in Portugal before moving to Manchester United in 2003. He spent six seasons with the club, winning three Premier League titles and one UEFA Champions League title. In 2009, he transferred to Real Madrid for a then-world record transfer fee of $131 million. He spent nine seasons with the club, winning four UEFA Champions League titles, two La Liga titles, and two Copa del Rey titles. In 2018, he transferred to Juventus, where he spent three seasons before returning to Manchester United in 2021. He has also had a successful international career with the Portugal national team, having won the UEFA European Championship in 2016 and the UEFA Nations League in 2019.", "One rule of thumb which applies to everything that we do - professionally and personally : Know what the customer want and deliver. In this case, it is important to know what the organisation what from employee. Connect the same to the KRA. Are you part of a delivery which directly ties to the larger organisational objective. If yes, then the next question is success rate of one’s delivery. If the KRAs are achieved or exceeded, then the employee is entitled for a decent hike."])
37
- gr.Markdown("""
38
- <center><a href='https://clustrmaps.com/site/1bsdc' title='Visit tracker'><img src='//clustrmaps.com/map_v2.png?cl=080808&w=a&t=tt&d=NXQdnwxvIm27veMbB5F7oHNID09nhSvkBRZ_Aji9eIA&co=ffffff&ct=808080'/></a></center>
39
- """)
40
 
41
  demo.launch(show_api=False)
 
3
  import torch
4
  import gradio as gr
5
  import os
6
+ import re
7
  app = Flask(__name__)
8
 
9
  ACCESS_TOKEN = os.environ["ACCESS_TOKEN"]
 
13
  model_name = "roberta-base"
14
  tokenizer = RobertaTokenizer.from_pretrained(model_name, map_location=torch.device('cpu'))
15
 
16
+ # function to break text into an array of sentences
17
+ def text_to_sentences(text):
18
+ return re.split(r'[.!?]', text)
19
+
20
+ # function to concatenate sentences into chunks of size 600 or less
21
+ def chunks_of_600(text, chunk_size=600):
22
+ sentences = text_to_sentences(text)
23
+ chunks = []
24
+ current_chunk = ""
25
+ for sentence in sentences:
26
+ if len(current_chunk + sentence) <= chunk_size:
27
+ current_chunk += sentence
28
+ else:
29
+ chunks.append(current_chunk)
30
+ current_chunk = sentence
31
+ chunks.append(current_chunk)
32
+ return chunks
33
+
34
  def predict(query, device="cpu"):
35
  tokens = tokenizer.encode(query)
36
  all_tokens = len(tokens)
 
46
  fake, real = probs.detach().cpu().flatten().numpy().tolist()
47
  return {"Real": real, "Fake": fake}
48
 
49
+ def findRealProb(text):
50
+ chunksOfText = (chunks_of_600(text))
51
+ results = []
52
+ for chunk in chunksOfText:
53
+ output = predict(chunk)
54
+ results.append([output, len(p)])
55
+
56
+ ans = 0
57
+ for prob, length in results:
58
+ ans = ans + prob*length
59
+ realProb = ans/len(text)
60
+ return realProb
61
+
62
  demo = gr.Interface(
63
+ fn=findRealProb,
64
  inputs=gr.Textbox(placeholder="Copy and paste here..."),
65
+ outputs="label",
66
  interpretation="default",
67
  examples=["Cristiano Ronaldo is a Portuguese professional soccer player who currently plays as a forward for Manchester United and the Portugal national team. He is widely considered one of the greatest soccer players of all time, having won numerous awards and accolades throughout his career. Ronaldo began his professional career with Sporting CP in Portugal before moving to Manchester United in 2003. He spent six seasons with the club, winning three Premier League titles and one UEFA Champions League title. In 2009, he transferred to Real Madrid for a then-world record transfer fee of $131 million. He spent nine seasons with the club, winning four UEFA Champions League titles, two La Liga titles, and two Copa del Rey titles. In 2018, he transferred to Juventus, where he spent three seasons before returning to Manchester United in 2021. He has also had a successful international career with the Portugal national team, having won the UEFA European Championship in 2016 and the UEFA Nations League in 2019.", "One rule of thumb which applies to everything that we do - professionally and personally : Know what the customer want and deliver. In this case, it is important to know what the organisation what from employee. Connect the same to the KRA. Are you part of a delivery which directly ties to the larger organisational objective. If yes, then the next question is success rate of one’s delivery. If the KRAs are achieved or exceeded, then the employee is entitled for a decent hike."])
 
 
 
68
 
69
  demo.launch(show_api=False)