You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
swarms/apps/omni_ui.py

85 lines
2.8 KiB

1 year ago
#Import required libraries
from gradio import Interface, Textbox, HTML
1 year ago
import threading
import os
1 year ago
import glob
import base64
1 year ago
from langchain.llms import ChatOpenAI #
1 year ago
from swarms.agents import OmniModalAgent # Replace with your actual class
1 year ago
1 year ago
#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()
1 year ago
1 year ago
#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:
return None
latest_file = max(list_of_files, key=os.path.getctime)
return latest_file
#Initialize your OmniModalAgent
1 year ago
llm = ChatOpenAI(model_name="gpt-4") # Replace with your actual initialization
1 year ago
agent = OmniModalAgent(llm) # Replace with your actual initialization
#Global variable to store chat history
1 year ago
chat_history = []
1 year ago
#Function to update chat
1 year ago
def update_chat(user_input):
global chat_history
chat_history.append({"type": "user", "content": user_input})
1 year ago
#Get agent response
1 year ago
agent_response = agent.run(user_input)
1 year ago
# Handle the case where agent_response is not in the expected dictionary format
if not isinstance(agent_response, dict):
agent_response = {"type": "text", "content": str(agent_response)}
1 year ago
chat_history.append(agent_response)
1 year ago
# Check for the most recently created image and add it to the chat history
latest_image = get_latest_image()
if latest_image:
chat_history.append({"type": "image", "content": latest_image})
1 year ago
return render_chat(chat_history)
1 year ago
#Function to render chat as HTML
1 year ago
def render_chat(chat_history):
1 year ago
chat_str = "<div style='max-height:400px;overflow-y:scroll;'>"
1 year ago
for message in chat_history:
if message['type'] == 'user':
1 year ago
chat_str += f"<p><strong>User:</strong> {message['content']}</p>"
1 year ago
elif message['type'] == 'text':
1 year ago
chat_str += f"<p><strong>Agent:</strong> {message['content']}</p>"
1 year ago
elif message['type'] == 'image':
1 year ago
img_path = os.path.join(".", message['content'])
base64_img = image_to_base64(img_path)
chat_str += f"<p><strong>Agent:</strong> <img src='data:image/png;base64,{base64_img}' alt='image' width='200'/></p>"
chat_str += "</div>"
1 year ago
return chat_str
1 year ago
#Define Gradio interface
1 year ago
iface = Interface(
fn=update_chat,
1 year ago
inputs=Textbox(label="Your Message", type="text"),
outputs=HTML(label="Chat History"),
live=True
1 year ago
)
1 year ago
#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
threading.Thread(target=update_display).start()
#Run Gradio interface
iface.launch()