Update app.py
Browse files
app.py
CHANGED
@@ -125,166 +125,167 @@ tabs = st.tabs(["Chat", "URL and Tools", "User Description", "Developers"])
|
|
125 |
|
126 |
# Tab 1: Chat
|
127 |
if tabs[0]:
|
128 |
-
with st.expander("Chat"):
|
129 |
-
# Code for URL and Tools checkboxes
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
143 |
|
144 |
|
145 |
# Tab 2: URL and Tools
|
146 |
elif tabs[1]:
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
st.markdown(" - Sample: What is the sentiment for \"Hello, I am happy\"?")
|
158 |
-
|
159 |
-
st.markdown("3. **Word Count**:")
|
160 |
-
st.markdown(" - Choose the desired URL and the 'Word Counter Tool'.")
|
161 |
-
st.markdown(" - Sample: Count the words in \"Hello, I am Christof\".")
|
162 |
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
|
170 |
-
|
171 |
-
|
172 |
|
173 |
# Tab 3: User Description
|
174 |
elif tabs[2]:
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
|
232 |
# Tab 4: Developers
|
233 |
elif tabs[3]:
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
|
|
|
|
286 |
|
287 |
-
''')
|
288 |
# Chat code (user input, agent responses, etc.)
|
289 |
if "messages" not in st.session_state:
|
290 |
st.session_state.messages = []
|
|
|
125 |
|
126 |
# Tab 1: Chat
|
127 |
if tabs[0]:
|
|
|
|
|
128 |
|
129 |
+
# Code for URL and Tools checkboxes
|
130 |
+
|
131 |
+
# Examples for the user perspective
|
132 |
+
st.markdown("### Examples:")
|
133 |
+
st.markdown("1. **Generate a Random Character**:")
|
134 |
+
st.markdown(" - Choose the desired URL and the 'Random Character Tool'.")
|
135 |
+
|
136 |
+
st.markdown("2. **Sentiment Analysis**:")
|
137 |
+
st.markdown(" - Choose the desired URL and the 'Sentiment Analysis Tool'.")
|
138 |
+
st.markdown(" - Sample: What is the sentiment for \"Hello, I am happy\"?")
|
139 |
+
|
140 |
+
st.markdown("3. **Word Count**:")
|
141 |
+
st.markdown(" - Choose the desired URL and the 'Word Counter Tool'.")
|
142 |
+
st.markdown(" - Sample: Count the words in \"Hello, I am Christof\".")
|
143 |
|
144 |
|
145 |
# Tab 2: URL and Tools
|
146 |
elif tabs[1]:
|
147 |
+
|
148 |
+
# Code for URL and Tools checkboxes
|
149 |
+
|
150 |
+
# Examples for the user perspective
|
151 |
+
st.markdown("### Examples:")
|
152 |
+
st.markdown("1. **Generate a Random Character**:")
|
153 |
+
st.markdown(" - Choose the desired URL and the 'Random Character Tool'.")
|
154 |
|
155 |
+
st.markdown("2. **Sentiment Analysis**:")
|
156 |
+
st.markdown(" - Choose the desired URL and the 'Sentiment Analysis Tool'.")
|
157 |
+
st.markdown(" - Sample: What is the sentiment for \"Hello, I am happy\"?")
|
158 |
+
|
159 |
+
st.markdown("3. **Word Count**:")
|
160 |
+
st.markdown(" - Choose the desired URL and the 'Word Counter Tool'.")
|
161 |
+
st.markdown(" - Sample: Count the words in \"Hello, I am Christof\".")
|
|
|
|
|
|
|
|
|
|
|
162 |
|
163 |
+
# Add a dropdown for selecting the inference URL
|
164 |
+
url_endpoint = st.selectbox("Select Inference URL", [
|
165 |
+
"https://api-inference.huggingface.co/models/bigcode/starcoder",
|
166 |
+
"https://api-inference.huggingface.co/models/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5",
|
167 |
+
"https://api-inference.huggingface.co/models/gpt2"
|
168 |
+
])
|
169 |
|
170 |
+
|
171 |
+
tool_checkboxes = [st.checkbox(f"{tool.name} --- {tool.description} ") for tool in tool_loader.tools]
|
172 |
|
173 |
# Tab 3: User Description
|
174 |
elif tabs[2]:
|
175 |
+
|
176 |
+
# User description content and tool descriptions
|
177 |
+
# Add a section for the app's description
|
178 |
+
st.markdown('''
|
179 |
+
# Hugging Face Agent and Tools App
|
180 |
+
|
181 |
+
## Description
|
182 |
+
Welcome to the Hugging Face Agent and Tools app! This app provides an interactive interface for utilizing various tools through the Hugging Face API. You can choose an inference URL and select from a variety of tools to perform different tasks.
|
183 |
+
|
184 |
+
## Examples
|
185 |
+
1. **Generate a Random Character**:
|
186 |
+
- Choose the desired URL and the 'Random Character Tool'.
|
187 |
+
|
188 |
+
2. **Sentiment Analysis**:
|
189 |
+
- Choose the desired URL and the 'Sentiment Analysis Tool'.
|
190 |
+
- Sample: What is the sentiment for "Hello, I am happy"?
|
191 |
+
|
192 |
+
3. **Word Count**:
|
193 |
+
- Choose the desired URL and the 'Word Counter Tool'.
|
194 |
+
- Sample: Count the words in "Hello, I am Christof".
|
195 |
+
|
196 |
+
## Tools
|
197 |
+
To interact with the tools, expand the section below to see tool descriptions and select the tools you want to use.
|
198 |
+
|
199 |
+
<details>
|
200 |
+
<summary>Expand to see tool descriptions</summary>
|
201 |
+
|
202 |
+
### Tool Descriptions
|
203 |
+
- **random-character-tool:** Generates a random character.
|
204 |
+
- **text-generation-tool:** Generates text based on a prompt.
|
205 |
+
- **sentiment-tool:** Analyzes the sentiment of a given text.
|
206 |
+
- **token-counter-tool:** Counts the tokens in a text.
|
207 |
+
- **most-downloaded-model:** Provides information about the most downloaded model.
|
208 |
+
- **rag-tool:** Utilizes Retrieval-Augmented Generation (RAG) for text generation.
|
209 |
+
- **word-counter-tool:** Counts the words in a text.
|
210 |
+
- **sentence-counter-tool:** Counts the sentences in a text.
|
211 |
+
- **EmojifyTextTool:** Emojifies the given text.
|
212 |
+
- **NamedEntityRecognitionTool:** Identifies named entities in a text.
|
213 |
+
- **TextDownloadTool:** Downloads text from a given URL.
|
214 |
+
- **source-code-retriever-tool:** Retrieves source code from a given URL.
|
215 |
+
- **text-to-image:** Generates an image from text.
|
216 |
+
- **text-to-video:** Generates a video from text.
|
217 |
+
- **image-transformation:** Applies transformations to images.
|
218 |
+
- **latent-upscaler-tool:** Upscales images using latent space.
|
219 |
+
|
220 |
+
</details>
|
221 |
+
|
222 |
+
## Usage
|
223 |
+
1. Choose the desired inference URL from the dropdown.
|
224 |
+
2. Expand the tool selection section and choose the tools you want to use.
|
225 |
+
3. Enter a message in the chat input to interact with the Hugging Face Agent.
|
226 |
+
4. View the assistant's responses, which may include images, audio, text, or other visualizations based on the selected tools.
|
227 |
+
|
228 |
+
Feel free to explore and experiment with different tools to achieve various tasks!
|
229 |
+
|
230 |
+
''')
|
231 |
|
232 |
# Tab 4: Developers
|
233 |
elif tabs[3]:
|
234 |
+
|
235 |
+
# Developer-related content
|
236 |
+
st.markdown('''
|
237 |
+
|
238 |
+
# Hugging Face Agent and Tools Code Overview
|
239 |
+
|
240 |
+
## Overview
|
241 |
+
The provided Python code implements an interactive Streamlit web application that allows users to interact with various tools through the Hugging Face API. The app integrates Hugging Face models and tools, enabling users to perform tasks such as text generation, sentiment analysis, and more.
|
242 |
+
|
243 |
+
## Imports
|
244 |
+
The code imports several external libraries and modules, including:
|
245 |
+
- `streamlit`: For building the web application.
|
246 |
+
- `os`: For interacting with the operating system.
|
247 |
+
- `base64`, `io`, `Image` (from `PIL`), `AudioSegment` (from `pydub`), `IPython`, `sf`: For handling images and audio.
|
248 |
+
- `requests`: For making HTTP requests.
|
249 |
+
- `pandas`: For working with DataFrames.
|
250 |
+
- `matplotlib.figure`, `numpy`: For visualization.
|
251 |
+
- `altair`, `Plot` (from `bokeh.models`), `px` (from `plotly.express`), `pdk` (from `pydeck`): For different charting libraries.
|
252 |
+
- `time`: For handling time-related operations.
|
253 |
+
- `transformers`: For loading tools and agents.
|
254 |
+
|
255 |
+
## ToolLoader Class
|
256 |
+
The `ToolLoader` class is responsible for loading tools based on their names. It has methods to load tools from a list of tool names and handles potential errors during loading.
|
257 |
+
|
258 |
+
## CustomHfAgent Class
|
259 |
+
The `CustomHfAgent` class extends the base `Agent` class from the `transformers` module. It is designed to interact with a remote inference API and includes methods for generating text based on a given prompt.
|
260 |
+
|
261 |
+
## Tool Loading and Customization
|
262 |
+
- Tool names are defined in the `tool_names` list.
|
263 |
+
- The `ToolLoader` instance (`tool_loader`) loads tools based on the provided names.
|
264 |
+
- The `CustomHfAgent` instance (`agent`) is created with a specified URL endpoint, token, and additional tools.
|
265 |
+
- New tools can be added by appending their names to the `tool_names` list.
|
266 |
+
|
267 |
+
## Streamlit App
|
268 |
+
The Streamlit app is structured as follows:
|
269 |
+
1. Tool selection dropdown for choosing the inference URL.
|
270 |
+
2. An expander for displaying tool descriptions.
|
271 |
+
3. An expander for selecting tools.
|
272 |
+
4. Examples and instructions for the user.
|
273 |
+
5. A chat interface for user interactions.
|
274 |
+
6. Handling of user inputs, tool selection, and agent responses.
|
275 |
+
|
276 |
+
## Handling of Responses
|
277 |
+
The code handles various types of responses from the agent, including images, audio, text, DataFrames, and charts. The responses are displayed in the Streamlit app based on their types.
|
278 |
+
|
279 |
+
## How to Run
|
280 |
+
1. Install required dependencies with `pip install -r requirements.txt`.
|
281 |
+
2. Run the app with `streamlit run <filename.py>`.
|
282 |
+
|
283 |
+
## Notes
|
284 |
+
- The code emphasizes customization and extensibility, allowing developers to easily add new tools and interact with the Hugging Face API.
|
285 |
+
- Ensure proper configuration, such as setting the Hugging Face token as an environment variable.
|
286 |
+
|
287 |
+
''')
|
288 |
|
|
|
289 |
# Chat code (user input, agent responses, etc.)
|
290 |
if "messages" not in st.session_state:
|
291 |
st.session_state.messages = []
|