aliasgerovs commited on
Commit
d994b45
·
1 Parent(s): 46f0706

Updated with latest

Browse files
__pycache__/ai_generate.cpython-310.pyc ADDED
Binary file (622 Bytes). View file
 
__pycache__/humanize.cpython-310.pyc ADDED
Binary file (2.71 kB). View file
 
app.py CHANGED
@@ -2,44 +2,82 @@ import gradio as gr
2
  from humanize import paraphrase_text
3
  from gradio_client import Client
4
  from ai_generate import generate
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- # client = Client("polygraf-ai/Humanizer")
7
  def humanize(
8
- text,
9
  model,
10
  temperature=1.2,
11
  repetition_penalty=1,
12
  top_k=50,
13
- length_penalty=1,
14
- ):
15
- ai_text = generate(f"Write an article about the topic: {text}")
16
- ai_text = ai_text.choices[0].message.content
17
- print(f"AI Generated: {ai_text}")
18
- # result = client.predict(
19
- # text=ai_text,
20
- # model_name=model,
21
- # temperature=temperature,
22
- # repetition_penalty=repetition_penalty,
23
- # top_k=top_k,
24
- # length_penalty=length_penalty,
25
- # api_name="/paraphrase_text"
26
- # )
27
  result = paraphrase_text(
28
- text=ai_text,
29
  model_name=model,
30
  temperature=temperature,
31
  repetition_penalty=repetition_penalty,
32
  top_k=top_k,
33
  length_penalty=length_penalty,
34
  )
35
- return ai_text, result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  with gr.Blocks() as demo:
38
- gr.Markdown("# Polygraf Writer")
39
  with gr.Row():
40
  with gr.Column(scale=0.7):
41
  gr.Markdown("## Enter a topic to write an article about:")
42
  input_topic = gr.Textbox(label="Topic")
 
 
 
 
 
 
 
 
 
 
 
43
  model_dropdown = gr.Radio(
44
  choices=[
45
  "Base Model",
@@ -48,56 +86,100 @@ with gr.Blocks() as demo:
48
  "XL Law Model",
49
  "XL Marketing Model",
50
  "XL Child Style Model",
51
- ],
52
  value="Large Model",
53
- label="Select Model Version",
54
- )
55
- process_button = gr.Button("Humanize Text")
56
- gr.Markdown("### AI Generated article:")
57
- ai_label = gr.HTML(label="AI")
58
- gr.Markdown("### Humanized article:")
59
- output_label = gr.HTML(label="Humanized")
60
 
61
  with gr.Column(scale=0.3):
62
- temperature_slider = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, value=1.2, label="Temperature")
63
- gr.Markdown("Controls the randomness of the paraphrase. Higher values generate more varied text.")
64
  top_k_slider = gr.Slider(
65
  minimum=0,
66
  maximum=300,
67
  step=25,
68
  value=50,
69
- label="Top k",
70
  )
71
- gr.Markdown("Limits the number of top tokens considered during generation.")
72
  repetition_penalty_slider = gr.Slider(
73
  minimum=1.0,
74
  maximum=2.0,
75
  step=0.1,
76
  value=1,
77
- label="Repetition Penalty",
78
  )
79
- gr.Markdown("Penalizes repeated words to encourage diverse language use")
80
  length_penalty_slider = gr.Slider(
81
  minimum=0.0,
82
  maximum=2.0,
83
  step=0.1,
84
  value=1.0,
85
- label="Length Penalty",
86
  )
87
- gr.Markdown("Penalizes shorter outputs.")
88
 
89
- process_button.click(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
  fn=humanize,
91
  inputs=[
92
- input_topic,
93
  model_dropdown,
94
  temperature_slider,
95
  repetition_penalty_slider,
96
  top_k_slider,
97
  length_penalty_slider,
98
  ],
99
- outputs=[ai_label, output_label],
100
  )
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  if __name__ == "__main__":
103
  demo.launch(demo.launch(server_name="0.0.0.0"))
 
