Skip to content

Commit

Permalink
Added message file uploads to gradio
Browse files Browse the repository at this point in the history
  • Loading branch information
VRSEN committed Feb 1, 2024
1 parent 9c73015 commit 5ba60c1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
35 changes: 34 additions & 1 deletion agency_swarm/agency/agency.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import inspect
import json
import os
import shutil
import uuid
from enum import Enum
from typing import List, TypedDict, Callable, Any, Dict, Literal
Expand Down Expand Up @@ -132,18 +133,48 @@ def demo_gradio(self, height=600, dark_mode=True):
else:
js = js.replace("{theme}", "light")

message_files = []

with gr.Blocks(js=js) as demo:
chatbot = gr.Chatbot(height=height)
msg = gr.Textbox()
file_upload = gr.Files(label="Upload File", type="filepath")

def handle_file_upload(file_list):
nonlocal message_files
message_files = []
if file_list:
try:
for file_obj in file_list:
# copy file to current directory
# path = "./" + os.path.basename(file_obj)
# shutil.copyfile(file_obj.name, path)
# print(f"Uploading file: {path}")
with open(file_obj.name, 'rb') as f:
# Upload the file to OpenAI
file = self.main_thread.client.files.create(
file=f,
purpose="assistants"
)
message_files.append(file.id)
print(f"Uploaded file ID: {file.id}")
return message_files
except Exception as e:
print(f"Error: {e}")
return str(e)

return "No files uploaded"

def user(user_message, history):
# Append the user message with a placeholder for bot response
user_message = "👤 User: " + user_message.strip()
return "", history + [[user_message, None]]

def bot(history):
nonlocal message_files
print("Message files: ", message_files)
# Replace this with your actual chatbot logic
gen = self.get_completion(message=history[-1][0])
gen = self.get_completion(message=history[-1][0], message_files=message_files)

try:
# Yield each message from the generator
Expand All @@ -157,9 +188,11 @@ def bot(history):
yield history
except StopIteration:
# Handle the end of the conversation if necessary
message_files = []
pass

# Chain the events
file_upload.change(handle_file_upload, file_upload)
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
Expand Down
2 changes: 2 additions & 0 deletions agency_swarm/tools/oai/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .Retrieval import Retrieval
from .CodeInterpreter import CodeInterpreter
21 changes: 10 additions & 11 deletions tests/demos/demo_gradio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@

import gradio as gr

from agency_swarm import set_openai_key
from agency_swarm import set_openai_key, Agent

sys.path.insert(0, '../agency-swarm')

from agency_swarm.agency.agency import Agency
from tests.ceo.ceo import Ceo
from tests.test_agent.test_agent import TestAgent
from tests.test_agent2.test_agent2 import TestAgent2
from agency_swarm.tools.oai import Retrieval

ceo = Agent(name="CEO",
description="Responsible for client communication, task planning and management.",
instructions="Read files with myfiles_browser tool.", # can be a file like ./instructions.md
tools=[Retrieval])


test_agent1 = TestAgent()
test_agent2 = TestAgent2()
ceo = Ceo()

agency = Agency([
ceo,
[ceo, test_agent1, test_agent2],
[ceo, test_agent2],
], shared_instructions="./manifesto.md")
], shared_instructions="")


agency.demo_gradio(height=1500)
agency.demo_gradio(height=900)

0 comments on commit 5ba60c1

Please sign in to comment.