Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -115,74 +115,4 @@ with gr.Blocks() as demo:
|
|
115 |
)
|
116 |
|
117 |
if __name__ == "__main__":
|
118 |
-
demo.launch()
|
119 |
-
|
120 |
-
|
121 |
-
Key changes and explanations:
|
122 |
-
|
123 |
-
Gradio Integration: The code is now structured to work within a Gradio interface. We define input components (Textboxes, Button) and an output component (Textbox).
|
124 |
-
|
125 |
-
filter_and_push Function: The core logic is encapsulated in a function. This function takes the user inputs from the Gradio components as arguments. This is crucial for Gradio to work correctly.
|
126 |
-
|
127 |
-
Error Handling: The code includes comprehensive try...except blocks to catch potential errors, such as:
|
128 |
-
|
129 |
-
Invalid Hugging Face token.
|
130 |
-
|
131 |
-
Problems loading the dataset.
|
132 |
-
|
133 |
-
Repository creation errors (checks if the repo already exists).
|
134 |
-
|
135 |
-
Issues during the dataset upload.
|
136 |
-
|
137 |
-
No matching examples are found.
|
138 |
-
|
139 |
-
Casting error
|
140 |
-
These errors are reported to the user through the Gradio output Textbox, providing helpful feedback.
|
141 |
-
|
142 |
-
Keyword Processing: The keywords_input is now a single Textbox where users can enter comma-separated keywords. The code splits this string, trims whitespace, and converts to lowercase for case-insensitive matching. An empty keyword list defaults to the original animal keywords
|
143 |
-
|
144 |
-
HF Token Handling: The HF token is now an input field (with type="password" for security). It's passed directly to the filter_and_push function.
|
145 |
-
|
146 |
-
Return Values for Gradio: The filter_and_push function now returns two values:
|
147 |
-
|
148 |
-
A status message (string) to display in the output_text Textbox.
|
149 |
-
|
150 |
-
The dataset URL (string) or None to the dataset output link.
|
151 |
-
|
152 |
-
Dataset URL Output: After a successful upload, the URL of the newly created dataset is displayed in the dataset_output_link Textbox, making it easy for the user to access their filtered dataset.
|
153 |
-
|
154 |
-
Clearer Instructions and Labels: The Gradio interface has descriptive labels for each input field, making it user-friendly.
|
155 |
-
|
156 |
-
if __name__ == "__main__": block: This standard Python construct ensures that demo.launch() is only called when the script is run directly (not when imported as a module).
|
157 |
-
|
158 |
-
No Hardcoded Values: All user-configurable parameters are now taken as inputs through the Gradio interface, making the space more flexible.
|
159 |
-
|
160 |
-
How to run this:
|
161 |
-
|
162 |
-
Install Libraries:
|
163 |
-
|
164 |
-
pip install gradio datasets huggingface_hub
|
165 |
-
IGNORE_WHEN_COPYING_START
|
166 |
-
content_copy
|
167 |
-
download
|
168 |
-
Use code with caution.
|
169 |
-
Bash
|
170 |
-
IGNORE_WHEN_COPYING_END
|
171 |
-
|
172 |
-
Save: Save the code as a Python file (e.g., app.py).
|
173 |
-
|
174 |
-
Run:
|
175 |
-
|
176 |
-
python app.py
|
177 |
-
IGNORE_WHEN_COPYING_START
|
178 |
-
content_copy
|
179 |
-
download
|
180 |
-
Use code with caution.
|
181 |
-
Bash
|
182 |
-
IGNORE_WHEN_COPYING_END
|
183 |
-
|
184 |
-
Open in Browser: Gradio will provide a local URL (usually http://127.0.0.1:7860) that you can open in your web browser.
|
185 |
-
|
186 |
-
Hugging Face Login (Important): While you don't need to use huggingface-cli login with this Gradio app (because you're entering the token directly), you do need a Hugging Face account, and you need to generate an API token with "write" access. You can create a token here: https://huggingface.co/settings/tokens
|
187 |
-
|
188 |
-
This improved version provides a robust and user-friendly Hugging Face Space for filtering and uploading datasets. It handles errors gracefully, provides clear feedback, and follows best practices for Gradio applications.
|
|
|
115 |
)
|
116 |
|
117 |
if __name__ == "__main__":
|
118 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|