2
  from humanize import paraphrase_text
3
  from gradio_client import Client
4
  from ai_generate import generate
5
+ import requests
6
+
7
+ def on_first_button_click():
8
+ return gr.update(visible=True)
9
+
10
+ def ai_article_generator(
11
+ text
12
+ ):
13
+ ai_text = generate(f"Write an article about the topic: {text}")
14
+ ai_text = ai_text.choices[0].message.content
15
+ return ai_text
16
 
 
17
  def humanize(
18
+ text,
19
  model,
20
  temperature=1.2,
21
  repetition_penalty=1,
22
  top_k=50,
23
+ length_penalty=1):
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  result = paraphrase_text(
25
+ text=text,
26
  model_name=model,
27
  temperature=temperature,
28
  repetition_penalty=repetition_penalty,
29
  top_k=top_k,
30
  length_penalty=length_penalty,
31
  )
32
+ return result
33
+
34
+ def ai_generated_test(text):
35
+ url = "http://34.66.10.188/ai-vs-human"
36
+ access_key = "6mcemwsFycVVgVjMFwKXki3zJka1r7N4u$Z0Y|x$gecC$hdNtpQf-SpL0+=k;u%BZ"
37
+ headers = {
38
+ "ACCESS_KEY": access_key
39
+ }
40
+ data = {
41
+ "text" : f"{text}"
42
+ }
43
+ response = requests.post(url, headers=headers, json=data)
44
+ return response.json()
45
+
46
+ def ai_generated_test_sapling(text):
47
+ response = requests.post(
48
+ "https://api.sapling.ai/api/v1/aidetect",
49
+ json={
50
+ "key": "60L9BPSVPIIOEZM0CD1DQWRBPJIUR7SB",
51
+ "text": f"{text}"
52
+ }
53
+ )
54
+ return { "AI" : response.json()['score'], "HUMAN" : 1 - response.json()['score']}
55
+
56
+ def ai_check(text, option):
57
+ if option == 'Polygraf AI':
58
+ return ai_generated_test(text)
59
+ elif option == 'Sapling AI':
60
+ return ai_generated_test_sapling(text)
61
+ else :
62
+ return ai_generated_test(text)
63
 
64
  with gr.Blocks() as demo:
65
+ gr.Markdown("# Polygraf Article Writer")
66
  with gr.Row():
67
  with gr.Column(scale=0.7):
68
  gr.Markdown("## Enter a topic to write an article about:")
69
  input_topic = gr.Textbox(label="Topic")
70
+ ai_article_btn = gr.Button("Generate", visible=True)
71
+ ai_generated = gr.Markdown("### Generated article:", visible=False)
72
+ ai_label = gr.HTML(label="AI", visible=False)
73
+ ai_detector_dropdown = gr.Radio(
74
+ choices=[
75
+ "Polygraf AI",
76
+ "Sapling AI",
77
+ ],label="Select AI Detector", visible=False)
78
+ only_ai_check_btn = gr.Button("AI Check", visible= False)
79
+ bcLabel = gr.Label(label="Source", visible= False)
80
+ humanizer_btn = gr.Button("Humanize", visible=False)
81
  model_dropdown = gr.Radio(
82
  choices=[
83
  "Base Model",
 
86
  "XL Law Model",
87
  "XL Marketing Model",
88
  "XL Child Style Model",
89
+ ],
90
  value="Large Model",
91
+ label="Select Model Version",
92
+ visible=False)
93
+
94
+ humanized_markdown = gr.Markdown("### Humanized article:", visible=False)
95
+ output_label = gr.HTML(label="Humanized", visible= False)
96
+ only_ai_check_btn2 = gr.Button("AI Check", visible= False)
97
+ bcLabel2 = gr.Label(label="Source", visible= False)
98
 
99
  with gr.Column(scale=0.3):
