Spaces:
Runtime error
Runtime error
update humanize
Browse files- .gitignore +1 -0
- ai_generate.py +13 -17
- app.py +179 -93
- humanize.py +7 -6
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
_pycache_/
|
ai_generate.py
CHANGED
@@ -5,21 +5,19 @@ from transformers import pipeline
|
|
5 |
from groq import Groq
|
6 |
|
7 |
groq_client = Groq(
|
8 |
-
|
9 |
)
|
10 |
|
|
|
11 |
def generate_groq(text, model):
|
12 |
completion = groq_client.chat.completions.create(
|
13 |
-
model
|
14 |
messages=[
|
15 |
-
{
|
16 |
-
"role": "user",
|
17 |
-
"content": text
|
18 |
-
},
|
19 |
{
|
20 |
"role": "assistant",
|
21 |
-
"content": "Please follow the instruction and write about the given topic in approximately the given number of words"
|
22 |
-
}
|
23 |
],
|
24 |
temperature=1,
|
25 |
max_tokens=1024,
|
@@ -33,26 +31,24 @@ def generate_groq(text, model):
|
|
33 |
response += chunk.choices[0].delta.content or ""
|
34 |
return response
|
35 |
|
|
|
36 |
def generate_openai(text, model, openai_client):
|
37 |
-
message=[{"role": "user", "content": text}]
|
38 |
response = openai_client.chat.completions.create(
|
39 |
-
model=model,
|
40 |
-
messages = message,
|
41 |
-
temperature=0.2,
|
42 |
-
max_tokens=800,
|
43 |
-
frequency_penalty=0.0
|
44 |
)
|
45 |
return response.choices[0].message.content
|
46 |
|
|
|
47 |
def generate(text, model, api):
|
48 |
if model == "Llama 3":
|
49 |
return generate_groq(text, "llama3-70b-8192")
|
50 |
elif model == "Groq":
|
51 |
return generate_groq(text, "llama3-groq-70b-8192-tool-use-preview")
|
52 |
elif model == "Mistral":
|
53 |
-
return generate_groq(text, "mixtral-8x7b-32768")
|
54 |
elif model == "Gemma":
|
55 |
-
return generate_groq(text, "gemma2-9b-it")
|
56 |
elif model == "OpenAI GPT 3.5":
|
57 |
try:
|
58 |
openai_client = OpenAI(api_key=api)
|
@@ -70,4 +66,4 @@ def generate(text, model, api):
|
|
70 |
openai_client = OpenAI(api_key=api)
|
71 |
return generate_openai(text, "gpt-4o", openai_client)
|
72 |
except:
|
73 |
-
return "Please add a valid API key"
|
|
|
5 |
from groq import Groq
|
6 |
|
7 |
groq_client = Groq(
|
8 |
+
api_key=os.environ.get("groq_key"),
|
9 |
)
|
10 |
|
11 |
+
|
12 |
def generate_groq(text, model):
|
13 |
completion = groq_client.chat.completions.create(
|
14 |
+
model=model,
|
15 |
messages=[
|
16 |
+
{"role": "user", "content": text},
|
|
|
|
|
|
|
17 |
{
|
18 |
"role": "assistant",
|
19 |
+
"content": "Please follow the instruction and write about the given topic in approximately the given number of words",
|
20 |
+
},
|
21 |
],
|
22 |
temperature=1,
|
23 |
max_tokens=1024,
|
|
|
31 |
response += chunk.choices[0].delta.content or ""
|
32 |
return response
|
33 |
|
34 |
+
|
35 |
def generate_openai(text, model, openai_client):
|
36 |
+
message = [{"role": "user", "content": text}]
|
37 |
response = openai_client.chat.completions.create(
|
38 |
+
model=model, messages=message, temperature=0.2, max_tokens=800, frequency_penalty=0.0
|
|
|
|
|
|
|
|
|
39 |
)
|
40 |
return response.choices[0].message.content
|
41 |
|
42 |
+
|
43 |
def generate(text, model, api):
|
44 |
if model == "Llama 3":
|
45 |
return generate_groq(text, "llama3-70b-8192")
|
46 |
elif model == "Groq":
|
47 |
return generate_groq(text, "llama3-groq-70b-8192-tool-use-preview")
|
48 |
elif model == "Mistral":
|
49 |
+
return generate_groq(text, "mixtral-8x7b-32768")
|
50 |
elif model == "Gemma":
|
51 |
+
return generate_groq(text, "gemma2-9b-it")
|
52 |
elif model == "OpenAI GPT 3.5":
|
53 |
try:
|
54 |
openai_client = OpenAI(api_key=api)
|
|
|
66 |
openai_client = OpenAI(api_key=api)
|
67 |
return generate_openai(text, "gpt-4o", openai_client)
|
68 |
except:
|
69 |
+
return "Please add a valid API key"
|
app.py
CHANGED
@@ -9,13 +9,14 @@ from gptzero_free import GPT2PPL
|
|
9 |
|
10 |
|
11 |
def clean_text(text: str) -> str:
|
12 |
-
paragraphs = text.split(
|
13 |
cleaned_paragraphs = []
|
14 |
for paragraph in paragraphs:
|
15 |
-
cleaned = re.sub(r
|
16 |
-
cleaned = re.sub(r
|
17 |
cleaned_paragraphs.append(cleaned)
|
18 |
-
return
|
|
|
19 |
|
20 |
def format_and_correct(text: str) -> str:
|
21 |
"""Correct formatting and grammar without changing content significantly."""
|
@@ -23,7 +24,7 @@ def format_and_correct(text: str) -> str:
|
|
23 |
Please correct the formatting, grammar, and spelling errors in the following text without changing its content significantly. Ensure proper paragraph breaks and maintain the original content:
|
24 |
{text}
|
25 |
"""
|
26 |
-
corrected_text = generate(prompt, "Groq", None)
|
27 |
return clean_text(corrected_text)
|
28 |
|
29 |
|
@@ -56,6 +57,7 @@ def generate_prompt(settings: Dict[str, str]) -> str:
|
|
56 |
"""
|
57 |
return prompt
|
58 |
|
|
|
59 |
def generate_article(
|
60 |
topic: str,
|
61 |
keywords: str,
|
@@ -70,32 +72,35 @@ def generate_article(
|
|
70 |
num_examples: str,
|
71 |
conclusion_type: str,
|
72 |
ai_model: str,
|
73 |
-
api_key: str = None
|
74 |
) -> str:
|
75 |
"""Generate an article based on user-defined settings."""
|
76 |
settings = {
|
77 |
"topic": topic,
|
78 |
-
"keywords": [k.strip() for k in keywords.split(
|
79 |
"article_length": article_length,
|
80 |
"format": format,
|
81 |
"writing_style": writing_style,
|
82 |
"tone": tone,
|
83 |
"user_category": user_category,
|
84 |
"depth_of_content": depth_of_content,
|
85 |
-
"structure": [s.strip() for s in structure.split(
|
86 |
-
"references": [r.strip() for r in references.split(
|
87 |
"num_examples": num_examples,
|
88 |
-
"conclusion_type": conclusion_type
|
89 |
}
|
90 |
-
|
91 |
prompt = generate_prompt(settings)
|
92 |
-
|
93 |
-
if ai_model in [
|
94 |
response = openai.ChatCompletion.create(
|
95 |
-
model="gpt-4" if ai_model ==
|
96 |
messages=[
|
97 |
-
{
|
98 |
-
|
|
|
|
|
|
|
99 |
],
|
100 |
max_tokens=3000,
|
101 |
n=1,
|
@@ -105,16 +110,17 @@ def generate_article(
|
|
105 |
article = response.choices[0].message.content.strip()
|
106 |
else:
|
107 |
article = generate(prompt, ai_model, api_key)
|
108 |
-
|
109 |
return clean_text(article)
|
110 |
|
|
|
111 |
def humanize(
|
112 |
-
text: str,
|
113 |
-
model: str,
|
114 |
-
temperature: float = 1.2,
|
115 |
repetition_penalty: float = 1,
|
116 |
top_k: int = 50,
|
117 |
-
length_penalty: float = 1
|
118 |
) -> str:
|
119 |
result = paraphrase_text(
|
120 |
text=text,
|
@@ -126,33 +132,28 @@ def humanize(
|
|
126 |
)
|
127 |
return format_and_correct(result)
|
128 |
|
|
|
129 |
ai_check_options = [
|
130 |
"Polygraf AI",
|
131 |
# "Sapling AI",
|
132 |
-
"GPTZero"
|
133 |
]
|
134 |
|
|
|
135 |
def ai_generated_test_polygraf(text: str) -> Dict:
|
136 |
url = "http://34.66.10.188/ai-vs-human"
|
137 |
-
access_key = "6mcemwsFycVVgVjMFwKXki3zJka1r7N4u$Z0Y|x$gecC$hdNtpQf-SpL0+=k;u%BZ"
|
138 |
-
headers = {
|
139 |
-
|
140 |
-
}
|
141 |
-
data = {
|
142 |
-
"text" : f"{text}"
|
143 |
-
}
|
144 |
response = requests.post(url, headers=headers, json=data)
|
145 |
return response.json()
|
146 |
|
|
|
147 |
def ai_generated_test_sapling(text: str) -> Dict:
|
148 |
response = requests.post(
|
149 |
-
"https://api.sapling.ai/api/v1/aidetect",
|
150 |
-
json={
|
151 |
-
"key": "60L9BPSVPIIOEZM0CD1DQWRBPJIUR7SB",
|
152 |
-
"text": f"{text}"
|
153 |
-
}
|
154 |
)
|
155 |
-
return {
|
156 |
|
157 |
|
158 |
def ai_generated_test_gptzero(text):
|
@@ -161,29 +162,32 @@ def ai_generated_test_gptzero(text):
|
|
161 |
print(result)
|
162 |
return result
|
163 |
|
|
|
164 |
def ai_check(text: str, option: str) -> Dict:
|
165 |
-
if option ==
|
166 |
return ai_generated_test_polygraf(text)
|
167 |
-
elif option ==
|
168 |
return ai_generated_test_sapling(text)
|
169 |
elif option == "GPTZero":
|
170 |
return ai_generated_test_gptzero(text)
|
171 |
else:
|
172 |
return ai_generated_test_polygraf(text)
|
173 |
|
|
|
174 |
def update_visibility_api(model: str):
|
175 |
-
if model in [
|
176 |
return gr.update(visible=True)
|
177 |
else:
|
178 |
return gr.update(visible=False)
|
179 |
|
|
|
180 |
def format_references(text: str) -> str:
|
181 |
"""Extract and format references from the generated text."""
|
182 |
-
lines = text.split(
|
183 |
references = []
|
184 |
article_text = []
|
185 |
in_references = False
|
186 |
-
|
187 |
for line in lines:
|
188 |
if line.strip().lower() == "references":
|
189 |
in_references = True
|
@@ -192,138 +196,215 @@ def format_references(text: str) -> str:
|
|
192 |
references.append(line.strip())
|
193 |
else:
|
194 |
article_text.append(line)
|
195 |
-
|
196 |
formatted_refs = []
|
197 |
for i, ref in enumerate(references, 1):
|
198 |
formatted_refs.append(f"[{i}] {ref}\n")
|
199 |
-
|
200 |
return "\n\n".join(article_text) + "\n\nReferences:\n" + "\n".join(formatted_refs)
|
201 |
|
|
|
202 |
def generate_and_format(
|
203 |
-
topic,
|
204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
):
|
206 |
article = generate_article(
|
207 |
-
topic,
|
208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
)
|
210 |
return format_references(article)
|
211 |
|
|
|
212 |
def copy_to_input(text):
|
213 |
return text
|
214 |
|
|
|
215 |
def create_interface():
|
216 |
-
with gr.Blocks(
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
|
|
221 |
gr.Markdown("# Polygraf AI Content Writer", elem_classes="text-center text-3xl mb-6")
|
222 |
-
|
223 |
with gr.Row():
|
224 |
with gr.Column(scale=2):
|
225 |
with gr.Group():
|
226 |
gr.Markdown("## Article Configuration", elem_classes="text-xl mb-4")
|
227 |
-
input_topic = gr.Textbox(
|
228 |
-
|
229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
230 |
with gr.Row():
|
231 |
input_format = gr.Dropdown(
|
232 |
-
choices=[
|
233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
234 |
label="Format",
|
235 |
-
elem_classes="input-highlight-turquoise"
|
236 |
)
|
237 |
input_length = gr.Dropdown(
|
238 |
-
choices=[
|
|
|
|
|
|
|
|
|
|
|
239 |
value="Medium (1000 words)",
|
240 |
label="Article Length",
|
241 |
-
elem_classes="input-highlight-pink"
|
242 |
)
|
243 |
-
|
244 |
with gr.Row():
|
245 |
input_writing_style = gr.Dropdown(
|
246 |
-
choices=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
247 |
value="Formal",
|
248 |
label="Writing Style",
|
249 |
-
elem_classes="input-highlight-yellow"
|
250 |
)
|
251 |
input_tone = gr.Dropdown(
|
252 |
choices=["Friendly", "Professional", "Neutral", "Enthusiastic", "Skeptical", "Humorous"],
|
253 |
value="Professional",
|
254 |
label="Tone",
|
255 |
-
elem_classes="input-highlight-turquoise"
|
256 |
)
|
257 |
-
|
258 |
input_user_category = gr.Dropdown(
|
259 |
-
choices=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
value="General Public",
|
261 |
label="Target Audience",
|
262 |
-
elem_classes="input-highlight-pink"
|
263 |
)
|
264 |
input_depth = gr.Dropdown(
|
265 |
-
choices=[
|
|
|
|
|
|
|
|
|
|
|
266 |
value="Moderate analysis",
|
267 |
label="Depth of Content",
|
268 |
-
elem_classes="input-highlight-yellow"
|
269 |
)
|
270 |
input_structure = gr.Dropdown(
|
271 |
choices=[
|
272 |
"Introduction, Body, Conclusion",
|
273 |
"Abstract, Introduction, Methods, Results, Discussion, Conclusion",
|
274 |
"Executive Summary, Problem Statement, Analysis, Recommendations, Conclusion",
|
275 |
-
"Introduction, Literature Review, Methodology, Findings, Analysis, Conclusion"
|
276 |
],
|
277 |
value="Introduction, Body, Conclusion",
|
278 |
label="Structure",
|
279 |
-
elem_classes="input-highlight-turquoise"
|
280 |
)
|
281 |
input_references = gr.Dropdown(
|
282 |
-
choices=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
value="News outlets",
|
284 |
label="References",
|
285 |
-
elem_classes="input-highlight-pink"
|
286 |
)
|
287 |
input_num_examples = gr.Dropdown(
|
288 |
choices=["1-2", "3-4", "5+"],
|
289 |
value="1-2",
|
290 |
label="Number of Examples/Case Studies",
|
291 |
-
elem_classes="input-highlight-yellow"
|
292 |
)
|
293 |
input_conclusion = gr.Dropdown(
|
294 |
choices=["Summary", "Call to Action", "Future Outlook", "Thought-provoking Question"],
|
295 |
value="Summary",
|
296 |
label="Conclusion Type",
|
297 |
-
elem_classes="input-highlight-turquoise"
|
298 |
)
|
299 |
-
|
300 |
with gr.Group():
|
301 |
gr.Markdown("## AI Model Configuration", elem_classes="text-xl mb-4")
|
302 |
ai_generator = gr.Dropdown(
|
303 |
-
choices=[
|
304 |
-
value=
|
305 |
label="AI Model",
|
306 |
-
elem_classes="input-highlight-pink"
|
307 |
)
|
308 |
input_api = gr.Textbox(label="API Key", visible=False)
|
309 |
ai_generator.change(update_visibility_api, ai_generator, input_api)
|
310 |
-
|
311 |
generate_btn = gr.Button("Generate Article", variant="primary")
|
312 |
-
|
313 |
with gr.Column(scale=3):
|
314 |
output_article = gr.Textbox(label="Generated Article", lines=20)
|
315 |
-
|
316 |
with gr.Row():
|
317 |
with gr.Column():
|
318 |
ai_detector_dropdown = gr.Radio(
|
319 |
-
choices=ai_check_options, label="Select AI Detector", value="Polygraf AI"
|
|
|
320 |
ai_check_btn = gr.Button("AI Check")
|
321 |
ai_check_result = gr.Label(label="AI Check Result")
|
322 |
-
|
323 |
humanize_btn = gr.Button("Humanize")
|
324 |
-
humanized_output = gr.Textbox(label="Humanized Article", lines=20)
|
325 |
copy_to_input_btn = gr.Button("Copy to Input for AI Check")
|
326 |
-
|
327 |
with gr.Accordion("Advanced Humanizer Settings", open=False):
|
328 |
with gr.Row():
|
329 |
model_dropdown = gr.Radio(
|
@@ -334,21 +415,25 @@ def create_interface():
|
|
334 |
# "XL Law Model",
|
335 |
# "XL Marketing Model",
|
336 |
# "XL Child Style Model",
|
337 |
-
],
|
338 |
value="Large Model",
|
339 |
-
label="Humanizer Model Version"
|
340 |
)
|
341 |
with gr.Row():
|
342 |
temperature_slider = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, value=1.2, label="Temperature")
|
343 |
top_k_slider = gr.Slider(minimum=0, maximum=300, step=25, value=50, label="Top k")
|
344 |
with gr.Row():
|
345 |
-
repetition_penalty_slider = gr.Slider(
|
346 |
-
|
|
|
|
|
|
|
|
|
347 |
|
348 |
generate_btn.click(
|
349 |
fn=generate_and_format,
|
350 |
inputs=[
|
351 |
-
input_topic,
|
352 |
input_keywords,
|
353 |
input_length,
|
354 |
input_format,
|
@@ -361,7 +446,7 @@ def create_interface():
|
|
361 |
input_num_examples,
|
362 |
input_conclusion,
|
363 |
ai_generator,
|
364 |
-
input_api
|
365 |
],
|
366 |
outputs=[output_article],
|
367 |
)
|
@@ -393,6 +478,7 @@ def create_interface():
|
|
393 |
|
394 |
return demo
|
395 |
|
|
|
396 |
if __name__ == "__main__":
|
397 |
demo = create_interface()
|
398 |
-
demo.launch(server_name="0.0.0.0", share=True)
|
|
|
9 |
|
10 |
|
11 |
def clean_text(text: str) -> str:
|
12 |
+
paragraphs = text.split("\n\n")
|
13 |
cleaned_paragraphs = []
|
14 |
for paragraph in paragraphs:
|
15 |
+
cleaned = re.sub(r"\s+", " ", paragraph).strip()
|
16 |
+
cleaned = re.sub(r"(?<=\.) ([a-z])", lambda x: x.group(1).upper(), cleaned)
|
17 |
cleaned_paragraphs.append(cleaned)
|
18 |
+
return "\n".join(cleaned_paragraphs)
|
19 |
+
|
20 |
|
21 |
def format_and_correct(text: str) -> str:
|
22 |
"""Correct formatting and grammar without changing content significantly."""
|
|
|
24 |
Please correct the formatting, grammar, and spelling errors in the following text without changing its content significantly. Ensure proper paragraph breaks and maintain the original content:
|
25 |
{text}
|
26 |
"""
|
27 |
+
corrected_text = generate(prompt, "Groq", None)
|
28 |
return clean_text(corrected_text)
|
29 |
|
30 |
|
|
|
57 |
"""
|
58 |
return prompt
|
59 |
|
60 |
+
|
61 |
def generate_article(
|
62 |
topic: str,
|
63 |
keywords: str,
|
|
|
72 |
num_examples: str,
|
73 |
conclusion_type: str,
|
74 |
ai_model: str,
|
75 |
+
api_key: str = None,
|
76 |
) -> str:
|
77 |
"""Generate an article based on user-defined settings."""
|
78 |
settings = {
|
79 |
"topic": topic,
|
80 |
+
"keywords": [k.strip() for k in keywords.split(",")],
|
81 |
"article_length": article_length,
|
82 |
"format": format,
|
83 |
"writing_style": writing_style,
|
84 |
"tone": tone,
|
85 |
"user_category": user_category,
|
86 |
"depth_of_content": depth_of_content,
|
87 |
+
"structure": [s.strip() for s in structure.split(",")],
|
88 |
+
"references": [r.strip() for r in references.split(",")],
|
89 |
"num_examples": num_examples,
|
90 |
+
"conclusion_type": conclusion_type,
|
91 |
}
|
92 |
+
|
93 |
prompt = generate_prompt(settings)
|
94 |
+
|
95 |
+
if ai_model in ["OpenAI GPT 3.5", "OpenAI GPT 4"]:
|
96 |
response = openai.ChatCompletion.create(
|
97 |
+
model="gpt-4" if ai_model == "OpenAI GPT 4" else "gpt-3.5-turbo",
|
98 |
messages=[
|
99 |
+
{
|
100 |
+
"role": "system",
|
101 |
+
"content": "You are a professional content writer with expertise in various fields.",
|
102 |
+
},
|
103 |
+
{"role": "user", "content": prompt},
|
104 |
],
|
105 |
max_tokens=3000,
|
106 |
n=1,
|
|
|
110 |
article = response.choices[0].message.content.strip()
|
111 |
else:
|
112 |
article = generate(prompt, ai_model, api_key)
|
113 |
+
|
114 |
return clean_text(article)
|
115 |
|
116 |
+
|
117 |
def humanize(
|
118 |
+
text: str,
|
119 |
+
model: str,
|
120 |
+
temperature: float = 1.2,
|
121 |
repetition_penalty: float = 1,
|
122 |
top_k: int = 50,
|
123 |
+
length_penalty: float = 1,
|
124 |
) -> str:
|
125 |
result = paraphrase_text(
|
126 |
text=text,
|
|
|
132 |
)
|
133 |
return format_and_correct(result)
|
134 |
|
135 |
+
|
136 |
ai_check_options = [
|
137 |
"Polygraf AI",
|
138 |
# "Sapling AI",
|
139 |
+
"GPTZero",
|
140 |
]
|
141 |
|
142 |
+
|
143 |
def ai_generated_test_polygraf(text: str) -> Dict:
|
144 |
url = "http://34.66.10.188/ai-vs-human"
|
145 |
+
access_key = "6mcemwsFycVVgVjMFwKXki3zJka1r7N4u$Z0Y|x$gecC$hdNtpQf-SpL0+=k;u%BZ"
|
146 |
+
headers = {"ACCESS_KEY": access_key}
|
147 |
+
data = {"text": f"{text}"}
|
|
|
|
|
|
|
|
|
148 |
response = requests.post(url, headers=headers, json=data)
|
149 |
return response.json()
|
150 |
|
151 |
+
|
152 |
def ai_generated_test_sapling(text: str) -> Dict:
|
153 |
response = requests.post(
|
154 |
+
"https://api.sapling.ai/api/v1/aidetect", json={"key": "60L9BPSVPIIOEZM0CD1DQWRBPJIUR7SB", "text": f"{text}"}
|
|
|
|
|
|
|
|
|
155 |
)
|
156 |
+
return {"AI": response.json()["score"], "HUMAN": 1 - response.json()["score"]}
|
157 |
|
158 |
|
159 |
def ai_generated_test_gptzero(text):
|
|
|
162 |
print(result)
|
163 |
return result
|
164 |
|
165 |
+
|
166 |
def ai_check(text: str, option: str) -> Dict:
|
167 |
+
if option == "Polygraf AI":
|
168 |
return ai_generated_test_polygraf(text)
|
169 |
+
elif option == "Sapling AI":
|
170 |
return ai_generated_test_sapling(text)
|
171 |
elif option == "GPTZero":
|
172 |
return ai_generated_test_gptzero(text)
|
173 |
else:
|
174 |
return ai_generated_test_polygraf(text)
|
175 |
|
176 |
+
|
177 |
def update_visibility_api(model: str):
|
178 |
+
if model in ["OpenAI GPT 3.5", "OpenAI GPT 4"]:
|
179 |
return gr.update(visible=True)
|
180 |
else:
|
181 |
return gr.update(visible=False)
|
182 |
|
183 |
+
|
184 |
def format_references(text: str) -> str:
|
185 |
"""Extract and format references from the generated text."""
|
186 |
+
lines = text.split("\n")
|
187 |
references = []
|
188 |
article_text = []
|
189 |
in_references = False
|
190 |
+
|
191 |
for line in lines:
|
192 |
if line.strip().lower() == "references":
|
193 |
in_references = True
|
|
|
196 |
references.append(line.strip())
|
197 |
else:
|
198 |
article_text.append(line)
|
199 |
+
|
200 |
formatted_refs = []
|
201 |
for i, ref in enumerate(references, 1):
|
202 |
formatted_refs.append(f"[{i}] {ref}\n")
|
203 |
+
|
204 |
return "\n\n".join(article_text) + "\n\nReferences:\n" + "\n".join(formatted_refs)
|
205 |
|
206 |
+
|
207 |
def generate_and_format(
|
208 |
+
topic,
|
209 |
+
keywords,
|
210 |
+
article_length,
|
211 |
+
format,
|
212 |
+
writing_style,
|
213 |
+
tone,
|
214 |
+
user_category,
|
215 |
+
depth_of_content,
|
216 |
+
structure,
|
217 |
+
references,
|
218 |
+
num_examples,
|
219 |
+
conclusion_type,
|
220 |
+
ai_model,
|
221 |
+
api_key,
|
222 |
):
|
223 |
article = generate_article(
|
224 |
+
topic,
|
225 |
+
keywords,
|
226 |
+
article_length,
|
227 |
+
format,
|
228 |
+
writing_style,
|
229 |
+
tone,
|
230 |
+
user_category,
|
231 |
+
depth_of_content,
|
232 |
+
structure,
|
233 |
+
references,
|
234 |
+
num_examples,
|
235 |
+
conclusion_type,
|
236 |
+
ai_model,
|
237 |
+
api_key,
|
238 |
)
|
239 |
return format_references(article)
|
240 |
|
241 |
+
|
242 |
def copy_to_input(text):
|
243 |
return text
|
244 |
|
245 |
+
|
246 |
def create_interface():
|
247 |
+
with gr.Blocks(
|
248 |
+
theme=gr.themes.Default(
|
249 |
+
primary_hue=gr.themes.colors.pink, secondary_hue=gr.themes.colors.yellow, neutral_hue=gr.themes.colors.gray
|
250 |
+
),
|
251 |
+
css=".custom-textbox textarea { white-space: pre-wrap; }",
|
252 |
+
) as demo:
|
253 |
gr.Markdown("# Polygraf AI Content Writer", elem_classes="text-center text-3xl mb-6")
|
254 |
+
|
255 |
with gr.Row():
|
256 |
with gr.Column(scale=2):
|
257 |
with gr.Group():
|
258 |
gr.Markdown("## Article Configuration", elem_classes="text-xl mb-4")
|
259 |
+
input_topic = gr.Textbox(
|
260 |
+
label="Topic",
|
261 |
+
placeholder="Enter the main topic of your article",
|
262 |
+
elem_classes="input-highlight-pink",
|
263 |
+
)
|
264 |
+
input_keywords = gr.Textbox(
|
265 |
+
label="Keywords",
|
266 |
+
placeholder="Enter comma-separated keywords",
|
267 |
+
elem_classes="input-highlight-yellow",
|
268 |
+
)
|
269 |
+
|
270 |
with gr.Row():
|
271 |
input_format = gr.Dropdown(
|
272 |
+
choices=[
|
273 |
+
"Article",
|
274 |
+
"Essay",
|
275 |
+
"Blog post",
|
276 |
+
"Report",
|
277 |
+
"Research paper",
|
278 |
+
"News article",
|
279 |
+
"White paper",
|
280 |
+
],
|
281 |
+
value="Article",
|
282 |
label="Format",
|
283 |
+
elem_classes="input-highlight-turquoise",
|
284 |
)
|
285 |
input_length = gr.Dropdown(
|
286 |
+
choices=[
|
287 |
+
"Short (500 words)",
|
288 |
+
"Medium (1000 words)",
|
289 |
+
"Long (2000+ words)",
|
290 |
+
"Very Long (3000+ words)",
|
291 |
+
],
|
292 |
value="Medium (1000 words)",
|
293 |
label="Article Length",
|
294 |
+
elem_classes="input-highlight-pink",
|
295 |
)
|
296 |
+
|
297 |
with gr.Row():
|
298 |
input_writing_style = gr.Dropdown(
|
299 |
+
choices=[
|
300 |
+
"Formal",
|
301 |
+
"Informal",
|
302 |
+
"Technical",
|
303 |
+
"Conversational",
|
304 |
+
"Journalistic",
|
305 |
+
"Academic",
|
306 |
+
"Creative",
|
307 |
+
],
|
308 |
value="Formal",
|
309 |
label="Writing Style",
|
310 |
+
elem_classes="input-highlight-yellow",
|
311 |
)
|
312 |
input_tone = gr.Dropdown(
|
313 |
choices=["Friendly", "Professional", "Neutral", "Enthusiastic", "Skeptical", "Humorous"],
|
314 |
value="Professional",
|
315 |
label="Tone",
|
316 |
+
elem_classes="input-highlight-turquoise",
|
317 |
)
|
318 |
+
|
319 |
input_user_category = gr.Dropdown(
|
320 |
+
choices=[
|
321 |
+
"Students",
|
322 |
+
"Professionals",
|
323 |
+
"Researchers",
|
324 |
+
"General Public",
|
325 |
+
"Policymakers",
|
326 |
+
"Entrepreneurs",
|
327 |
+
],
|
328 |
value="General Public",
|
329 |
label="Target Audience",
|
330 |
+
elem_classes="input-highlight-pink",
|
331 |
)
|
332 |
input_depth = gr.Dropdown(
|
333 |
+
choices=[
|
334 |
+
"Surface-level overview",
|
335 |
+
"Moderate analysis",
|
336 |
+
"In-depth research",
|
337 |
+
"Comprehensive study",
|
338 |
+
],
|
339 |
value="Moderate analysis",
|
340 |
label="Depth of Content",
|
341 |
+
elem_classes="input-highlight-yellow",
|
342 |
)
|
343 |
input_structure = gr.Dropdown(
|
344 |
choices=[
|
345 |
"Introduction, Body, Conclusion",
|
346 |
"Abstract, Introduction, Methods, Results, Discussion, Conclusion",
|
347 |
"Executive Summary, Problem Statement, Analysis, Recommendations, Conclusion",
|
348 |
+
"Introduction, Literature Review, Methodology, Findings, Analysis, Conclusion",
|
349 |
],
|
350 |
value="Introduction, Body, Conclusion",
|
351 |
label="Structure",
|
352 |
+
elem_classes="input-highlight-turquoise",
|
353 |
)
|
354 |
input_references = gr.Dropdown(
|
355 |
+
choices=[
|
356 |
+
"Academic journals",
|
357 |
+
"Industry reports",
|
358 |
+
"Government publications",
|
359 |
+
"News outlets",
|
360 |
+
"Expert interviews",
|
361 |
+
"Case studies",
|
362 |
+
],
|
363 |
value="News outlets",
|
364 |
label="References",
|
365 |
+
elem_classes="input-highlight-pink",
|
366 |
)
|
367 |
input_num_examples = gr.Dropdown(
|
368 |
choices=["1-2", "3-4", "5+"],
|
369 |
value="1-2",
|
370 |
label="Number of Examples/Case Studies",
|
371 |
+
elem_classes="input-highlight-yellow",
|
372 |
)
|
373 |
input_conclusion = gr.Dropdown(
|
374 |
choices=["Summary", "Call to Action", "Future Outlook", "Thought-provoking Question"],
|
375 |
value="Summary",
|
376 |
label="Conclusion Type",
|
377 |
+
elem_classes="input-highlight-turquoise",
|
378 |
)
|
379 |
+
|
380 |
with gr.Group():
|
381 |
gr.Markdown("## AI Model Configuration", elem_classes="text-xl mb-4")
|
382 |
ai_generator = gr.Dropdown(
|
383 |
+
choices=["Llama 3", "Groq", "Mistral", "Gemma", "OpenAI GPT 3.5", "OpenAI GPT 4"],
|
384 |
+
value="Llama 3",
|
385 |
label="AI Model",
|
386 |
+
elem_classes="input-highlight-pink",
|
387 |
)
|
388 |
input_api = gr.Textbox(label="API Key", visible=False)
|
389 |
ai_generator.change(update_visibility_api, ai_generator, input_api)
|
390 |
+
|
391 |
generate_btn = gr.Button("Generate Article", variant="primary")
|
392 |
+
|
393 |
with gr.Column(scale=3):
|
394 |
output_article = gr.Textbox(label="Generated Article", lines=20)
|
395 |
+
|
396 |
with gr.Row():
|
397 |
with gr.Column():
|
398 |
ai_detector_dropdown = gr.Radio(
|
399 |
+
choices=ai_check_options, label="Select AI Detector", value="Polygraf AI"
|
400 |
+
)
|
401 |
ai_check_btn = gr.Button("AI Check")
|
402 |
ai_check_result = gr.Label(label="AI Check Result")
|
403 |
+
|
404 |
humanize_btn = gr.Button("Humanize")
|
405 |
+
humanized_output = gr.Textbox(label="Humanized Article", lines=20, elem_classes=["custom-textbox"])
|
406 |
copy_to_input_btn = gr.Button("Copy to Input for AI Check")
|
407 |
+
|
408 |
with gr.Accordion("Advanced Humanizer Settings", open=False):
|
409 |
with gr.Row():
|
410 |
model_dropdown = gr.Radio(
|
|
|
415 |
# "XL Law Model",
|
416 |
# "XL Marketing Model",
|
417 |
# "XL Child Style Model",
|
418 |
+
],
|
419 |
value="Large Model",
|
420 |
+
label="Humanizer Model Version",
|
421 |
)
|
422 |
with gr.Row():
|
423 |
temperature_slider = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, value=1.2, label="Temperature")
|
424 |
top_k_slider = gr.Slider(minimum=0, maximum=300, step=25, value=50, label="Top k")
|
425 |
with gr.Row():
|
426 |
+
repetition_penalty_slider = gr.Slider(
|
427 |
+
minimum=1.0, maximum=2.0, step=0.1, value=1, label="Repetition Penalty"
|
428 |
+
)
|
429 |
+
length_penalty_slider = gr.Slider(
|
430 |
+
minimum=0.0, maximum=2.0, step=0.1, value=1.0, label="Length Penalty"
|
431 |
+
)
|
432 |
|
433 |
generate_btn.click(
|
434 |
fn=generate_and_format,
|
435 |
inputs=[
|
436 |
+
input_topic,
|
437 |
input_keywords,
|
438 |
input_length,
|
439 |
input_format,
|
|
|
446 |
input_num_examples,
|
447 |
input_conclusion,
|
448 |
ai_generator,
|
449 |
+
input_api,
|
450 |
],
|
451 |
outputs=[output_article],
|
452 |
)
|
|
|
478 |
|
479 |
return demo
|
480 |
|
481 |
+
|
482 |
if __name__ == "__main__":
|
483 |
demo = create_interface()
|
484 |
+
demo.launch(server_name="0.0.0.0", share=True)
|
humanize.py
CHANGED
@@ -7,7 +7,7 @@ from transformers import T5ForConditionalGeneration, T5Tokenizer
|
|
7 |
|
8 |
nltk.download("punkt")
|
9 |
# autodetect the available device
|
10 |
-
GPU_IDX =
|
11 |
if torch.cuda.is_available():
|
12 |
num_gpus = torch.cuda.device_count()
|
13 |
print(f"Number of available GPUs: {num_gpus}")
|
@@ -75,11 +75,11 @@ def paraphrase_text(
|
|
75 |
paragraphs = text.split("\n")
|
76 |
humanized_paragraphs = []
|
77 |
|
78 |
-
for paragraph in paragraphs:
|
79 |
# paraphrase each chunk of text
|
80 |
sentences = sent_tokenize(paragraph)
|
81 |
paraphrases = []
|
82 |
-
for sentence in
|
83 |
sentence = sentence.strip()
|
84 |
if len(sentence) == 0:
|
85 |
continue
|
@@ -98,10 +98,11 @@ def paraphrase_text(
|
|
98 |
)
|
99 |
paraphrased_sentence = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
100 |
paraphrases.append(paraphrased_sentence)
|
101 |
-
print(f"\nOriginal: {sentence}")
|
102 |
-
print(f"Paraphrased: {paraphrased_sentence}")
|
103 |
combined_paraphrase = " ".join(paraphrases)
|
104 |
humanized_paragraphs.append(combined_paraphrase)
|
105 |
|
106 |
-
humanized_text = "\n".join(humanized_paragraphs)
|
|
|
107 |
return humanized_text
|
|
|
7 |
|
8 |
nltk.download("punkt")
|
9 |
# autodetect the available device
|
10 |
+
GPU_IDX = 0 # which GPU to use
|
11 |
if torch.cuda.is_available():
|
12 |
num_gpus = torch.cuda.device_count()
|
13 |
print(f"Number of available GPUs: {num_gpus}")
|
|
|
75 |
paragraphs = text.split("\n")
|
76 |
humanized_paragraphs = []
|
77 |
|
78 |
+
for paragraph in progress.tqdm(paragraphs, desc="Humanizing"):
|
79 |
# paraphrase each chunk of text
|
80 |
sentences = sent_tokenize(paragraph)
|
81 |
paraphrases = []
|
82 |
+
for sentence in sentences:
|
83 |
sentence = sentence.strip()
|
84 |
if len(sentence) == 0:
|
85 |
continue
|
|
|
98 |
)
|
99 |
paraphrased_sentence = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
100 |
paraphrases.append(paraphrased_sentence)
|
101 |
+
# print(f"\nOriginal: {sentence}")
|
102 |
+
# print(f"Paraphrased: {paraphrased_sentence}")
|
103 |
combined_paraphrase = " ".join(paraphrases)
|
104 |
humanized_paragraphs.append(combined_paraphrase)
|
105 |
|
106 |
+
humanized_text = "\n \n".join(humanized_paragraphs)
|
107 |
+
print(humanized_text)
|
108 |
return humanized_text
|