feat: Add discord bot

Former-commit-id: 708ed1d503
discord-bot
Zack 2 years ago
parent 4221bcbe6b
commit d47e8d1efe

@ -6,13 +6,20 @@ import glob
import base64 import base64
from langchain.llms import OpenAIChat from langchain.llms import OpenAIChat
from swarms.agents import OmniModalAgent from swarms.agents import OmniModalAgent
import gradio_client as grc
grc.Client("Wawaa/omniagent").deploy_discord()
# Function to convert image to base64 # Function to convert image to base64
def image_to_base64(image_path): def image_to_base64(image_path):
with open(image_path, "rb") as image_file: with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode() return base64.b64encode(image_file.read()).decode()
# Function to get the most recently created image in the directory # Function to get the most recently created image in the directory
def get_latest_image(): def get_latest_image():
list_of_files = glob.glob('./*.png') # Replace with your image file type list_of_files = glob.glob('./*.png') # Replace with your image file type
if not list_of_files: if not list_of_files:
@ -20,6 +27,7 @@ def get_latest_image():
latest_file = max(list_of_files, key=os.path.getctime) latest_file = max(list_of_files, key=os.path.getctime)
return latest_file return latest_file
# Initialize your OmniModalAgent # Initialize your OmniModalAgent
llm = OpenAIChat(model_name="gpt-4") # Replace with your actual initialization llm = OpenAIChat(model_name="gpt-4") # Replace with your actual initialization
agent = OmniModalAgent(llm) # Replace with your actual initialization agent = OmniModalAgent(llm) # Replace with your actual initialization
@ -28,6 +36,8 @@ agent = OmniModalAgent(llm) # Replace with your actual initialization
chat_history = [] chat_history = []
# Function to update chat # Function to update chat
def update_chat(user_input): def update_chat(user_input):
global chat_history global chat_history
chat_history.append({"type": "user", "content": user_input}) chat_history.append({"type": "user", "content": user_input})
@ -50,6 +60,7 @@ def update_chat(user_input):
# Function to render chat as HTML # Function to render chat as HTML
def render_chat(chat_history): def render_chat(chat_history):
chat_str = "<div style='max-height:400px;overflow-y:scroll;'>" chat_str = "<div style='max-height:400px;overflow-y:scroll;'>"
for message in chat_history: for message in chat_history:
@ -64,6 +75,7 @@ def render_chat(chat_history):
chat_str += "</div>" chat_str += "</div>"
return chat_str return chat_str
# Define Gradio interface # Define Gradio interface
iface = Interface( iface = Interface(
fn=update_chat, fn=update_chat,
@ -73,11 +85,14 @@ iface = Interface(
) )
# Function to update the chat display # Function to update the chat display
def update_display(): def update_display():
global chat_history global chat_history
while True: while True:
iface.update(render_chat(chat_history)) iface.update(render_chat(chat_history))
# Run the update_display function in a separate thread # Run the update_display function in a separate thread
threading.Thread(target=update_display).start() threading.Thread(target=update_display).start()

Loading…
Cancel
Save