Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,19 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
-
import base64
|
| 4 |
-
import io
|
| 5 |
-
from PIL import Image
|
| 6 |
-
from pydub import AudioSegment
|
| 7 |
-
import IPython
|
| 8 |
-
import soundfile as sf
|
| 9 |
-
import requests
|
| 10 |
-
import pandas as pd # If you're working with DataFrames
|
| 11 |
-
import matplotlib.figure # If you're using matplotlib figures
|
| 12 |
-
import numpy as np
|
| 13 |
|
| 14 |
-
from custom_agent import CustomHfAgent
|
| 15 |
from tool_loader import ToolLoader
|
| 16 |
-
from
|
| 17 |
-
from
|
|
|
|
|
|
|
| 18 |
from logger import log_response
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
import altair as alt
|
| 22 |
-
|
| 23 |
-
# For Bokeh charts
|
| 24 |
-
from bokeh.models import Plot
|
| 25 |
-
|
| 26 |
-
# For Plotly charts
|
| 27 |
-
import plotly.express as px
|
| 28 |
-
|
| 29 |
-
# For Pydeck charts
|
| 30 |
-
import pydeck as pdk
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
import logging
|
| 34 |
-
import streamlit as st
|
| 35 |
-
from transformers import load_tool, Agent
|
| 36 |
-
from tool_loader import ToolLoader
|
| 37 |
-
|
| 38 |
-
# Configure the logging settings for transformers
|
| 39 |
-
transformers_logger = logging.getLogger("transformers.file_utils")
|
| 40 |
-
transformers_logger.setLevel(logging.INFO) # Set the desired logging level
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
import time
|
| 44 |
-
import torch
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def handle_submission(user_message, selected_tools, url_endpoint):
|
| 48 |
-
|
| 49 |
-
log_response("User input \n {}".format(user_message))
|
| 50 |
-
log_response("selected_tools \n {}".format(selected_tools))
|
| 51 |
-
log_response("url_endpoint \n {}".format(url_endpoint))
|
| 52 |
-
|
| 53 |
-
agent = CustomHfAgent(
|
| 54 |
-
url_endpoint=url_endpoint,
|
| 55 |
-
token=os.environ['HF_token'],
|
| 56 |
-
additional_tools=selected_tools,
|
| 57 |
-
input_params={"max_new_tokens": 192},
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
response = agent.run(user_message)
|
| 61 |
-
|
| 62 |
-
log_response("Agent Response\n {}".format(response))
|
| 63 |
-
|
| 64 |
-
return response
|
| 65 |
|
| 66 |
# Declare global variable
|
| 67 |
global log_enabled
|
| 68 |
log_enabled = False
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# Create tool loader instance
|
| 76 |
-
tool_loader = ToolLoader(tool_names)
|
| 77 |
-
|
| 78 |
st.title("Hugging Face Agent and tools")
|
| 79 |
|
| 80 |
## LB https://huggingface.co/spaces/qiantong-xu/toolbench-leaderboard
|
|
@@ -92,83 +31,18 @@ with tabs[0]:
|
|
| 92 |
# Examples for the user perspective
|
| 93 |
st.markdown("Stat to chat. e.g. Generate an image of a boat. This will make the agent use the tool text2image to generate an image.")
|
| 94 |
|
| 95 |
-
|
| 96 |
# Tab 2: URL and Tools
|
| 97 |
with tabs[1]:
|
| 98 |
#
|
| 99 |
-
|
| 100 |
|
| 101 |
# Tab 3: User Description
|
| 102 |
with tabs[2]:
|
| 103 |
#
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
|
| 108 |
# Tab 4: Developers
|
| 109 |
with tabs[3]:
|
| 110 |
app_dev_desc()
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
# Chat code (user input, agent responses, etc.)
|
| 115 |
-
if "messages" not in st.session_state:
|
| 116 |
-
st.session_state.messages = []
|
| 117 |
-
|
| 118 |
-
for message in st.session_state.messages:
|
| 119 |
-
with st.chat_message(message["role"]):
|
| 120 |
-
st.markdown(message["content"])
|
| 121 |
-
|
| 122 |
-
with st.chat_message("assistant"):
|
| 123 |
-
st.markdown("Hello there! How can I assist you today?")
|
| 124 |
-
|
| 125 |
-
if user_message := st.chat_input("Enter message"):
|
| 126 |
-
st.chat_message("user").markdown(user_message)
|
| 127 |
-
st.session_state.messages.append({"role": "user", "content": user_message})
|
| 128 |
-
|
| 129 |
-
selected_tools = [tool_loader.tools[idx] for idx, checkbox in enumerate(tool_checkboxes) if checkbox]
|
| 130 |
-
# Handle submission with the selected inference URL
|
| 131 |
-
response = handle_submission(user_message, selected_tools, url_endpoint)
|
| 132 |
-
|
| 133 |
-
with st.chat_message("assistant"):
|
| 134 |
-
if response is None:
|
| 135 |
-
st.warning("The agent's response is None. Please try again. Generate an image of a flying horse.")
|
| 136 |
-
elif isinstance(response, Image.Image):
|
| 137 |
-
st.image(response)
|
| 138 |
-
elif isinstance(response, AudioSegment):
|
| 139 |
-
st.audio(response)
|
| 140 |
-
elif isinstance(response, int):
|
| 141 |
-
st.markdown(response)
|
| 142 |
-
elif isinstance(response, str):
|
| 143 |
-
if "emojified_text" in response:
|
| 144 |
-
st.markdown(f"{response['emojified_text']}")
|
| 145 |
-
else:
|
| 146 |
-
st.markdown(response)
|
| 147 |
-
elif isinstance(response, list):
|
| 148 |
-
for item in response:
|
| 149 |
-
st.markdown(item) # Assuming the list contains strings
|
| 150 |
-
elif isinstance(response, pd.DataFrame):
|
| 151 |
-
st.dataframe(response)
|
| 152 |
-
elif isinstance(response, pd.Series):
|
| 153 |
-
st.table(response.iloc[0:10])
|
| 154 |
-
elif isinstance(response, dict):
|
| 155 |
-
st.json(response)
|
| 156 |
-
elif isinstance(response, st.graphics_altair.AltairChart):
|
| 157 |
-
st.altair_chart(response)
|
| 158 |
-
elif isinstance(response, st.graphics_bokeh.BokehChart):
|
| 159 |
-
st.bokeh_chart(response)
|
| 160 |
-
elif isinstance(response, st.graphics_graphviz.GraphvizChart):
|
| 161 |
-
st.graphviz_chart(response)
|
| 162 |
-
elif isinstance(response, st.graphics_plotly.PlotlyChart):
|
| 163 |
-
st.plotly_chart(response)
|
| 164 |
-
elif isinstance(response, st.graphics_pydeck.PydeckChart):
|
| 165 |
-
st.pydeck_chart(response)
|
| 166 |
-
elif isinstance(response, matplotlib.figure.Figure):
|
| 167 |
-
st.pyplot(response)
|
| 168 |
-
elif isinstance(response, streamlit.graphics_vega_lite.VegaLiteChart):
|
| 169 |
-
st.vega_lite_chart(response)
|
| 170 |
-
else:
|
| 171 |
-
st.warning("Unrecognized response type. Please try again. e.g. Generate an image of a flying horse.")
|
| 172 |
-
|
| 173 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 174 |
-
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
| 3 |
from tool_loader import ToolLoader
|
| 4 |
+
from app_chat import app_chat
|
| 5 |
+
from app_user_desc import app_user_desc
|
| 6 |
+
from app_dev_desc import app_dev_desc
|
| 7 |
+
from app_agent_config import app_agent_config
|
| 8 |
from logger import log_response
|
| 9 |
|
| 10 |
+
#from transformers import load_tool, Agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Declare global variable
|
| 13 |
global log_enabled
|
| 14 |
log_enabled = False
|
| 15 |
|
| 16 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
st.title("Hugging Face Agent and tools")
|
| 18 |
|
| 19 |
## LB https://huggingface.co/spaces/qiantong-xu/toolbench-leaderboard
|
|
|
|
| 31 |
# Examples for the user perspective
|
| 32 |
st.markdown("Stat to chat. e.g. Generate an image of a boat. This will make the agent use the tool text2image to generate an image.")
|
| 33 |
|
|
|
|
| 34 |
# Tab 2: URL and Tools
|
| 35 |
with tabs[1]:
|
| 36 |
#
|
| 37 |
+
app_agent_config()
|
| 38 |
|
| 39 |
# Tab 3: User Description
|
| 40 |
with tabs[2]:
|
| 41 |
#
|
| 42 |
+
app_user_desc()
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Tab 4: Developers
|
| 45 |
with tabs[3]:
|
| 46 |
app_dev_desc()
|
| 47 |
|
| 48 |
+
app_chat()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|