Chris4K commited on
Commit
377076c
·
verified ·
1 Parent(s): c218a80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -69
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}, # Set the desired value
102
  )
103
 
 
 
 
104
 
105
- # Define the callback function to handle the form submission
106
- def handle_submission():
107
- # Get the user's message and the selected tools
108
- #message = st.text_input("Enter your message:", "")
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
- print(selected_tools)
 
 
 
 
116
 
117
- agent.tools = selected_tools
 
118
 
119
-
120
- # Run the agent with the user's message and selected tools
121
- #response = agent.run(message)
122
- response = agent.chat(message)
123
 
124
- print("Agent Response\n {}".format(response))
125
-
126
- # Display the agent's response
127
- if response is None:
128
- st.warning("The agent's response is None. Please try again. For Example: Generate an image of a boat in the water")
129
- elif isinstance(response, Image.Image):
130
- # Display the image response
131
- st.image(response)
132
- elif "audio" in response:
133
- # Handle audio response (replace with your audio rendering code)
134
- audio_data = base64.b64decode(response.split(",")[1])
135
- audio = AudioSegment.from_file(io.BytesIO(audio_data))
136
- st.audio(audio)
137
- elif isinstance(response, AudioSegment):
138
- # Handle audio response (replace with your audio rendering code)
139
- st.audio(response)
140
- elif isinstance(response, str):
141
- # Display the text response
142
- st.write(response)
143
- elif "text" in response:
144
- # Display the text response
145
- st.write(response)
146
- else:
147
- # Handle unrecognized response type
148
- st.warning("Unrecognized response type. Please try again. For Example: Generate an image of a boat in the water")
149
-
150
-
151
-
152
- # Add the callback function to the Streamlit app
153
- submit_button = st.button("Submit", on_click=handle_submission)
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()