Chris4K commited on
Commit
7155419
·
verified ·
1 Parent(s): e8494e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -49
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
- # Main chat loop
114
- while True:
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
- # Checkboxes for the tools to be used by the agent
125
- tool_checkboxes = [st.checkbox(f"Use {tool.name} --- {tool.description} ") for tool in tools]
 
 
126
 
127
- # Submit button
128
- submit_button = st.button("Submit")
129
 
130
- # Define the callback function to handle the form submission
131
- def handle_submission():
132
- selected_tools = [tools[idx] for idx, checkbox in enumerate(tool_checkboxes) if checkbox]
133
- agent.tools = selected_tools
134
-
135
- response = agent.chat(user_message)
136
-
137
- print("Agent Response\n {}".format(response))
138
-
139
- # Display the agent's response
140
- with st.chat_message("assistant"):
141
- if response is None:
142
- st.warning("The agent's response is None. Please try again.")
143
- elif isinstance(response, Image.Image):
144
- st.image(response)
145
- elif "audio" in response:
146
- audio_data = base64.b64decode(response.split(",")[1])
147
- audio = AudioSegment.from_file(io.BytesIO(audio_data))
148
- st.audio(audio)
149
- elif isinstance(response, AudioSegment):
150
- st.audio(response)
151
- elif isinstance(response, str):
152
- st.markdown(response)
153
- elif "text" in response:
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()