feat: Add discord bot

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

@ -1,18 +1,25 @@
#Import required libraries # Import required libraries
from gradio import Interface, Textbox, HTML from gradio import Interface, Textbox, HTML
import threading import threading
import os import os
import glob 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,19 +27,22 @@ 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
#Global variable to store chat history # Global variable to store chat history
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})
#Get agent response # Get agent response
agent_response = agent.run(user_input) agent_response = agent.run(user_input)
# Handle the case where agent_response is not in the expected dictionary format # Handle the case where agent_response is not in the expected dictionary format
@ -48,7 +58,8 @@ def update_chat(user_input):
return render_chat(chat_history) return render_chat(chat_history)
#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;'>"
@ -64,22 +75,26 @@ 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,
inputs=Textbox(label="Your Message", type="text"), inputs=Textbox(label="Your Message", type="text"),
outputs=HTML(label="Chat History"), outputs=HTML(label="Chat History"),
live=True live=True
) )
#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()
#Run Gradio interface # Run Gradio interface
iface.launch() iface.launch()

Loading…
Cancel
Save