aliasgerovs commited on
Commit
78d5a17
·
2 Parent(s): 741a6fd e3277bc

Merge branch 'main' of https://huggingface.co/spaces/polygraf-ai/article_writer

Browse files
Files changed (1) hide show
  1. app.py +58 -17
app.py CHANGED
@@ -7,6 +7,18 @@ from ai_generate import generate
7
  import requests
8
  from gptzero_free import GPT2PPL
9
  import language_tool_python
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
 
12
  def clean_text(text: str) -> str:
@@ -83,12 +95,8 @@ def regenerate_prompt(settings: Dict[str, str]) -> str:
83
 
84
  Comments:
85
  - {settings['user_comments']}
86
-
87
- Additional requirements:
88
- - Include {settings['num_examples']} relevant examples or case studies
89
- - Incorporate data or statistics from {', '.join(settings['references'])}
90
- - End with a {settings['conclusion_type']} conclusion
91
- - Add a "References" section at the end with at least 3 credible sources, formatted as [1], [2], etc.
92
  - Do not make any headline, title bold.
93
 
94
  Ensure proper paragraph breaks for better readability.
@@ -181,13 +189,15 @@ def humanize(
181
 
182
 
183
  ai_check_options = [
184
- "Polygraf AI",
 
 
185
  # "Sapling AI",
186
  "GPTZero",
187
  ]
188
 
189
 
190
- def ai_generated_test_polygraf(text: str) -> Dict:
191
  url = "http://34.66.10.188/ai-vs-human"
192
  access_key = "6mcemwsFycVVgVjMFwKXki3zJka1r7N4u$Z0Y|x$gecC$hdNtpQf-SpL0+=k;u%BZ"
193
  headers = {"ACCESS_KEY": access_key}
@@ -207,18 +217,41 @@ def ai_generated_test_gptzero(text):
207
  gptzero_model = GPT2PPL()
208
  result = gptzero_model(text)
209
  print(result)
210
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
 
212
 
213
- def ai_check(text: str, option: str) -> Dict:
214
- if option == "Polygraf AI":
215
- return ai_generated_test_polygraf(text)
216
  elif option == "Sapling AI":
217
  return ai_generated_test_sapling(text)
218
  elif option == "GPTZero":
219
  return ai_generated_test_gptzero(text)
220
  else:
221
- return ai_generated_test_polygraf(text)
222
 
223
 
224
  def update_visibility_api(model: str):
@@ -236,7 +269,7 @@ def format_references(text: str) -> str:
236
  in_references = False
237
 
238
  for line in lines:
239
- if "references" in line.strip().lower():
240
  in_references = True
241
  continue
242
  if in_references:
@@ -246,8 +279,7 @@ def format_references(text: str) -> str:
246
 
247
  formatted_refs = []
248
  for i, ref in enumerate(references, 1):
249
- if f"[{i}]" in ref:
250
- ref = ref.replace(f"[{i}]", "")
251
  formatted_refs.append(f"[{i}] {ref}\n")
252
 
253
  return "\n\n".join(article_text) + "\n\nReferences:\n" + "\n".join(formatted_refs)
@@ -459,6 +491,7 @@ def create_interface():
459
  ai_check_btn = gr.Button("AI Check")
460
  ai_check_result = gr.Label(label="AI Check Result")
461
 
 
462
  humanize_btn = gr.Button("Humanize")
463
  # humanized_output = gr.Textbox(label="Humanized Article", lines=20, elem_classes=["custom-textbox"])
464
  humanized_output = gr.Markdown(label="Humanized Article", value="\n\n\n\n", render=True)
@@ -470,6 +503,13 @@ def create_interface():
470
  else:
471
  return gr.update(visible=False)
472
 
 
 
 
 
 
 
 
473
  output_article.change(become_visible, inputs=output_article, outputs=ai_comments)
474
  ai_comments.change(become_visible, inputs=output_article, outputs=regenerate_btn)
475
 
@@ -518,6 +558,7 @@ def create_interface():
518
  ],
519
  outputs=[output_article],
520
  )
 
