article_writer / app.py
minko186's picture
Update app.py
214c731 verified
raw
history blame
8.63 kB
import gradio as gr
from humanize import paraphrase_text
from gradio_client import Client
from ai_generate import generate
import requests
def on_first_button_click():
return gr.update(visible=True)
def ai_article_generator(
topic, text_type, length, ai_model, api = None
):
prompt = f"Write a {text_type} about the topic in about {length} words: {topic}"
print(prompt)
ai_text = generate(f"Write an article about the topic: {prompt}", ai_model, api)
return ai_text
def humanize(
text,
model,
temperature=1.2,
repetition_penalty=1,
top_k=50,
length_penalty=1):
result = paraphrase_text(
text=text,
model_name=model,
temperature=temperature,
repetition_penalty=repetition_penalty,
top_k=top_k,
length_penalty=length_penalty,
)
return result
def ai_generated_test(text):
url = "http://34.66.10.188/ai-vs-human"
access_key = "6mcemwsFycVVgVjMFwKXki3zJka1r7N4u$Z0Y|x$gecC$hdNtpQf-SpL0+=k;u%BZ"
headers = {
"ACCESS_KEY": access_key
}
data = {
"text" : f"{text}"
}
response = requests.post(url, headers=headers, json=data)
return response.json()
def ai_generated_test_sapling(text):
response = requests.post(
"https://api.sapling.ai/api/v1/aidetect",
json={
"key": "60L9BPSVPIIOEZM0CD1DQWRBPJIUR7SB",
"text": f"{text}"
}
)
return { "AI" : response.json()['score'], "HUMAN" : 1 - response.json()['score']}
def ai_check(text, option):
if option == 'Polygraf AI':
return ai_generated_test(text)
elif option == 'Sapling AI':
return ai_generated_test_sapling(text)
else :
return ai_generated_test(text)
def update_visibility_api(model):
if model in ['OpenAI GPT 3.5', 'OpenAI GPT 4']:
return gr.update(visible=True)
else:
return gr.update(visible=False)
with gr.Blocks() as demo:
gr.Markdown("# Polygraf Article Writer")
with gr.Row():
with gr.Column(scale=0.7):
gr.Markdown("## Enter a topic to write an article about:")
input_topic = gr.Textbox(label="Topic")
with gr.Row():
with gr.Column():
input_text_type = gr.Dropdown(
choices = ['article', 'essay', 'blog', 'report', 'letter'],
value = 'article',
label="Choose text_type of text:"
)
with gr.Column():
input_length = gr.Number(value=200, label="Choose length of the text:")
with gr.Row():
ai_generator = gr.Dropdown(
choices = ['Llama 3', 'Groq', 'Mistral', 'Gemma', 'OpenAI GPT 3.5', 'OpenAI GPT 4', 'OpenAI GPT 4o'],
value = 'Llama 3',
label="Choose AI Model"
)
input_api = gr.Textbox(label="API Key", visible=False)
ai_generator.change(update_visibility_api, ai_generator, input_api)
ai_article_btn = gr.Button("Generate", visible=True)
ai_generated = gr.Markdown("### Generated article:", visible=False)
ai_label = gr.HTML(label="AI", visible=False)
ai_detector_dropdown = gr.Radio(
choices=[
"Polygraf AI",
"Sapling AI",
],label="Select AI Detector", visible=False)
only_ai_check_btn = gr.Button("AI Check", visible= False)
bcLabel = gr.Label(label="Source", visible= False)
humanizer_btn = gr.Button("Humanize", visible=False)
model_dropdown = gr.Radio(
choices=[
"Base Model",
"Large Model",
"XL Model",
"XL Law Model",
"XL Marketing Model",
"XL Child Style Model",
],
value="Large Model",
label="Select Model Version",
visible=False)
humanized_markdown = gr.Markdown("### Humanized article:", visible=False)
output_label = gr.HTML(label="Humanized", visible= False)
only_ai_check_btn2 = gr.Button("AI Check", visible= False)
bcLabel2 = gr.Label(label="Source", visible= False)
with gr.Column(scale=0.3):
temperature_slider = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, value=1.2, label="Temperature", visible= False)
controls_markdown = gr.Markdown("Controls the randomness of the paraphrase. Higher values generate more varied text.", visible= False)
top_k_slider = gr.Slider(
minimum=0,
maximum=300,
step=25,
value=50,
label="Top k", visible= False
)
top_token_markdown = gr.Markdown("Limits the number of top tokens considered during generation.", visible= False)
repetition_penalty_slider = gr.Slider(
minimum=1.0,
maximum=2.0,
step=0.1,
value=1,
label="Repetition Penalty", visible= False
)
penalize_repeated_markdown = gr.Markdown("Penalizes repeated words to encourage diverse language use", visible= False)
length_penalty_slider = gr.Slider(
minimum=0.0,
maximum=2.0,
step=0.1,
value=1.0,
label="Length Penalty", visible= False
)
penalize_markdown = gr.Markdown("Penalizes shorter outputs.", visible= False)
ai_article_btn.click(
fn=ai_article_generator,
inputs=[
input_topic,
input_text_type,
input_length,
ai_generator,
input_api
],
outputs=[ai_label],
)
ai_article_btn.click(on_first_button_click, inputs=None, outputs=ai_generated)
ai_article_btn.click(on_first_button_click, inputs=None, outputs=ai_label)
ai_article_btn.click(on_first_button_click, inputs=None, outputs=only_ai_check_btn)
# only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=ai_detector_dropdown)
only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=bcLabel)
only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=model_dropdown)
only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=humanizer_btn)
only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=temperature_slider)
only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=controls_markdown)
only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=top_k_slider)
only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=top_token_markdown)
only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=repetition_penalty_slider)
only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=penalize_repeated_markdown)
only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=length_penalty_slider)
only_ai_check_btn.click(on_first_button_click, inputs=None, outputs=penalize_markdown)
humanizer_btn.click(
fn=humanize,
inputs=[
ai_label,
model_dropdown,
temperature_slider,
repetition_penalty_slider,
top_k_slider,
length_penalty_slider,
],
outputs=[output_label],
)
only_ai_check_btn.click(
fn=ai_generated_test,
inputs=[ai_label],
outputs=[bcLabel],
api_name="ai_check",
)
only_ai_check_btn2.click(
fn=ai_generated_test,
inputs=[output_label],
outputs=[bcLabel2]
)
humanizer_btn.click(on_first_button_click, inputs=None, outputs=humanized_markdown)
humanizer_btn.click(on_first_button_click, inputs=None, outputs=output_label)
humanizer_btn.click(on_first_button_click, inputs=None, outputs=only_ai_check_btn2)
humanizer_btn.click(on_first_button_click, inputs=None, outputs=bcLabel2)
if __name__ == "__main__":
demo.launch(demo.launch(server_name="0.0.0.0"))