Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from humanize import paraphrase_text | |
| from gradio_client import Client | |
| from ai_generate import generate | |
| import requests | |
| import http.client | |
| import json | |
| from gptzero_free import GPT2PPL | |
| def on_first_button_click(): | |
| return gr.update(visible=True) | |
| def ai_article_generator( | |
| topic, text_type, tonality, length, ai_model, api = None | |
| ): | |
| prompt = f"Write a {text_type} about the topic in about {length} words using a {tonality} tone: {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 | |
| ai_check_options = [ | |
| "Polygraf AI", | |
| "Sapling AI", | |
| # "CopyLeaks", | |
| "GPTZero Free" | |
| ] | |
| def ai_generated_test_polygraf(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_generated_test_copyleak(text): | |
| conn = http.client.HTTPSConnection("api.copyleaks.com") | |
| payload = payload = "{\n \"text\": \"" + text + "\"\n}" | |
| headers = { | |
| 'Authorization': "9ea35187-f43a-4dc2-bfdf-25825b78eaf1", | |
| 'Content-Type': "application/json", | |
| 'Accept': "application/json" | |
| } | |
| scanID = "9ea35187-f43a-4dc2-bfdf-25825b78eaf1" | |
| conn.request("POST", f"/v2/writer-detector/{scanID}/check", payload, headers) | |
| res = conn.getresponse() | |
| data = res.read() | |
| print(data.decode("utf-8")) | |
| print(data.decode("utf-8")["results"]["summary"]) | |
| return data.decode("utf-8")["results"]["summary"] | |
| gptzero_model = GPT2PPL() | |
| def ai_generated_test_gptzero(text): | |
| result = gptzero_model(text) | |
| print(result) | |
| return result | |
| def ai_check(text, option): | |
| if option == 'Polygraf AI': | |
| return ai_generated_test_polygraf(text) | |
| elif option == 'Sapling AI': | |
| return ai_generated_test_sapling(text) | |
| # elif option == 'CopyLeaks': | |
| # return ai_generated_test_copyleak(text) | |
| elif option == "GPTZero Free": | |
| return ai_generated_test_gptzero(text) | |
| else: | |
| return ai_generated_test_polygraf(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_tonality = gr.Dropdown( | |
| choices = ["casual", "professional", "formal", "friendly", "humorous"], | |
| value = 'casual', | |
| label="Choose tonality 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=ai_check_options, label="Select AI Detector", value="Polygraf AI", 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) | |
| ai_detector_dropdown2 = gr.Radio( | |
| choices=ai_check_options, label="Select AI Detector", value="Polygraf AI", 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_tonality, | |
| 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) | |
| ai_article_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_check, | |
| inputs=[ai_label, ai_detector_dropdown], | |
| outputs=[bcLabel], | |
| api_name="ai_check", | |
| ) | |
| only_ai_check_btn2.click( | |
| fn=ai_check, | |
| inputs=[ai_label, ai_detector_dropdown2], | |
| 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=ai_detector_dropdown2) | |
| 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(server_name="0.0.0.0") | |