Updates
Browse files- __pycache__/tool_loader.cpython-311.pyc +0 -0
- app.py +14 -63
- app_agent_config.py +3 -79
- controller.py +42 -74
- model/conversation_chain_singleton.py +36 -0
- custom_agent.py β model/custom_agent.py +1 -1
- logger.py β utils/logger.py +0 -0
- tool_config.py β utils/tool_config.py +0 -0
- tool_loader.py β utils/tool_loader.py +2 -2
- app_chat.py β view/app_chat.py +36 -51
- app_dev_desc.py β view/app_dev_desc.py +0 -0
- view/app_header.py +10 -0
- view/app_sidebar.py +84 -0
- app_user_desc.py β view/app_user_desc.py +0 -0
__pycache__/tool_loader.cpython-311.pyc
ADDED
Binary file (1.48 kB). View file
|
|
app.py
CHANGED
@@ -1,74 +1,25 @@
|
|
1 |
import streamlit as st
|
2 |
|
3 |
-
from
|
4 |
-
from tool_loader import ToolLoader
|
5 |
-
from app_user_desc import app_user_desc
|
6 |
-
from app_dev_desc import app_dev_desc
|
7 |
-
from logger import log_response
|
8 |
-
from logger import log_enabled
|
9 |
-
from app_chat import app_chat
|
10 |
-
import numpy as np
|
11 |
-
import re,sys,unicodedata
|
12 |
-
import logging
|
13 |
-
|
14 |
-
|
15 |
-
from app_agent_config import AgentConfig
|
16 |
-
|
17 |
-
# Create an instance of AgentConfig ## holds all data and settings
|
18 |
-
agent_config = AgentConfig()
|
19 |
|
|
|
|
|
|
|
20 |
|
|
|
21 |
st.set_page_config(
|
22 |
page_title="Custom Transformers can realy do anything...",
|
23 |
-
page_icon="π"
|
24 |
)
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
print("The modal was closed!")
|
29 |
-
|
30 |
-
|
31 |
-
def open_modal():
|
32 |
-
print("Todo")
|
33 |
-
# st.modal("User Guide", "app_user_desc()")
|
34 |
-
|
35 |
-
st.button("User Description", open_modal())
|
36 |
-
# Define a callback function that will be called when the button is clicked
|
37 |
-
#def open_modal():
|
38 |
-
# st.modal("User Guide", app_user_desc())
|
39 |
-
|
40 |
-
# Create a button that opens the modal
|
41 |
-
|
42 |
-
#######
|
43 |
-
|
44 |
-
import pandas as pd
|
45 |
-
from io import StringIO
|
46 |
-
with st.sidebar:
|
47 |
-
|
48 |
-
|
49 |
-
st.header("Set Tools and Option. ")
|
50 |
-
|
51 |
-
with st.expander("Configure the agent and activate tools"):
|
52 |
-
|
53 |
-
agent_config.configure()
|
54 |
-
|
55 |
-
with st.expander("Set Content and Context"):
|
56 |
-
|
57 |
-
agent_config.content_and_context()
|
58 |
-
|
59 |
-
# Create a page with tabs
|
60 |
-
#tabs = st.tabs(["Chat","User Description"])
|
61 |
-
|
62 |
-
#with tabs[0]:
|
63 |
-
st.title("Hugging Face Agent and Tools")
|
64 |
|
65 |
-
##
|
|
|
66 |
|
67 |
-
|
|
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
#with tabs[1]:
|
72 |
-
# app_user_desc()
|
73 |
-
|
74 |
-
app_chat(agent_config)
|
|
|
1 |
import streamlit as st
|
2 |
|
3 |
+
from controller import Controller
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
from view.app_header import app_header
|
6 |
+
from view.app_sidebar import app_sidebar
|
7 |
+
from view.app_chat import app_chat
|
8 |
|
9 |
+
## Streamlit configuration (holds the session and session history as well)
|
10 |
st.set_page_config(
|
11 |
page_title="Custom Transformers can realy do anything...",
|
12 |
+
page_icon="π"
|
13 |
)
|
14 |
|
15 |
+
# Create an instance of Controller with agentConfig ## holds all data, config and settings
|
16 |
+
controller = Controller()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
## sidebar for context & config
|
19 |
+
app_sidebar(controller)
|
20 |
|
21 |
+
## app header
|
22 |
+
app_header(controller)
|
23 |
|
24 |
+
## Main content the chat
|
25 |
+
app_chat(controller)
|
|
|
|
|
|
|
|
app_agent_config.py
CHANGED
@@ -1,11 +1,7 @@
|
|
1 |
# app_agent_config.py
|
2 |
-
|
3 |
-
from
|
4 |
-
from
|
5 |
-
from logger import log_enabled
|
6 |
-
|
7 |
-
from PIL import Image
|
8 |
-
import numpy as np
|
9 |
|
10 |
class AgentConfig:
|
11 |
def __init__(self):
|
@@ -17,75 +13,3 @@ class AgentConfig:
|
|
17 |
self.context = ""
|
18 |
self.tool_loader = ToolLoader(tool_names)
|
19 |
|
20 |
-
def configure(self):
|
21 |
-
st.markdown("Change the agent's configuration here.")
|
22 |
-
self.url_endpoint = st.selectbox("Select Inference URL", [
|
23 |
-
"https://api-inference.huggingface.co/models/bigcode/starcoder",
|
24 |
-
"https://api-inference.huggingface.co/models/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5",
|
25 |
-
"https://api-inference.huggingface.co/models/gpt2"
|
26 |
-
])
|
27 |
-
tool_loader = ToolLoader(tool_names)
|
28 |
-
self.log_enabled = st.checkbox("Enable Logging")
|
29 |
-
self.tool_checkboxes = [st.checkbox(f"{tool.name} --- {tool.description} ") for tool in tool_loader.tools]
|
30 |
-
|
31 |
-
def content_and_context(self):
|
32 |
-
self.context = st.text_area("Context")
|
33 |
-
|
34 |
-
self.image = st.camera_input("Take a picture")
|
35 |
-
|
36 |
-
img_file_buffer = st.file_uploader('Upload a PNG image', type='png')
|
37 |
-
|
38 |
-
if img_file_buffer is not None:
|
39 |
-
image_raw = Image.open(img_file_buffer)
|
40 |
-
#global image
|
41 |
-
self.image = np.array(image_raw)
|
42 |
-
########
|
43 |
-
st.image(agent_config.image)
|
44 |
-
|
45 |
-
uploaded_file = st.file_uploader("Choose a pdf", type='pdf')
|
46 |
-
if uploaded_file is not None:
|
47 |
-
# To read file as bytes:
|
48 |
-
pdf_document = uploaded_file.getvalue()
|
49 |
-
self.document = pdf_document
|
50 |
-
st.write(pdf_document)
|
51 |
-
|
52 |
-
uploaded_txt_file = st.file_uploader("Choose a txt", type='txt')
|
53 |
-
if uploaded_txt_file is not None:
|
54 |
-
# To read file as bytes:
|
55 |
-
txt_document = uploaded_txt_file.getvalue()
|
56 |
-
self.document = txt_document
|
57 |
-
st.write(txt_document)
|
58 |
-
|
59 |
-
uploaded_csv_file = st.file_uploader("Choose a csv", type='csv')
|
60 |
-
if uploaded_csv_file is not None:
|
61 |
-
# To read file as bytes:
|
62 |
-
csv_document = uploaded_csv_file.getvalue()
|
63 |
-
self.document = csv_document
|
64 |
-
st.write(csv_document)
|
65 |
-
|
66 |
-
uploaded_csv_file = st.file_uploader("Choose audio", type='wav')
|
67 |
-
if uploaded_csv_file is not None:
|
68 |
-
# To read file as bytes:
|
69 |
-
csv_document = uploaded_csv_file.getvalue()
|
70 |
-
self.document = csv_document
|
71 |
-
st.write(csv_document)
|
72 |
-
|
73 |
-
uploaded_csv_file = st.file_uploader("Choose video", type='avi')
|
74 |
-
if uploaded_csv_file is not None:
|
75 |
-
# To read file as bytes:
|
76 |
-
csv_document = uploaded_csv_file.getvalue()
|
77 |
-
self.document = csv_document
|
78 |
-
st.write(csv_document)
|
79 |
-
|
80 |
-
# To convert to a string based IO:
|
81 |
-
#stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
|
82 |
-
#st.write(stringio)
|
83 |
-
|
84 |
-
# To read file as string:
|
85 |
-
#string_data = stringio.read()
|
86 |
-
#st.write(string_data)
|
87 |
-
|
88 |
-
# Can be used wherever a "file-like" object is accepted:
|
89 |
-
dataframe = pd.read_csv(uploaded_file)
|
90 |
-
st.write(dataframe)
|
91 |
-
|
|
|
1 |
# app_agent_config.py
|
2 |
+
from utils.tool_loader import ToolLoader
|
3 |
+
from utils.tool_config import tool_names
|
4 |
+
from utils.logger import log_enabled
|
|
|
|
|
|
|
|
|
5 |
|
6 |
class AgentConfig:
|
7 |
def __init__(self):
|
|
|
13 |
self.context = ""
|
14 |
self.tool_loader = ToolLoader(tool_names)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
controller.py
CHANGED
@@ -1,88 +1,56 @@
|
|
1 |
import os
|
2 |
-
from
|
3 |
-
from
|
4 |
-
|
|
|
5 |
|
6 |
-
from langchain.vectorstores import FAISS
|
7 |
-
from langchain.memory import ConversationBufferMemory
|
8 |
-
from langchain.chains import ConversationalRetrievalChain, ConversationChain
|
9 |
-
from langchain.llms import HuggingFaceHub
|
10 |
-
|
11 |
-
|
12 |
-
image = []
|
13 |
-
def handle_submission(user_message, selected_tools, url_endpoint, document, image, context):
|
14 |
-
|
15 |
-
log_response("User input \n {}".format(user_message))
|
16 |
-
log_response("selected_tools \n {}".format(selected_tools))
|
17 |
-
log_response("url_endpoint \n {}".format(url_endpoint))
|
18 |
-
log_response("document \n {}".format(document))
|
19 |
-
log_response("image \n {}".format(image))
|
20 |
-
log_response("context \n {}".format(context))
|
21 |
-
|
22 |
-
agent = CustomHfAgent(
|
23 |
-
url_endpoint=url_endpoint,
|
24 |
-
token=os.environ['HF_token'],
|
25 |
-
additional_tools=selected_tools,
|
26 |
-
input_params={"max_new_tokens": 192},
|
27 |
-
)
|
28 |
-
|
29 |
-
response = agent.chat(user_message,document=document,image=image, context = context)
|
30 |
-
|
31 |
-
log_response("Agent Response\n {}".format(response))
|
32 |
-
|
33 |
-
return response
|
34 |
-
|
35 |
-
def cut_text_after_keyword(text, keyword):
|
36 |
-
index = text.find(keyword)
|
37 |
-
if index != -1:
|
38 |
-
return text[:index].strip()
|
39 |
-
return text
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
def handle_submission_chat(user_message, response):
|
45 |
-
# os.environ['HUGGINGFACEHUB_API_TOKEN'] = os.environ['HF_token']
|
46 |
-
agent_chat_bot = ConversationChainSingleton().get_conversation_chain()
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
text = agent_chat_bot.predict(input=user_message)
|
52 |
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
|
57 |
|
58 |
-
|
59 |
-
_instance = None
|
60 |
|
61 |
-
|
62 |
-
if not cls._instance:
|
63 |
-
cls._instance = super(ConversationChainSingleton, cls).__new__(cls)
|
64 |
-
# Initialize your conversation chain here
|
65 |
-
cls._instance.conversation_chain = get_conversation_chain()
|
66 |
-
return cls._instance
|
67 |
|
68 |
-
def
|
69 |
-
|
|
|
|
|
|
|
70 |
|
71 |
|
72 |
-
def
|
73 |
-
|
74 |
-
|
75 |
|
76 |
-
|
|
|
|
|
|
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
)
|
82 |
-
# llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")
|
83 |
|
84 |
-
|
85 |
-
conversation_chain = ConversationChain(
|
86 |
-
llm=llm, verbose=True, memory=ConversationBufferMemory()
|
87 |
-
)
|
88 |
-
return conversation_chain
|
|
|
1 |
import os
|
2 |
+
from app_agent_config import AgentConfig
|
3 |
+
from utils.logger import log_response
|
4 |
+
from model.custom_agent import CustomHfAgent
|
5 |
+
from model.conversation_chain_singleton import ConversationChainSingleton
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
|
|
|
|
|
|
8 |
|
9 |
+
class Controller:
|
10 |
+
def __init__(self):
|
11 |
+
self.agent_config = AgentConfig()
|
|
|
12 |
|
13 |
+
image = []
|
14 |
+
def handle_submission(user_message, selected_tools, url_endpoint, document, image, context):
|
15 |
+
|
16 |
+
log_response("User input \n {}".format(user_message))
|
17 |
+
log_response("selected_tools \n {}".format(selected_tools))
|
18 |
+
log_response("url_endpoint \n {}".format(url_endpoint))
|
19 |
+
log_response("document \n {}".format(document))
|
20 |
+
log_response("image \n {}".format(image))
|
21 |
+
log_response("context \n {}".format(context))
|
22 |
+
|
23 |
+
agent = CustomHfAgent(
|
24 |
+
url_endpoint=url_endpoint,
|
25 |
+
token=os.environ['HF_token'],
|
26 |
+
additional_tools=selected_tools,
|
27 |
+
input_params={"max_new_tokens": 192},
|
28 |
+
)
|
29 |
|
30 |
+
response = agent.chat(user_message,document=document,image=image, context = context)
|
31 |
|
32 |
+
log_response("Agent Response\n {}".format(response))
|
|
|
33 |
|
34 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
def cut_text_after_keyword(text, keyword):
|
37 |
+
index = text.find(keyword)
|
38 |
+
if index != -1:
|
39 |
+
return text[:index].strip()
|
40 |
+
return text
|
41 |
|
42 |
|
43 |
+
def handle_submission_chat(user_message, response):
|
44 |
+
# os.environ['HUGGINGFACEHUB_API_TOKEN'] = os.environ['HF_token']
|
45 |
+
agent_chat_bot = ConversationChainSingleton().get_conversation_chain()
|
46 |
|
47 |
+
if response is not None:
|
48 |
+
text = agent_chat_bot.predict(input=user_message + response)
|
49 |
+
else:
|
50 |
+
text = agent_chat_bot.predict(input=user_message)
|
51 |
|
52 |
+
result = cut_text_after_keyword(text, "Human:")
|
53 |
+
|
54 |
+
print(result)
|
|
|
|
|
55 |
|
56 |
+
return result
|
|
|
|
|
|
|
|
model/conversation_chain_singleton.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.vectorstores import FAISS
|
2 |
+
from langchain.memory import ConversationBufferMemory
|
3 |
+
from langchain.chains import ConversationalRetrievalChain, ConversationChain
|
4 |
+
from langchain.llms import HuggingFaceHub
|
5 |
+
|
6 |
+
class ConversationChainSingleton:
|
7 |
+
_instance = None
|
8 |
+
|
9 |
+
def __new__(cls, *args, **kwargs):
|
10 |
+
if not cls._instance:
|
11 |
+
cls._instance = super(ConversationChainSingleton, cls).__new__(cls)
|
12 |
+
# Initialize your conversation chain here
|
13 |
+
cls._instance.conversation_chain = get_conversation_chain()
|
14 |
+
return cls._instance
|
15 |
+
|
16 |
+
def get_conversation_chain(self):
|
17 |
+
return self.conversation_chain
|
18 |
+
|
19 |
+
|
20 |
+
def get_conversation_chain( ):
|
21 |
+
"""
|
22 |
+
Create a conversational retrieval chain and a language model.
|
23 |
+
|
24 |
+
"""
|
25 |
+
|
26 |
+
llm = HuggingFaceHub(
|
27 |
+
repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
28 |
+
model_kwargs={"max_length": 1048, "temperature":0.2, "max_new_tokens":256, "top_p":0.95, "repetition_penalty":1.0},
|
29 |
+
)
|
30 |
+
# llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")
|
31 |
+
|
32 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
33 |
+
conversation_chain = ConversationChain(
|
34 |
+
llm=llm, verbose=True, memory=memory
|
35 |
+
)
|
36 |
+
return conversation_chain
|
custom_agent.py β model/custom_agent.py
RENAMED
@@ -5,7 +5,7 @@ import io
|
|
5 |
import requests
|
6 |
import time
|
7 |
from transformers import Agent
|
8 |
-
from logger import log_response
|
9 |
|
10 |
import time
|
11 |
import torch
|
|
|
5 |
import requests
|
6 |
import time
|
7 |
from transformers import Agent
|
8 |
+
from utils.logger import log_response
|
9 |
|
10 |
import time
|
11 |
import torch
|
logger.py β utils/logger.py
RENAMED
File without changes
|
tool_config.py β utils/tool_config.py
RENAMED
File without changes
|
tool_loader.py β utils/tool_loader.py
RENAMED
@@ -2,8 +2,8 @@
|
|
2 |
|
3 |
import logging
|
4 |
from transformers import load_tool
|
5 |
-
from logger import log_response # Import the logger
|
6 |
-
from tool_config import tool_names
|
7 |
|
8 |
class ToolLoader:
|
9 |
def __init__(self, tool_names):
|
|
|
2 |
|
3 |
import logging
|
4 |
from transformers import load_tool
|
5 |
+
from utils.logger import log_response # Import the logger
|
6 |
+
from utils.tool_config import tool_names
|
7 |
|
8 |
class ToolLoader:
|
9 |
def __init__(self, tool_names):
|
app_chat.py β view/app_chat.py
RENAMED
@@ -1,33 +1,12 @@
|
|
1 |
-
|
2 |
import streamlit as st
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
from PIL import Image
|
7 |
from pydub import AudioSegment
|
8 |
-
import IPython
|
9 |
-
import soundfile as sf
|
10 |
-
from app_agent_config import AgentConfig
|
11 |
-
from tool_loader import ToolLoader
|
12 |
-
import re,sys,unicodedata
|
13 |
-
|
14 |
-
|
15 |
-
import pandas as pd # If you're working with DataFrames
|
16 |
-
import matplotlib.figure # If you're using matplotlib figures
|
17 |
-
import numpy as np
|
18 |
-
|
19 |
-
# For Bokeh charts
|
20 |
-
from bokeh.models import Plot
|
21 |
-
|
22 |
-
# For Plotly charts
|
23 |
-
import plotly.express as px
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
# Call app_agent_config to set the global variables
|
29 |
-
|
30 |
-
def app_chat(agent_config):
|
31 |
# Chat code (user input, agent responses, etc.)
|
32 |
if "messages" not in st.session_state:
|
33 |
st.session_state.messages = []
|
@@ -37,84 +16,90 @@ def app_chat(agent_config):
|
|
37 |
with st.chat_message(message["role"]):
|
38 |
st.markdown(message["content"])
|
39 |
|
40 |
-
#with st.chat_message("assistant"):
|
41 |
-
# st.markdown("Hello there! How can I assist you today?")
|
42 |
-
|
43 |
if user_message := st.chat_input("Enter message"):
|
44 |
st.chat_message("user").markdown(user_message)
|
45 |
st.session_state.messages.append({"role": "user", "content": user_message, "avatar": "π€"})
|
46 |
|
47 |
selected_tools = [agent_config.tool_loader.tools[idx] for idx, checkbox in enumerate(agent_config.tool_checkboxes) if checkbox]
|
48 |
-
# Handle submission with the selected inference URL
|
49 |
-
#app_agent_config()
|
50 |
|
51 |
response = ""
|
52 |
chat_respone = ""
|
53 |
with st.spinner('Please stand by ...'):
|
|
|
54 |
|
55 |
-
response = handle_submission(user_message, selected_tools, agent_config.url_endpoint, agent_config.document, agent_config.image, agent_config.context)
|
56 |
-
|
57 |
with st.chat_message("assistant"):
|
58 |
if response is None:
|
59 |
-
chat_respone = handle_submission_chat(user_message, response)
|
60 |
st.write(chat_respone)
|
61 |
# st.warning("The agent's response is None. Please try again. Generate an image of a flying uncormn.")
|
62 |
elif isinstance(response, Image.Image):
|
63 |
agent_config.image = response
|
64 |
-
chat_respone = handle_submission_chat(user_message, "No context . Created an image.")
|
65 |
st.write(chat_respone)
|
66 |
st.image(response)
|
67 |
elif isinstance(response, AudioSegment):
|
68 |
agent_config.audio = response
|
69 |
-
chat_respone = handle_submission_chat(user_message, "Agent Tool created audio file.")
|
70 |
st.write(chat_respone)
|
71 |
st.audio(response)
|
72 |
elif isinstance(response, int):
|
73 |
-
chat_respone = handle_submission_chat(user_message, response)
|
74 |
st.write(chat_respone)
|
75 |
st.markdown(response)
|
76 |
elif isinstance(response, str):
|
77 |
if "emojified_text" in response:
|
78 |
-
chat_respone = handle_submission_chat(user_message, "Agent Tool created the text with emojies.")
|
79 |
st.write(chat_respone)
|
80 |
st.markdown(f"{response['emojified_text']}")
|
81 |
else:
|
82 |
-
chat_respone = handle_submission_chat(user_message, response)
|
83 |
st.write(chat_respone)
|
84 |
st.markdown(response)
|
85 |
elif isinstance(response, list):
|
86 |
-
chat_respone = handle_submission_chat(user_message, "Agent Tool produced a list")
|
87 |
for item in response:
|
88 |
st.markdown(item) # Assuming the list contains strings
|
|
|
89 |
elif isinstance(response, pd.DataFrame):
|
90 |
-
chat_respone = handle_submission_chat(user_message, "Agent Tool produced a pd.DataFrame")
|
|
|
91 |
st.dataframe(response)
|
|
|
92 |
elif isinstance(response, pd.Series):
|
93 |
-
chat_respone = handle_submission_chat(user_message, "Agent Tool produced a pd.Series")
|
|
|
94 |
st.table(response.iloc[0:10])
|
95 |
elif isinstance(response, dict):
|
96 |
-
chat_respone = handle_submission_chat(user_message, "Agent Tool produced a dict")
|
|
|
97 |
st.json(response)
|
98 |
elif isinstance(response, st.graphics_altair.AltairChart):
|
99 |
-
chat_respone = handle_submission_chat(user_message, "Agent Tool produced a st.graphics_altair.AltairChart")
|
|
|
100 |
st.altair_chart(response)
|
101 |
elif isinstance(response, st.graphics_bokeh.BokehChart):
|
102 |
-
chat_respone = handle_submission_chat(user_message, "Agent Tool produced a st.graphics_bokeh.BokehChart")
|
|
|
103 |
st.bokeh_chart(response)
|
104 |
elif isinstance(response, st.graphics_graphviz.GraphvizChart):
|
105 |
-
chat_respone = handle_submission_chat(user_message, "Agent Tool produced a st.graphics_graphviz.GraphvizChart")
|
|
|
106 |
st.graphviz_chart(response)
|
107 |
elif isinstance(response, st.graphics_plotly.PlotlyChart):
|
108 |
-
chat_respone = handle_submission_chat(user_message, "Agent Tool produced a st.graphics_plotly.PlotlyChart")
|
|
|
109 |
st.plotly_chart(response)
|
110 |
elif isinstance(response, st.graphics_pydeck.PydeckChart):
|
111 |
-
chat_respone = handle_submission_chat(user_message, "Agent Tool produced a st.graphics_pydeck.PydeckChart")
|
|
|
112 |
st.pydeck_chart(response)
|
113 |
elif isinstance(response, matplotlib.figure.Figure):
|
114 |
-
chat_respone = handle_submission_chat(user_message, "Agent Tool produced a matplotlib.figure.Figure")
|
|
|
115 |
st.pyplot(response)
|
116 |
-
elif isinstance(response,
|
117 |
-
chat_respone = handle_submission_chat(user_message, "Agent Tool produced a VegaLiteChart")
|
|
|
118 |
st.vega_lite_chart(response)
|
119 |
else:
|
120 |
st.warning("Unrecognized response type. Please try again. e.g. Generate an image of a flying horse.")
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import matplotlib.figure
|
4 |
+
|
5 |
from PIL import Image
|
6 |
from pydub import AudioSegment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
def app_chat(controller):
|
9 |
+
agent_config = controller.agent_config
|
|
|
|
|
|
|
|
|
10 |
# Chat code (user input, agent responses, etc.)
|
11 |
if "messages" not in st.session_state:
|
12 |
st.session_state.messages = []
|
|
|
16 |
with st.chat_message(message["role"]):
|
17 |
st.markdown(message["content"])
|
18 |
|
|
|
|
|
|
|
19 |
if user_message := st.chat_input("Enter message"):
|
20 |
st.chat_message("user").markdown(user_message)
|
21 |
st.session_state.messages.append({"role": "user", "content": user_message, "avatar": "π€"})
|
22 |
|
23 |
selected_tools = [agent_config.tool_loader.tools[idx] for idx, checkbox in enumerate(agent_config.tool_checkboxes) if checkbox]
|
|
|
|
|
24 |
|
25 |
response = ""
|
26 |
chat_respone = ""
|
27 |
with st.spinner('Please stand by ...'):
|
28 |
+
response = controller.handle_submission(user_message, selected_tools, agent_config.url_endpoint, agent_config.document, agent_config.image, agent_config.context)
|
29 |
|
|
|
|
|
30 |
with st.chat_message("assistant"):
|
31 |
if response is None:
|
32 |
+
chat_respone = controller.handle_submission_chat(user_message, response)
|
33 |
st.write(chat_respone)
|
34 |
# st.warning("The agent's response is None. Please try again. Generate an image of a flying uncormn.")
|
35 |
elif isinstance(response, Image.Image):
|
36 |
agent_config.image = response
|
37 |
+
chat_respone = controller.handle_submission_chat(user_message, "No context . Created an image.")
|
38 |
st.write(chat_respone)
|
39 |
st.image(response)
|
40 |
elif isinstance(response, AudioSegment):
|
41 |
agent_config.audio = response
|
42 |
+
chat_respone = controller.handle_submission_chat(user_message, "Agent Tool created audio file.")
|
43 |
st.write(chat_respone)
|
44 |
st.audio(response)
|
45 |
elif isinstance(response, int):
|
46 |
+
chat_respone = controller.handle_submission_chat(user_message, response)
|
47 |
st.write(chat_respone)
|
48 |
st.markdown(response)
|
49 |
elif isinstance(response, str):
|
50 |
if "emojified_text" in response:
|
51 |
+
chat_respone = controller.handle_submission_chat(user_message, "Agent Tool created the text with emojies.")
|
52 |
st.write(chat_respone)
|
53 |
st.markdown(f"{response['emojified_text']}")
|
54 |
else:
|
55 |
+
chat_respone = controller.handle_submission_chat(user_message, response)
|
56 |
st.write(chat_respone)
|
57 |
st.markdown(response)
|
58 |
elif isinstance(response, list):
|
59 |
+
chat_respone = controller.handle_submission_chat(user_message, "Agent Tool produced a list")
|
60 |
for item in response:
|
61 |
st.markdown(item) # Assuming the list contains strings
|
62 |
+
st.write(chat_respone)
|
63 |
elif isinstance(response, pd.DataFrame):
|
64 |
+
chat_respone = controller.handle_submission_chat(user_message, "Agent Tool produced a pd.DataFrame")
|
65 |
+
st.write(chat_respone)
|
66 |
st.dataframe(response)
|
67 |
+
|
68 |
elif isinstance(response, pd.Series):
|
69 |
+
chat_respone = controller.handle_submission_chat(user_message, "Agent Tool produced a pd.Series")
|
70 |
+
st.write(chat_respone)
|
71 |
st.table(response.iloc[0:10])
|
72 |
elif isinstance(response, dict):
|
73 |
+
chat_respone = controller.handle_submission_chat(user_message, "Agent Tool produced a dict")
|
74 |
+
st.write(chat_respone)
|
75 |
st.json(response)
|
76 |
elif isinstance(response, st.graphics_altair.AltairChart):
|
77 |
+
chat_respone = controller.handle_submission_chat(user_message, "Agent Tool produced a st.graphics_altair.AltairChart")
|
78 |
+
st.write(chat_respone)
|
79 |
st.altair_chart(response)
|
80 |
elif isinstance(response, st.graphics_bokeh.BokehChart):
|
81 |
+
chat_respone = controller.handle_submission_chat(user_message, "Agent Tool produced a st.graphics_bokeh.BokehChart")
|
82 |
+
st.write(chat_respone)
|
83 |
st.bokeh_chart(response)
|
84 |
elif isinstance(response, st.graphics_graphviz.GraphvizChart):
|
85 |
+
chat_respone = controller.handle_submission_chat(user_message, "Agent Tool produced a st.graphics_graphviz.GraphvizChart")
|
86 |
+
st.write(chat_respone)
|
87 |
st.graphviz_chart(response)
|
88 |
elif isinstance(response, st.graphics_plotly.PlotlyChart):
|
89 |
+
chat_respone = controller.handle_submission_chat(user_message, "Agent Tool produced a st.graphics_plotly.PlotlyChart")
|
90 |
+
st.write(chat_respone)
|
91 |
st.plotly_chart(response)
|
92 |
elif isinstance(response, st.graphics_pydeck.PydeckChart):
|
93 |
+
chat_respone = controller.handle_submission_chat(user_message, "Agent Tool produced a st.graphics_pydeck.PydeckChart")
|
94 |
+
st.write(chat_respone)
|
95 |
st.pydeck_chart(response)
|
96 |
elif isinstance(response, matplotlib.figure.Figure):
|
97 |
+
chat_respone = controller.handle_submission_chat(user_message, "Agent Tool produced a matplotlib.figure.Figure")
|
98 |
+
st.write(chat_respone)
|
99 |
st.pyplot(response)
|
100 |
+
elif isinstance(response, st.graphics_vega_lite.VegaLiteChart):
|
101 |
+
chat_respone = controller.handle_submission_chat(user_message, "Agent Tool produced a VegaLiteChart")
|
102 |
+
st.write(chat_respone)
|
103 |
st.vega_lite_chart(response)
|
104 |
else:
|
105 |
st.warning("Unrecognized response type. Please try again. e.g. Generate an image of a flying horse.")
|
app_dev_desc.py β view/app_dev_desc.py
RENAMED
File without changes
|
view/app_header.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
|
4 |
+
def app_header(controller):
|
5 |
+
st.title("Hugging Face Agent and Tools")
|
6 |
+
|
7 |
+
## LB https://huggingface.co/spaces/qiantong-xu/toolbench-leaderboard
|
8 |
+
|
9 |
+
st.markdown("Welcome to the Hugging Face Agent and Tools app! This app allows you to interact with various tools using the Hugging Face Inference API. CustomTransformers can do anything \nπ€ͺπ€ππ€π€ͺ.")
|
10 |
+
|
view/app_sidebar.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
def app_sidebar(controller):
|
8 |
+
|
9 |
+
with st.sidebar:
|
10 |
+
st.header("Set Tools and Option. ")
|
11 |
+
with st.expander("Configure the agent and tools"):
|
12 |
+
configure(controller.agent_config)
|
13 |
+
with st.expander("Set the Content and Context"):
|
14 |
+
content_and_context(controller.agent_config)
|
15 |
+
|
16 |
+
def configure(agent_config):
|
17 |
+
st.markdown("Change the agent's configuration here.")
|
18 |
+
|
19 |
+
agent_config.url_endpoint = st.selectbox("Select Inference URL", [
|
20 |
+
"https://api-inference.huggingface.co/models/bigcode/starcoder",
|
21 |
+
"https://api-inference.huggingface.co/models/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5",
|
22 |
+
"https://api-inference.huggingface.co/models/gpt2"
|
23 |
+
])
|
24 |
+
|
25 |
+
agent_config.log_enabled = st.checkbox("Enable Logging")
|
26 |
+
|
27 |
+
agent_config.tool_checkboxes = [st.checkbox(f"{tool.name} --- {tool.description} ") for tool in agent_config.tool_loader.tools]
|
28 |
+
|
29 |
+
def content_and_context(agent_config):
|
30 |
+
agent_config.context = st.text_area("Context")
|
31 |
+
|
32 |
+
agent_config.image = st.camera_input("Take a picture")
|
33 |
+
|
34 |
+
img_file_buffer = st.file_uploader('Upload a PNG image', type='png')
|
35 |
+
|
36 |
+
if img_file_buffer is not None:
|
37 |
+
image_raw = Image.open(img_file_buffer)
|
38 |
+
#global image
|
39 |
+
agent_config.image = np.array(image_raw)
|
40 |
+
########
|
41 |
+
st.image(agent_config.image)
|
42 |
+
|
43 |
+
uploaded_file = st.file_uploader("Choose a pdf", type='pdf')
|
44 |
+
if uploaded_file is not None:
|
45 |
+
# To read file as bytes:
|
46 |
+
agent_config.document = uploaded_file.getvalue()
|
47 |
+
st.write(agent_config.document)
|
48 |
+
|
49 |
+
uploaded_txt_file = st.file_uploader("Choose a txt", type='txt')
|
50 |
+
if uploaded_txt_file is not None:
|
51 |
+
# To read file as bytes:
|
52 |
+
agent_config.document = uploaded_txt_file.getvalue()
|
53 |
+
st.write(agent_config.document)
|
54 |
+
|
55 |
+
uploaded_csv_file = st.file_uploader("Choose a csv", type='csv')
|
56 |
+
if uploaded_csv_file is not None:
|
57 |
+
# To read file as bytes:
|
58 |
+
agent_config.document = uploaded_csv_file.getvalue()
|
59 |
+
st.write(agent_config.document)
|
60 |
+
|
61 |
+
uploaded_csv_file = st.file_uploader("Choose audio", type='wav')
|
62 |
+
if uploaded_csv_file is not None:
|
63 |
+
# To read file as bytes:
|
64 |
+
agent_config.document = uploaded_csv_file.getvalue()
|
65 |
+
st.write(agent_config.document)
|
66 |
+
|
67 |
+
uploaded_csv_file = st.file_uploader("Choose video", type='avi')
|
68 |
+
if uploaded_csv_file is not None:
|
69 |
+
# To read file as bytes:
|
70 |
+
agent_config.document = uploaded_csv_file.getvalue()
|
71 |
+
st.write(agent_config.document)
|
72 |
+
|
73 |
+
# To convert to a string based IO:
|
74 |
+
#stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
|
75 |
+
#st.write(stringio)
|
76 |
+
|
77 |
+
# To read file as string:
|
78 |
+
#string_data = stringio.read()
|
79 |
+
#st.write(string_data)
|
80 |
+
|
81 |
+
# Can be used wherever a "file-like" object is accepted:
|
82 |
+
dataframe = pd.read_csv(uploaded_file)
|
83 |
+
st.write(dataframe)
|
84 |
+
|
app_user_desc.py β view/app_user_desc.py
RENAMED
File without changes
|