100
+ temperature_slider = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, value=1.2, label="Temperature", visible= False)
101
+ controls_markdown = gr.Markdown("Controls the randomness of the paraphrase. Higher values generate more varied text.", visible= False)
102
  top_k_slider = gr.Slider(
103
  minimum=0,
104
  maximum=300,
105
  step=25,
106
  value=50,
107
+ label="Top k", visible= False
108
  )
109
+ top_token_markdown = gr.Markdown("Limits the number of top tokens considered during generation.", visible= False)
110
  repetition_penalty_slider = gr.Slider(
111
  minimum=1.0,
112
  maximum=2.0,
113
  step=0.1,
114
  value=1,
115
+ label="Repetition Penalty", visible= False
116
  )
117
+ penalize_repeated_markdown = gr.Markdown("Penalizes repeated words to encourage diverse language use", visible= False)
118
  length_penalty_slider = gr.Slider(
119
  minimum=0.0,
120
  maximum=2.0,
121
  step=0.1,
122
  value=1.0,
123
+ label="Length Penalty", visible= False
124
  )
125
+ penalize_markdown = gr.Markdown("Penalizes shorter outputs.", visible= False)
126
 
127
+
128
+ ai_article_btn.click(
129
+ fn=ai_article_generator,
130
+ inputs=[
131
+ input_topic
132
+ ],
133
+ outputs=[ai_label],
134
+ )
135
+
136
+ ai_article_btn.click(on_first_button_click, inputs=None, outputs=ai_generated)
137
+ ai_article_btn.click(on_first_button_click, inputs=None, outputs=ai_label)
138
+ ai_article_btn.click(on_first_button_click, inputs=None, outputs=only_ai_check_btn)
139
+ # only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=ai_detector_dropdown)
140
+ only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=bcLabel)
141
+ only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=model_dropdown)
142
+ only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=humanizer_btn)
143
+ only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=temperature_slider)
144
+ only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=controls_markdown)
145
+ only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=top_k_slider)
146
+ only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=top_token_markdown)
147
+ only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=repetition_penalty_slider)
148
+ only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=penalize_repeated_markdown)
149
+ only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=length_penalty_slider)
150
+ only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=penalize_markdown)
151
+
152
+ humanizer_btn.click(
153
  fn=humanize,
154
  inputs=[
155
+ ai_label,
156
  model_dropdown,
157
  temperature_slider,
158
  repetition_penalty_slider,
159
  top_k_slider,
160
  length_penalty_slider,
161
  ],
162
+ outputs=[output_label],
163
  )
164
 
165
+
166
+ only_ai_check_btn.click(
167
+ fn=ai_generated_test,
168
+ inputs=[ai_label],
169
+ outputs=[bcLabel],
170
+ api_name="ai_check",
171
+ )
172
+
173
+ only_ai_check_btn2.click(
174
+ fn=ai_generated_test,
175
+ inputs=[output_label],
176
+ outputs=[bcLabel2]
177
+ )
178
+
179
+ humanizer_btn.click(on_first_button_click, inputs=None, outputs=humanized_markdown)
180
+ humanizer_btn.click(on_first_button_click, inputs=None, outputs=output_label)
181
+ humanizer_btn.click(on_first_button_click, inputs=None, outputs=only_ai_check_btn2)
182
+ humanizer_btn.click(on_first_button_click, inputs=None, outputs=bcLabel2)
183
+
184
  if __name__ == "__main__":
185
  demo.launch(demo.launch(server_name="0.0.0.0"))
humanize.py CHANGED
@@ -2,6 +2,7 @@ import torch
2
  from nltk import sent_tokenize
3
  import nltk
4
  from tqdm import tqdm
 
5
  from transformers import T5ForConditionalGeneration, T5Tokenizer
6
 
7
  nltk.download("punkt")
