davidna22's picture
Upload app.py
5d7b9c1
raw
history blame
18.4 kB
import gradio as gr
import sys
import subprocess
import os
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 = """<h1 align="center">🔥Fake Tweet Bot Simulation App 🔥</h1>"""
subtitle_1 = """<h2 align="center">Initialize Bot</h2>"""
subtitle_2 = """<h2 align="center">Run bot simulation</h2>"""
image_tokens_list = ["black and white", "year 2023", "cartoon", "animated",
"comic", "propaganda", "news", "classic disney style",
"holliemengert artstyle"]
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):
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)
tootbot_app.mastodon_login(username="research-team@bot-simulation-research.app",
password="DavidSamYungjun",
redirect_uri="http://localhost:8080",
client_id="mastodon/clientcred.secret",
to_file="mastodon/usercred.secret")
os.environ["OPENAI_API_KEY"] = api_key
os.environ["OPENAI_API_KEY_2"] = api_key
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_box: "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=""):
console_logs = StringIO()
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(" ", "-")
img_filename = f"{image_folder}/{subfolder}/tweet-img-row-0.png"
img_fullpath = os.path.join(os.path.dirname(__file__), img_filename)
return {
progress_box: "Simulation finished. To see your results, visit the bot simulation Mastodon server: 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)
}
with gr.Blocks() as demo:
gr.HTML(title)
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)")
# if model_class.replace(" ","").lower() == "openai":
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.Number(label="Set Your Temperature (0.0-2.0)", value=0.9)
with gr.Accordion("Additional Parameters", open=False):
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",minimum=20, maximum=500, step=5)
n = gr.Slider(label="Number of Tweets to return per API call (smaller = More randomized answers)", minimum=1, maximum=50, step=1)
system_prompt = gr.Dropdown(label="",
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)",
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("<br>")
add_token_btn = gr.Button("Add Token")
image_tokens = gr.CheckboxGroup(show_label=False,
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"
])
story_model = 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:
progress_box = gr.Textbox(label="Current Progress", show_label=True)
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")
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,
news_company,
story_model
],
outputs=[progress_box,
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")
submit_btn.click(init_app,
inputs=[api_key,
model_name,
model_class,
temperature,
diffusion_model,
keyword_model,
dtype],
outputs=[progress_box,
log_output,
main_block_step_1,
main_block_step_2,
init_btn,
run_sim_btn
])
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("""
<div>
<p>By using our app, which is powered by OpenAI's API, you acknowledge and agree to the following terms regarding the data you provide:</p>
<ol>
<li><strong>Collection:</strong> 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.</li>
<li><strong>Use:</strong> 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.</li>
<li><strong>Sharing and Publication:</strong> 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.</li>
<li><strong>Data Retention:</strong> We may retain your data, including the technical details collected from your device's request headers, for as long as necessary.</li>
</ol>
<p>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.</p>
</div>
""")
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=num_processes)
demo.launch(share=False)