Chris4K commited on
Commit
f259d93
·
verified ·
1 Parent(s): 0608f06

Update app.py

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