import gradio as gr import sys import subprocess import os sys.path.append("./") sys.path.append("../") import BotSimulator from BotSimulator import TootBot import openai from io import StringIO import multiprocessing restore_point=sys.stdout if not os.path.exists("data/fake-tweets"): os.makedirs("data/fake-tweets") if not os.path.exists("data/failed-tweets"): os.makedirs("data/failed-tweets") if not os.path.exists("images"): os.makedirs("images") title = """

🔥Fake Tweet Bot Simulation App 🔥

""" subtitle_1 = """

Initialize Bot

""" subtitle_2 = """

Run bot simulation

""" image_tokens_list = ["black and white", "year 2023", "cartoon", "animated", "comic", "propaganda", "news", "classic disney style", "holliemengert artstyle"] topic_prompt_default = "Write a short tweet with less than 500 characters as if you were a real person with social media lingo and hashtags on this topic: " # pool = multiprocessing.Pool() # num_processes = pool._processes global tootbot_app def init_app(api_key, model_name, model_class, temperature, diffusion_model, keyword_model, dtype, topic_prompt): try: openai.api_key = api_key openai.Model.list() except Exception as e: raise gr.Error("Invalid API Key... " + str(e)) console_logs = StringIO() sys.stdout = console_logs model_class = model_class.replace(" ", "").lower() global tootbot_app tootbot_app = TootBot(model=model_name, model_class=model_class, temperature=temperature) with open("client_cred.secret", "w") as file: file.write(os.getenv("MASTODON_CLIENT_SECRET")) file.close() tootbot_app.mastodon_login(username=os.getenv("MASTODON_USERNAME"), password=os.getenv("MASTODON_PASSWORD"), redirect_uri="http://localhost:8080", client_id="client_cred.secret", to_file="usercred.secret") os.remove("usercred.secret") os.remove("client_cred.secret") os.environ["OPENAI_API_KEY"] = api_key os.environ["OPENAI_API_KEY_2"] = api_key tootbot_app.topic_prescript = topic_prompt.strip() + " " tootbot_app.init_models(diffusion_model=diffusion_model, keyword_model=keyword_model, text_fail_classifier="davidna22/text-failed-classifier", dtype=dtype, device="cuda") return { progress_output: "### Current Progress \n Model Initialized Successfully! Time to Run the Simulation", log_output: console_logs.getvalue(), main_block_step_1: gr.update(visible=False), main_block_step_2: gr.update(visible=True), init_btn: gr.update(visible=False), run_sim_btn: gr.update(visible=True) } def run_simulation(topic, save=True, num_responses=50, n=10, system_prompt=BotSimulator.assistant_prompt, with_images=True, img_mode="default", augment_mode="default", image_every_n_posts=10, image_subtoken="Provide a realistic photograph. ", image_tokens=[], news_company="CNN", text_model_name="gpt-3.5-turbo-0301"): if topic == "": raise gr.Error("Topic must not be empty") console_logs = StringIO() sys.stdout = console_logs filename = topic.replace(" ", "-") + ".csv" tweet_folder="data/fake-tweets" tweet_failfolder="data/failed-tweets" image_folder = "images" if not save: tweet_filename = "" else: tweet_filename = tweet_folder + "/" + filename if n > num_responses: n = num_responses if image_every_n_posts > num_responses: image_every_n_posts = num_responses tootbot_app.run(topic, system_prompt=system_prompt, num_responses=num_responses, n=n, save=save, filename=filename, tweet_folder=tweet_folder, tweet_failfolder=tweet_failfolder, with_images=with_images, img_mode=img_mode, augment_mode=augment_mode, news_company=news_company, image_every_n_posts=image_every_n_posts, image_tokens=image_tokens, image_subtoken=image_subtoken, text_model_name=text_model_name, image_folder=image_folder) subfolder = topic.replace(" ", "-") if with_images: img_filename = f"{image_folder}/{subfolder}/tweet-img-row-0.png" img_fullpath = os.path.join(os.path.dirname(__file__), img_filename) else: img_fullpath = gr.update(visible=False) sys.stdout = restore_point return { progress_output: "### Current Progress \n To see your results, visit the bot simulation Mastodon server. \n Link to Mastodon Server: [https://bot-simulation-research.app/home](https://bot-simulation-research.app/home)", log_output: console_logs.getvalue(), simulation_output_box: gr.update(visible=True), saved_file: tweet_filename, example_image: img_fullpath } def toggle_image_params(with_images): if with_images: return { image_params: gr.update(visible=True) } else: return { image_params: gr.update(visible=False) } def add_token_func(add_token): image_tokens_list.append(add_token) return { image_tokens: gr.update(choices=image_tokens_list, interactive=True) } def reset_app(): sys.stdout = restore_point console_logs = StringIO() return { progress_output: gr.update(value="### Current Progress
"), log_output: console_logs.getvalue(), api_key: gr.update(value=""), topic: gr.update(value=""), topic_prompt: gr.update(value=topic_prompt_default), with_images: gr.update(value=False), image_params: gr.update(visible=False), main_block_step_1: gr.update(visible=True), main_block_step_2: gr.update(visible=False), simulation_output_box: gr.update(visible=False), run_sim_btn: gr.update(visible=False), init_btn: gr.update(visible=True) } with gr.Blocks() as demo: gr.HTML(title) demo_state = gr.State("Terms") with gr.Column(elem_id="main_block", visible=False) as main_block: with gr.Column(elem_id="main_block_step_1", visible=True) as main_block_step_1: gr.HTML(value=subtitle_1) api_key = gr.Textbox(label="Enter your API key") model_class = gr.Dropdown(value="Open AI", choices=["Open AI"], label="Select your Model Class (Only Open AI is supported as of now)") model_name = gr.Dropdown(value="gpt-3.5-turbo-0301", label="Select your Model", choices=["gpt-3.5-turbo", "gpt-3.5-turbo-0301", "gpt-4", "gpt-4-0314", "gpt-4-32k", "gpt-4-32k-0314"]) temperature = gr.Slider(label="Set Your Temperature (0.0-1.7) WARNING: Temperature above 1.5 tends to produce gibberish 50% of the time.", value=1.4, minimum=0.0, maximum=1.7, step=0.1) with gr.Accordion("Additional Parameters", open=False): topic_prompt = gr.Textbox(label="Enter the Topic Prompt (Default Example below, Make sure the prompt has a ':' at the end):", value=topic_prompt_default) diffusion_model = gr.Dropdown(value="stabilityai/stable-diffusion-2-1-base", choices=["stabilityai/stable-diffusion-2-1-base", "stabilityai/stable-diffusion-2-base", "runwayml/stable-diffusion-v1-5", "prompthero/openjourney", "ogkalu/Comic-Diffusion", "nitrosocke/classic-anim-diffusion" ], label="Select your Image Diffusion Model") keyword_model = gr.Dropdown(value="ml6team/keyphrase-extraction-distilbert-inspec", choices=["ml6team/keyphrase-extraction-distilbert-inspec", "ml6team/keyphrase-generation-keybart-inspec", "ml6team/keyphrase-extraction-distilbert-kptimes", "ml6team/keyphrase-extraction-kbir-inspec", "ml6team/keyphrase-extraction-kbir-kpcrowd", ], label="Select your Keyphrase Extraction Model") dtype = gr.Dropdown(value="float32", choices=["float32", "float16"], label="Select the torch datatype (float32 or float16)") with gr.Column(elem_id="main_block_step_2", visible=False) as main_block_step_2: gr.HTML(value=subtitle_2) gr.Markdown("Tweet Generation Parameters") topic = gr.Textbox(label="Enter the topic to Generate tweets (Ex: \"The Earth is Flat\")") save = gr.Checkbox(label="Download Generated Tweets CSV? (Y/n)", value=True) with gr.Accordion("Additional Parameters", open=False): num_responses = gr.Slider(label= "Number of total fake Tweets to generate", value=20, minimum=10, maximum=500, step=5) n = gr.Slider(label="Number of Tweets to return per API call (smaller = More randomized answers)", value=1, minimum=1, maximum=50, step=1) system_prompt = gr.Dropdown(label="Select the system prompt for the model (This is what Open AI uses to assist the model in text generation):", value=BotSimulator.assistant_prompt, choices=[ BotSimulator.assistant_prompt, BotSimulator.prompt_DAN ], allow_custom_value="True") gr.Markdown("Image Generation Parameters") with_images = gr.Checkbox(label="Generate Images with tweets? (Y/n)", value=False) with gr.Column(elem_id="image_params", visible=False) as image_params: img_mode_label = """Image mode can be one of three types: \t 1) Default: Image generated using the topic specified \t 2) News: Prompt is generated by first creating a fake news article. Then generating a title for that article. \t 3) Keyword: Prompt is generated using a keyword extractor model """ gr.Markdown(img_mode_label) img_mode = gr.Dropdown(show_label=False, value="Default", choices=[ "Default", "News", "Keyword"]) augment_mode_label = """Augment mode can be one of three types: \t 1) Default: Image generated without augmentations \t 2) News: Image generated using a fake news template \t 3) Screenshot: Image generated using a news screenshot template """ gr.Markdown(augment_mode_label) augment_mode = gr.Dropdown(show_label=False, value="Default", choices=[ "Default", "News", "Screenshot"]) image_every_n_posts = gr.Slider(label="Generate images every \"n\" tweets (Control how many tweets are with images or not)", value=10, minimum=1, maximum=100, step=1) with gr.Accordion("Additional Parameters", open=False): image_subtoken = gr.Dropdown(label="Select the intial style prompt for the Image Model. More in-depth prompts can be added in next parameter.", allow_custom_value=True, value="Provide a realistic photograph, ", choices=[ "Provide a realistic photograph, ", "Provide a drawing, ", "Provide a portrait, ",]) with gr.Column(): gr.Markdown("Image style prompt tokens to add to the Image Diffusion Model. Allows for more customizable Images.") with gr.Row(): with gr.Column(scale=4): add_tokens = gr.Textbox(label="Add an image style prompt token to the list below") with gr.Column(scale=1): gr.Markdown("
") add_token_btn = gr.Button("Add Token") image_tokens = gr.CheckboxGroup(show_label=False, interactive=True, choices=image_tokens_list) add_token_btn.click(add_token_func, add_tokens, [image_tokens]) news_company = gr.Dropdown(label="News Company Logo to use", value="CNN", choices=[ "CNN"]) text_model_name = gr.Dropdown(value="gpt-3.5-turbo-0301", label="Select Text Model to use", choices=["gpt-3.5-turbo", "gpt-3.5-turbo-0301", "gpt-4", "gpt-4-0314", "gpt-4-32k", "gpt-4-32k-0314"]) with_images.change(toggle_image_params, with_images, [image_params]) with gr.Row(elem_id="progress_box") as progress_box: with gr.Column(): progress_output = gr.Markdown("### Current Progress
") with gr.Column(): log_output = gr.Textbox(every=0.5, label="Logs", lines=8) with gr.Row(elem_id="simulation_output_box", visible=False) as simulation_output_box: saved_file = gr.File(label="List of Generated Tweets (CSV)") example_image = gr.Image(label="Example Image from generation", type="pil") with gr.Row(elem_id="run_sim_btn", visible=False) as run_sim_btn: submit_btn = gr.Button("Run Simulation") submt_process = submit_btn.click(run_simulation, inputs=[topic, save, num_responses, n, system_prompt, with_images, img_mode, augment_mode, image_every_n_posts, image_subtoken, image_tokens, news_company, text_model_name], outputs=[progress_output, log_output, simulation_output_box, saved_file, example_image]) with gr.Row(elem_id="init_btn", visible=True) as init_btn: submit_btn = gr.Button("Initialize Bot") submt_process = submit_btn.click(init_app, inputs=[api_key, model_name, model_class, temperature, diffusion_model, keyword_model, dtype, topic_prompt], outputs=[progress_output, log_output, main_block_step_1, main_block_step_2, init_btn, run_sim_btn]) with gr.Row(elem_id="reset_block", visible=True) as reset_block: reset_btn = gr.Button("Reset") reset_btn.click(reset_app, inputs=[], outputs=[progress_output, log_output, api_key, topic, topic_prompt, with_images, image_params, main_block_step_1, main_block_step_2, simulation_output_box, run_sim_btn, init_btn], cancels=[submt_process]) with gr.Column(elem_id = "user_consent_container") as user_consent_block: accept_checkbox = gr.Checkbox(visible=False) js = "(x) => confirm('By clicking \"OK\", I agree that my data may be published or shared.')" with gr.Accordion("User Consent for Data Collection, Use, and Sharing", open=True): gr.HTML("""

By using our app, which is powered by OpenAI's API, you acknowledge and agree to the following terms regarding the data you provide:

  1. Collection: We may collect information, including the inputs you type into our app, the outputs generated by OpenAI's API, and certain technical details about your device and connection (such as browser type, operating system, and IP address) provided by your device's request headers.
  2. Use: We may use the collected data for research purposes, to improve our services, and to develop new products or services, including commercial applications, and for security purposes, such as protecting against unauthorized access and attacks.
  3. Sharing and Publication: Your data, including the technical details collected from your device's request headers, may be published, shared with third parties, or used for analysis and reporting purposes.
  4. Data Retention: We may retain your data, including the technical details collected from your device's request headers, for as long as necessary.

By continuing to use our app, you provide your explicit consent to the collection, use, and potential sharing of your data as described above. If you do not agree with our data collection, use, and sharing practices, please do not use our app.

""") accept_button = gr.Button("I Agree") def enable_inputs(): return user_consent_block.update(visible=False), main_block.update(visible=True) accept_button.click(None, None, accept_checkbox, _js=js, queue=False) accept_checkbox.change(fn=enable_inputs, inputs=[], outputs=[user_consent_block, main_block], queue=False) # demo.queue(concurrency_count=1, max_size=num_processes) demo.launch(share=False, show_api=False)