minko186 commited on
Commit
7454788
·
1 Parent(s): 19b4151

add regeneration of ai text based on comments

Browse files
Files changed (1) hide show
  1. app.py +78 -5
app.py CHANGED
@@ -74,6 +74,29 @@ def generate_prompt(settings: Dict[str, str]) -> str:
74
  return prompt
75
 
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  def generate_article(
78
  topic: str,
79
  keywords: str,
@@ -89,6 +112,8 @@ def generate_article(
89
  conclusion_type: str,
90
  ai_model: str,
91
  api_key: str = None,
 
 
92
  ) -> str:
93
  """Generate an article based on user-defined settings."""
94
  settings = {
@@ -104,10 +129,16 @@ def generate_article(
104
  "references": [r.strip() for r in references.split(",")],
105
  "num_examples": num_examples,
106
  "conclusion_type": conclusion_type,
 
 
107
  }
108
 
109
- prompt = generate_prompt(settings)
 
 
 
110
 
 
111
  if ai_model in ["OpenAI GPT 3.5", "OpenAI GPT 4"]:
112
  response = openai.ChatCompletion.create(
113
  model="gpt-4" if ai_model == "OpenAI GPT 4" else "gpt-3.5-turbo",
@@ -205,7 +236,7 @@ def format_references(text: str) -> str:
205
  in_references = False
206
 
207
  for line in lines:
208
- if line.strip().lower() == "references":
209
  in_references = True
210
  continue
211
  if in_references:
@@ -215,6 +246,8 @@ def format_references(text: str) -> str:
215
 
216
  formatted_refs = []
217
  for i, ref in enumerate(references, 1):
 
 
218
  formatted_refs.append(f"[{i}] {ref}\n")
219
 
220
  return "\n\n".join(article_text) + "\n\nReferences:\n" + "\n".join(formatted_refs)
@@ -235,6 +268,8 @@ def generate_and_format(
235
  conclusion_type,
236
  ai_model,
237
  api_key,
 
 
238
  ):
239
  article = generate_article(
240
  topic,
@@ -251,6 +286,8 @@ def generate_and_format(
251
  conclusion_type,
252
  ai_model,
253
  api_key,
 
 
254
  )
255
  return format_references(article)
256
 
@@ -264,7 +301,9 @@ def create_interface():
264
  theme=gr.themes.Default(
265
  primary_hue=gr.themes.colors.pink, secondary_hue=gr.themes.colors.yellow, neutral_hue=gr.themes.colors.gray
266
  ),
267
- css=".custom-textbox textarea { white-space: pre-wrap; }",
 
 
268
  ) as demo:
269
  gr.Markdown("# Polygraf AI Content Writer", elem_classes="text-center text-3xl mb-6")
270
 
@@ -408,7 +447,10 @@ def create_interface():
408
 
409
  with gr.Column(scale=3):
410
  output_article = gr.Textbox(label="Generated Article", lines=20)
411
-
 
 
 
412
  with gr.Row():
413
  with gr.Column():
414
  ai_detector_dropdown = gr.Radio(
@@ -419,9 +461,18 @@ def create_interface():
419
 
420
  humanize_btn = gr.Button("Humanize")
421
  # humanized_output = gr.Textbox(label="Humanized Article", lines=20, elem_classes=["custom-textbox"])
422
- humanized_output = gr.Markdown(label="Humanized Article", height="24em")
423
  copy_to_input_btn = gr.Button("Copy to Input for AI Check")
424
 
 
 
 
 
 
 
 
 
 
425
  with gr.Accordion("Advanced Humanizer Settings", open=False):
426
  with gr.Row():
427
  model_dropdown = gr.Radio(
@@ -467,6 +518,28 @@ def create_interface():
467
  ],
468
  outputs=[output_article],
469
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
470
 
471
  ai_check_btn.click(
472
  fn=ai_check,
 
74
  return prompt
75
 
76
 
77
+ def regenerate_prompt(settings: Dict[str, str]) -> str:
78
+ """Edit the given text based on user settings and comments."""
79
+ prompt = f"""
80
+ "{settings['generated_article']}"
81
+
82
+ Edit the given text based on user comments.
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.
95
+ Avoid any references to artificial intelligence, language models, or the fact that this is generated by an AI, and do not mention something like here is the article etc.
96
+ """
97
+ return prompt
98
+
99
+
100
  def generate_article(
101
  topic: str,
102
  keywords: str,
 
112
  conclusion_type: str,
113
  ai_model: str,
114
  api_key: str = None,
115
+ generated_article: str = None,
116
+ user_comments: str = None,
117
  ) -> str:
118
  """Generate an article based on user-defined settings."""
119
  settings = {
 
129
  "references": [r.strip() for r in references.split(",")],
130
  "num_examples": num_examples,
131
  "conclusion_type": conclusion_type,
132
+ "generated_article": generated_article,
133
+ "user_comments": user_comments,
134
  }
135
 
136
+ if generated_article:
137
+ prompt = regenerate_prompt(settings)
138
+ else:
139
+ prompt = generate_prompt(settings)
140
 
141
+ print(prompt)
142
  if ai_model in ["OpenAI GPT 3.5", "OpenAI GPT 4"]:
143
  response = openai.ChatCompletion.create(
144
  model="gpt-4" if ai_model == "OpenAI GPT 4" else "gpt-3.5-turbo",
 
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
 
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)
 
268
  conclusion_type,
269
  ai_model,
270
  api_key,
271
+ generated_article: str = None,
272
+ user_comments: str = None,
273
  ):
274
  article = generate_article(
275
  topic,
 
286
  conclusion_type,
287
  ai_model,
288
  api_key,
289
+ generated_article,
290
+ user_comments,
291
  )
292
  return format_references(article)
293
 
 
301
  theme=gr.themes.Default(
302
  primary_hue=gr.themes.colors.pink, secondary_hue=gr.themes.colors.yellow, neutral_hue=gr.themes.colors.gray
303
  ),
304
+ css="""
305
+ .input-highlight-pink block_label {background-color: #008080}
306
+ """,
307
  ) as demo:
308
  gr.Markdown("# Polygraf AI Content Writer", elem_classes="text-center text-3xl mb-6")
309
 
 
447
 
448
  with gr.Column(scale=3):
449
  output_article = gr.Textbox(label="Generated Article", lines=20)
450
+ ai_comments = gr.Textbox(
451
+ label="Add comments to help edit generated text", interactive=True, visible=False
452
+ )
453
+ regenerate_btn = gr.Button("Regenerate Article", variant="primary", visible=False)
454
  with gr.Row():
455
  with gr.Column():
456
  ai_detector_dropdown = gr.Radio(
 
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", height="24em", render=True)
465
  copy_to_input_btn = gr.Button("Copy to Input for AI Check")
466
 
467
+ def become_visible(text):
468
+ if text:
469
+ return gr.update(visible=True)
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
+
476
  with gr.Accordion("Advanced Humanizer Settings", open=False):
477
  with gr.Row():
478
  model_dropdown = gr.Radio(
 
518
  ],
519
  outputs=[output_article],
520
  )
521
+ regenerate_btn.click(
522
+ fn=generate_and_format,
523
+ inputs=[
524
+ input_topic,
525
+ input_keywords,
526
+ input_length,
527
+ input_format,
528
+ input_writing_style,
529
+ input_tone,
530
+ input_user_category,
531
+ input_depth,
532
+ input_structure,
533
+ input_references,
534
+ input_num_examples,
535
+ input_conclusion,
536
+ ai_generator,
537
+ input_api,
538
+ output_article,
539
+ ai_comments,
540
+ ],
541
+ outputs=[output_article],
542
+ )
543
 
544
  ai_check_btn.click(
545
  fn=ai_check,