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
import threading
import os
import glob
import base64
from langchain.llms import OpenAIChat
from swarms.agents import OmniModalAgent
from langchain.llms import OpenAIChat
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):
with open(image_path, "rb") as image_file:
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():
list_of_files = glob.glob('./*.png') # Replace with your image file type
if not list_of_files:
@ -20,19 +27,22 @@ def get_latest_image():
latest_file = max(list_of_files, key=os.path.getctime)
return latest_file
#Initialize your OmniModalAgent
# Initialize your OmniModalAgent
llm = OpenAIChat(model_name="gpt-4") # 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 = []
#Function to update chat
# Function to update chat
def update_chat(user_input):
global chat_history
chat_history.append({"type": "user", "content": user_input})
#Get agent response
# Get agent response
agent_response = agent.run(user_input)
# 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)
#Function to render chat as HTML
# Function to render chat as HTML
def render_chat(chat_history):
chat_str = "<div style='max-height:400px;overflow-y:scroll;'>"
@ -64,22 +75,26 @@ def render_chat(chat_history):
chat_str += "</div>"
return chat_str
#Define Gradio interface
# Define Gradio interface
iface = Interface(
fn=update_chat,
inputs=Textbox(label="Your Message", type="text"),
fn=update_chat,
inputs=Textbox(label="Your Message", type="text"),
outputs=HTML(label="Chat History"),
live=True
)
#Function to update the chat display
# Function to update the chat display
def update_display():
global chat_history
while True:
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()
#Run Gradio interface
iface.launch()
# Run Gradio interface
iface.launch()

Loading…
Cancel
Save