From 2bb9e407af287ebe4f6b570e92d7ef4a4823c77f Mon Sep 17 00:00:00 2001 From: Zack Date: Tue, 24 Oct 2023 19:33:28 -0500 Subject: [PATCH] feat: remove cookies.json requirement, update env --- .env.example | 5 ++- main.py | 77 ++++++++++++++++++++++++++++++++++++++ swarms/models/bing_chat.py | 14 +++++-- 3 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 main.py diff --git a/.env.example b/.env.example index 8c73ae02..345b10a1 100644 --- a/.env.example +++ b/.env.example @@ -47,7 +47,10 @@ REVGPT_UNVERIFIED_PLUGIN_DOMAINS="showme.redstarplugin.com" CHATGPT_BASE_URL="" #Discord Bot -################################ SAVE_DIRECTORY="" STORAGE_SERVICE="" DISCORD_TOKEN="" + +#Bing +AUTH_COOKIE="_U value at bing.com" +AUTH_COOKIE_SRCHHPGUSR"_SRCHHPGUSR value at bing.com" diff --git a/main.py b/main.py new file mode 100644 index 00000000..96d02e3c --- /dev/null +++ b/main.py @@ -0,0 +1,77 @@ +import openai +import os +import dotenv +import logging +import gradio as gr +from BingImageCreator import ImageGen +from swarms.models.bing_chat import BingChat + +# from swarms.models.bingchat import BingChat +dotenv.load_dotenv(".env") + +# Initialize the EdgeGPTModel +cookie = os.environ.get("BING_COOKIE") +auth = os.environ.get("AUTH_COOKIE") +model = BingChat(cookies_path="./cookies.json", bing_cookie="BING_COOKIE",auth_cookie="AUTH_COOKIE") + +response = model("Generate") + +logging.basicConfig(level=logging.INFO) + +accumulated_story = "" +latest_caption = "" +standard_suffix = "" +storyboard = [] + +def generate_images_with_bingchat(caption): + img_path = model.create_img(caption) + img_urls = model.images(caption) + return img_urls + +def generate_single_caption(text): + prompt = f"A comic about {text}." + response = model(text) + return response + +def interpret_text_with_gpt(text, suffix): + return generate_single_caption(f"{text} {suffix}") + +def create_standard_suffix(original_prompt): + return f"In the style of {original_prompt}" + +def gradio_interface(text=None, next_button_clicked=False): + global accumulated_story, latest_caption, standard_suffix, storyboard + + if not standard_suffix: + standard_suffix = create_standard_suffix(text) + + if next_button_clicked: + new_caption = generate_single_caption(latest_caption + " " + standard_suffix) + new_urls = generate_images_with_bingchat(new_caption) + latest_caption = new_caption + storyboard.append((new_urls, new_caption)) + + elif text: + caption = generate_single_caption(text + " " + standard_suffix) + comic_panel_urls = generate_images_with_bingchat(caption) + latest_caption = caption + storyboard.append((comic_panel_urls, caption)) + + storyboard_html = "" + for urls, cap in storyboard: + for url in urls: + storyboard_html += f'{cap}
{cap}
' + + return storyboard_html + +if __name__ == "__main__": + iface = gr.Interface( + fn=gradio_interface, + inputs=[ + gr.inputs.Textbox(default="Type your story concept here", optional=True, label="Story Concept"), + gr.inputs.Checkbox(label="Generate Next Part") + ], + outputs=[gr.outputs.HTML()], + live=False # Submit button will appear + ) + iface.launch() diff --git a/swarms/models/bing_chat.py b/swarms/models/bing_chat.py index 750d5e62..4c7de939 100644 --- a/swarms/models/bing_chat.py +++ b/swarms/models/bing_chat.py @@ -25,11 +25,19 @@ class BingChat: """ - def __init__(self, cookies_path: str = None, bing_cookie: str = None, auth_cookie: str = None): - self.cookies = json.loads(open(cookies_path, encoding="utf-8").read()) + + def __init__(self, cookies_path: str = None, auth_cookie: str = None, auth_cookie_SRCHHPGUSR: str = None): + auth_cookie = os.environ("AUTH_COOKIE") + auth_cookie_SRCHHPGUSR + if cookies_path: + self.cookies = json.loads(open(cookies_path, encoding="utf-8").read()) + elif auth_cookie: + self.cookies = auth_cookie + else: + raise ValueError("Either cookies_path or auth_cookie must be provided.") self.bot = asyncio.run(Chatbot.create(cookies=self.cookies)) self.auth_cookie = auth_cookie - self.auth_cookie_SRCHHPGUSR = bing_cookie + self.auth_cookie_SRCHHPGUSR = auth_cookie_SRCHHPGUSR def __call__( self, prompt: str, style: ConversationStyle = ConversationStyle.creative