Update app.py
Browse files
app.py
CHANGED
@@ -81,84 +81,63 @@ class CustomHfAgent(Agent):
|
|
81 |
|
82 |
st.title("Hugging Face Agent and tools")
|
83 |
|
84 |
-
# Input field for the user's message
|
85 |
-
message = st.text_input("Enter your message:", "")
|
86 |
-
|
87 |
-
# Checkboxes for the tools to be used by the agent
|
88 |
-
|
89 |
-
|
90 |
-
tool_checkboxes = [st.checkbox(f"Use {tool.name} --- {tool.description} ") for tool in tools]
|
91 |
-
|
92 |
-
# Submit button
|
93 |
-
#submit_button = st.button("Submit")
|
94 |
-
|
95 |
# Initialize the agent
|
96 |
-
|
97 |
agent = CustomHfAgent(
|
98 |
url_endpoint="https://api-inference.huggingface.co/models/bigcode/starcoder",
|
99 |
token=os.environ['HF_token'],
|
100 |
additional_tools=[],
|
101 |
-
input_params={"max_new_tokens": 192},
|
102 |
)
|
103 |
|
|
|
|
|
|
|
104 |
|
105 |
-
#
|
106 |
-
|
107 |
-
#
|
108 |
-
|
109 |
-
|
110 |
-
#selected_tools = []
|
111 |
-
#selected_tools = [tool for idx, tool in enumerate(tools) if tool_checkboxes[idx]]
|
112 |
-
selected_tools = [tools[idx] for idx, checkbox in enumerate(tool_checkboxes) if checkbox]
|
113 |
-
#selected_tools = [tool for idx, checkbox in enumerate(tool_checkboxes) if checkbox]
|
114 |
|
115 |
-
|
|
|
|
|
|
|
|
|
116 |
|
117 |
-
|
|
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
#response = agent.run(message)
|
122 |
-
response = agent.chat(message)
|
123 |
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
#
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
# Add the callback function to the Streamlit app
|
153 |
-
submit_button
|
154 |
-
|
155 |
-
# Define a callback function to handle the button click
|
156 |
-
def ask_again():
|
157 |
-
# Reset the message input field
|
158 |
-
message_input.value = ""
|
159 |
-
|
160 |
-
# Run the agent again with an empty message
|
161 |
-
agent.run("")
|
162 |
-
|
163 |
-
# Add the callback function to the button
|
164 |
-
ask_again = st.button("Ask again", on_click=ask_again)
|
|
|
81 |
|
82 |
st.title("Hugging Face Agent and tools")
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
# Initialize the agent
|
|
|
85 |
agent = CustomHfAgent(
|
86 |
url_endpoint="https://api-inference.huggingface.co/models/bigcode/starcoder",
|
87 |
token=os.environ['HF_token'],
|
88 |
additional_tools=[],
|
89 |
+
input_params={"max_new_tokens": 192},
|
90 |
)
|
91 |
|
92 |
+
# Display a welcome message
|
93 |
+
with st.chat_message("assistant"):
|
94 |
+
st.markdown("Hello there! How can I assist you today?")
|
95 |
|
96 |
+
# Main chat loop
|
97 |
+
while True:
|
98 |
+
# Input field for the user's message
|
99 |
+
user_message = st.text_input("User:", "")
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
+
# Check if the user wants to end the conversation
|
102 |
+
if user_message.lower() in ["bye", "goodbye", "exit"]:
|
103 |
+
with st.chat_message("assistant"):
|
104 |
+
st.markdown("Goodbye! Have a great day.")
|
105 |
+
break
|
106 |
|
107 |
+
# Checkboxes for the tools to be used by the agent
|
108 |
+
tool_checkboxes = [st.checkbox(f"Use {tool.name} --- {tool.description} ") for tool in tools]
|
109 |
|
110 |
+
# Submit button
|
111 |
+
submit_button = st.button("Submit")
|
|
|
|
|
112 |
|
113 |
+
# Define the callback function to handle the form submission
|
114 |
+
def handle_submission():
|
115 |
+
selected_tools = [tools[idx] for idx, checkbox in enumerate(tool_checkboxes) if checkbox]
|
116 |
+
agent.tools = selected_tools
|
117 |
+
|
118 |
+
response = agent.chat(user_message)
|
119 |
+
|
120 |
+
print("Agent Response\n {}".format(response))
|
121 |
+
|
122 |
+
# Display the agent's response
|
123 |
+
with st.chat_message("assistant"):
|
124 |
+
if response is None:
|
125 |
+
st.warning("The agent's response is None. Please try again.")
|
126 |
+
elif isinstance(response, Image.Image):
|
127 |
+
st.image(response)
|
128 |
+
elif "audio" in response:
|
129 |
+
audio_data = base64.b64decode(response.split(",")[1])
|
130 |
+
audio = AudioSegment.from_file(io.BytesIO(audio_data))
|
131 |
+
st.audio(audio)
|
132 |
+
elif isinstance(response, AudioSegment):
|
133 |
+
st.audio(response)
|
134 |
+
elif isinstance(response, str):
|
135 |
+
st.markdown(response)
|
136 |
+
elif "text" in response:
|
137 |
+
st.markdown(response)
|
138 |
+
else:
|
139 |
+
st.warning("Unrecognized response type. Please try again.")
|
140 |
+
|
141 |
+
# Add the callback function to the Streamlit app
|
142 |
+
if submit_button:
|
143 |
+
handle_submission()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|