521
  regenerate_btn.click(
522
  fn=generate_and_format,
523
  inputs=[
@@ -544,7 +585,7 @@ def create_interface():
544
  ai_check_btn.click(
545
  fn=ai_check,
546
  inputs=[output_article, ai_detector_dropdown],
547
- outputs=[ai_check_result],
548
  )
549
 
550
  humanize_btn.click(
 
7
  import requests
8
  from gptzero_free import GPT2PPL
9
  import language_tool_python
10
+ import re
11
+ from gradio_client import Client
12
+
13
+ highlighter_client = Client("polygraf-ai/highlighter_test")
14
+
15
+
16
+ def remove_bracketed_numbers(text):
17
+ # This regex pattern matches any occurrence of [{int}]
18
+ pattern = r"^\[\d+\]"
19
+ # re.sub() replaces all occurrences of the pattern with an empty string
20
+ cleaned_text = re.sub(pattern, "", text)
21
+ return cleaned_text
22
 
23
 
24
  def clean_text(text: str) -> str:
 
95
 
96
  Comments:
97
  - {settings['user_comments']}
98
+ - The originial content should not be changed. Make minor modifications based on user comments above.
99
+ - Keep the references the same as the given text in the same format.
 
 
 
 
100
  - Do not make any headline, title bold.
101
 
102
  Ensure proper paragraph breaks for better readability.
 
189
 
190
 
191
  ai_check_options = [
192
+ "Polygraf AI Base",
193
+ "Polygraf AI 2 Sentence Block",
194
+ "Polygraf AI 3 Sentence Block",
195
  # "Sapling AI",
196
  "GPTZero",
197
  ]
198
 
199
 
200
+ def ai_generated_test_polygraf(text: str):
201
  url = "http://34.66.10.188/ai-vs-human"
202
  access_key = "6mcemwsFycVVgVjMFwKXki3zJka1r7N4u$Z0Y|x$gecC$hdNtpQf-SpL0+=k;u%BZ"
203
  headers = {"ACCESS_KEY": access_key}
 
217
  gptzero_model = GPT2PPL()
218
  result = gptzero_model(text)
219
  print(result)
220
+ return result, None
221
+
222
+
223
+ def highlighter_polygraf(text, model="Polygraf AI Base"):
224
+ if model == "Polygraf AI Base":
225
+ result = highlighter_client.predict(text=text, model="BC Original", api_name="/process_text")
226
+ ai_score = result[0]["confidences"][0]["confidence"]
227
+ ai_res = {"AI": ai_score, "HUMAN": 1 - ai_score}
228
+ return ai_res, result[1]
229
+ elif model == "Polygraf AI 2 Sentence Block":
230
+ result = highlighter_client.predict(text=text, model="BC 2 Sentences", api_name="/process_text")
231
+ ai_score = result[0]["confidences"][0]["confidence"]
232
+ ai_res = {"AI": ai_score, "HUMAN": 1 - ai_score}
233
+ return ai_res, result[1]
234
+ elif model == "Polygraf AI 3 Sentence Block":
235
+ result = highlighter_client.predict(text=text, model="BC 3 Sentences", api_name="/process_text")
236
+ ai_score = result[0]["confidences"][0]["confidence"]
237
+ ai_res = {"AI": ai_score, "HUMAN": 1 - ai_score}
238
+ return ai_res, result[1]
239
+ else:
240
+ result = highlighter_client.predict(text=text, model="BC Original", api_name="/process_text")
241
+ ai_score = result[0]["confidences"][0]["confidence"]
242
+ ai_res = {"AI": ai_score, "HUMAN": 1 - ai_score}
243
+ return ai_res, result[1]
244
 
245
 
246
+ def ai_check(text: str, option: str):
247
+ if option.startswith("Polygraf AI"):
248
+ return highlighter_polygraf(text, option)
249
  elif option == "Sapling AI":
250
  return ai_generated_test_sapling(text)
251
  elif option == "GPTZero":
252
  return ai_generated_test_gptzero(text)
253
  else:
254
+ return highlighter_polygraf(text, option)
255
 
256
 
257
  def update_visibility_api(model: str):
 
269
  in_references = False
270
 
271
  for line in lines:
272
+ if line.strip().lower() == "references" or line.strip().lower() == "references:":
273
  in_references = True
274
  continue
275
  if in_references:
 
279
 
280
  formatted_refs = []
281
  for i, ref in enumerate(references, 1):
282
+ ref = remove_bracketed_numbers(ref)
 
283
  formatted_refs.append(f"[{i}] {ref}\n")
284
 
285
  return "\n\n".join(article_text) + "\n\nReferences:\n" + "\n".join(formatted_refs)
 
491
  ai_check_btn = gr.Button("AI Check")
492
  ai_check_result = gr.Label(label="AI Check Result")
493
 
494
+ highlighted_text = gr.HTML(label="Sentence Breakdown", visible=False)
495
  humanize_btn = gr.Button("Humanize")
496
  # humanized_output = gr.Textbox(label="Humanized Article", lines=20, elem_classes=["custom-textbox"])
497
  humanized_output = gr.Markdown(label="Humanized Article", value="\n\n\n\n", render=True)
 
503
  else:
504
  return gr.update(visible=False)
505
 
506
+ def highlight_visible(text):
507
+ if text.startswith("Polygraf"):
508
+ return gr.update(visible=True)
509
+ else:
510
+ return gr.update(visible=False)
511
+
512
+ ai_detector_dropdown.change(highlight_visible, inputs=ai_detector_dropdown, outputs=highlighted_text)
513
  output_article.change(become_visible, inputs=output_article, outputs=ai_comments)
514
  ai_comments.change(become_visible, inputs=output_article, outputs=regenerate_btn)
515
 
 
558
  ],
559
  outputs=[output_article],
560
  )
561
+
562
  regenerate_btn.click(
563
  fn=generate_and_format,
564
  inputs=[
 
585
  ai_check_btn.click(
586
  fn=ai_check,
587
  inputs=[output_article, ai_detector_dropdown],
588
+ outputs=[ai_check_result, highlighted_text],
589
  )
590
 
591
  humanize_btn.click(