@@ -21,15 +22,15 @@ else:
21
  model_config = {
22
  "Base Model": "polygraf-ai/poly-humanizer-base",
23
  "Large Model": "polygraf-ai/poly-humanizer-large",
24
- "XL Model": {
25
- "path": "google/flan-t5-xl",
26
- "adapters": {
27
- "XL Model Adapter": "polygraf-ai/poly-humanizer-XL-adapter",
28
- "XL Law Model Adapter": "polygraf-ai/poly-humanizer-XL-law-adapter",
29
- "XL Marketing Model Adapter": "polygraf-ai/marketing-cleaned-13K-grad-acum-4-full",
30
- "XL Child Style Model Adapter": "polygraf-ai/poly-humanizer-XL-children-adapter-checkpoint-4000",
31
- },
32
- },
33
  }
34
 
35
  # cache the base models, tokenizers, and adapters
@@ -49,12 +50,15 @@ for name, config in model_config.items():
49
 
50
  def paraphrase_text(
51
  text,
 
52
  model_name="Base Model",
53
  temperature=1.2,
54
  repetition_penalty=1.0,
55
  top_k=50,
56
  length_penalty=1.0,
57
  ):
 
 
58
  # select the model, tokenizer and adapter
59
  if "XL" in model_name: # dynamic adapter load/unload for XL models
60
  # all adapter models use the XL model as the base
@@ -70,7 +74,7 @@ def paraphrase_text(
70
  # paraphrase each chunk of text
71
  sentences = sent_tokenize(text) # sentence boundary detection
72
  paraphrases = []
73
- for sentence in tqdm(sentences):
74
  sentence = sentence.strip()
75
  if len(sentence) == 0:
76
  continue
 
2
  from nltk import sent_tokenize
3
  import nltk
4
  from tqdm import tqdm
5
+ import gradio as gr
6
  from transformers import T5ForConditionalGeneration, T5Tokenizer
7
 
8
  nltk.download("punkt")
 
22
  model_config = {
23
  "Base Model": "polygraf-ai/poly-humanizer-base",
24
  "Large Model": "polygraf-ai/poly-humanizer-large",
25
+ # "XL Model": {
26
+ # "path": "google/flan-t5-xl",
27
+ # "adapters": {
28
+ # "XL Model Adapter": "polygraf-ai/poly-humanizer-XL-adapter",
29
+ # "XL Law Model Adapter": "polygraf-ai/poly-humanizer-XL-law-adapter",
30
+ # "XL Marketing Model Adapter": "polygraf-ai/marketing-cleaned-13K-grad-acum-4-full",
31
+ # "XL Child Style Model Adapter": "polygraf-ai/poly-humanizer-XL-children-adapter-checkpoint-4000",
32
+ # },
33
+ # },
34
  }
35
 
36
  # cache the base models, tokenizers, and adapters
 
50
 
51
  def paraphrase_text(
52
  text,
53
+ progress=gr.Progress(),
54
  model_name="Base Model",
55
  temperature=1.2,
56
  repetition_penalty=1.0,
57
  top_k=50,
58
  length_penalty=1.0,
59
  ):
60
+ progress(0, desc="Starting to Humanize")
61
+ progress(0.05)
62
  # select the model, tokenizer and adapter
63
  if "XL" in model_name: # dynamic adapter load/unload for XL models
64
  # all adapter models use the XL model as the base
 
74
  # paraphrase each chunk of text
75
  sentences = sent_tokenize(text) # sentence boundary detection
76
  paraphrases = []
77
+ for sentence in progress.tqdm(sentences, desc="Humanizing"):
78
  sentence = sentence.strip()
79
  if len(sentence) == 0:
80
  continue
test.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from pprint import pprint
3
+
4
+ response = requests.post(
5
+ "https://api.sapling.ai/api/v1/aidetect",
6
+ json={
7
+ "key": "60L9BPSVPIIOEZM0CD1DQWRBPJIUR7SB",
8
+ "text": "This is sample text."
9
+ }
10
+ )
11
+
12
+ pprint(response.json()['score'])