Update app.py
Browse files
app.py
CHANGED
@@ -50,10 +50,8 @@ image_transformation = load_tool("Chris4K/image-transformation")
|
|
50 |
latent_upscaler_tool = load_tool("Chris4K/latent-upscaler-tool")
|
51 |
|
52 |
tools = [random_character_tool, text_generation_tool, sentiment_tool, token_counter_tool, most_downloaded_model, word_counter_tool, sentence_counter_tool, emojify_text_tool , namedEntityRecognitionTool, sourcecode_retriever_tool, text_to_image, text_to_video, image_transformation, latent_upscaler_tool ]
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
# Define the custom HfAgent class
|
57 |
class CustomHfAgent(Agent):
|
58 |
def __init__(
|
59 |
self, url_endpoint, token=os.environ['HF_token'], chat_prompt_template=None, run_prompt_template=None, additional_tools=None, input_params=None
|
@@ -109,52 +107,44 @@ agent = CustomHfAgent(
|
|
109 |
# Display a welcome message
|
110 |
with st.chat_message("assistant"):
|
111 |
st.markdown("Hello there! How can I assist you today?")
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
-
#
|
114 |
-
|
115 |
-
# Input field for the user's message
|
116 |
-
user_message = st.text_input("User:", key="user_input")
|
117 |
-
|
118 |
-
# Check if the user wants to end the conversation
|
119 |
-
if user_message.lower() in ["bye", "goodbye", "exit"]:
|
120 |
-
with st.chat_message("assistant"):
|
121 |
-
st.markdown("Goodbye! Have a great day.")
|
122 |
-
break
|
123 |
|
124 |
-
|
125 |
-
|
|
|
|
|
126 |
|
127 |
-
|
128 |
-
submit_button = st.button("Submit")
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
st.markdown(response)
|
155 |
-
else:
|
156 |
-
st.warning("Unrecognized response type. Please try again.")
|
157 |
-
|
158 |
-
# Add the callback function to the Streamlit app
|
159 |
-
if submit_button:
|
160 |
-
handle_submission()
|
|
|
50 |
latent_upscaler_tool = load_tool("Chris4K/latent-upscaler-tool")
|
51 |
|
52 |
tools = [random_character_tool, text_generation_tool, sentiment_tool, token_counter_tool, most_downloaded_model, word_counter_tool, sentence_counter_tool, emojify_text_tool , namedEntityRecognitionTool, sourcecode_retriever_tool, text_to_image, text_to_video, image_transformation, latent_upscaler_tool ]
|
53 |
+
|
54 |
+
# Define the custom HfAgent class with token and input_params for e.g max_new_token
|
|
|
|
|
55 |
class CustomHfAgent(Agent):
|
56 |
def __init__(
|
57 |
self, url_endpoint, token=os.environ['HF_token'], chat_prompt_template=None, run_prompt_template=None, additional_tools=None, input_params=None
|
|
|
107 |
# Display a welcome message
|
108 |
with st.chat_message("assistant"):
|
109 |
st.markdown("Hello there! How can I assist you today?")
|
110 |
+
|
111 |
+
# Input field for the user's message
|
112 |
+
user_message = st.text_input("User:", key="user_input")
|
113 |
+
|
114 |
+
# Checkboxes for the tools to be used by the agent
|
115 |
+
tool_checkboxes = [st.checkbox(f"Use {tool.name} --- {tool.description} ") for tool in tools]
|
116 |
|
117 |
+
# Submit button
|
118 |
+
submit_button = st.button("Submit")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
+
# Define the callback function to handle the form submission
|
121 |
+
def handle_submission():
|
122 |
+
selected_tools = [tools[idx] for idx, checkbox in enumerate(tool_checkboxes) if checkbox]
|
123 |
+
agent.tools = selected_tools
|
124 |
|
125 |
+
response = agent.chat(user_message)
|
|
|
126 |
|
127 |
+
print("Agent Response\n {}".format(response))
|
128 |
+
|
129 |
+
# Display the agent's response
|
130 |
+
with st.chat_message("assistant"):
|
131 |
+
if response is None:
|
132 |
+
st.warning("The agent's response is None. Please try again.")
|
133 |
+
elif isinstance(response, Image.Image):
|
134 |
+
st.image(response)
|
135 |
+
elif "audio" in response:
|
136 |
+
audio_data = base64.b64decode(response.split(",")[1])
|
137 |
+
audio = AudioSegment.from_file(io.BytesIO(audio_data))
|
138 |
+
st.audio(audio)
|
139 |
+
elif isinstance(response, AudioSegment):
|
140 |
+
st.audio(response)
|
141 |
+
elif isinstance(response, str):
|
142 |
+
st.markdown(response)
|
143 |
+
elif "text" in response:
|
144 |
+
st.markdown(response)
|
145 |
+
else:
|
146 |
+
st.warning("Unrecognized response type. Please try again.")
|
147 |
+
|
148 |
+
# Add the callback function to the Streamlit app
|
149 |
+
if submit_button:
|
150 |
+
handle_submission()
|
|
|
|
|
|
|
|
|
|
|
|
|
|