greendra commited on
Commit
87106fd
·
verified ·
1 Parent(s): 92cbf13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -65
app.py CHANGED
@@ -1,9 +1,3 @@
1
- Okay, let's integrate gr.Examples into your script with some diverse, high-quality Stable Diffusion-style prompts and enable caching.
2
-
3
- Caching examples means that when the Space first builds (or rebuilds after changes), it will run the query function once for each example and store the resulting image. When a user clicks that example later, the cached image is shown instantly instead of running the model again.
4
-
5
- Here's the modified app.py:
6
-
7
  import gradio as gr
8
  import requests
9
  import io
@@ -270,62 +264,4 @@ with gr.Blocks(theme=theme, css=css) as app:
270
  # share=False is correct for Hugging Face Spaces deployment
271
  # show_api=False is fine unless you specifically need to expose the API endpoint documentation
272
  # debug=True can be useful for development but remove for production/sharing
273
- app.launch(show_api=False, share=False)
274
-
275
-
276
- Key Changes:
277
-
278
- Import typing: Added Callable, List, Any, Literal for better type hinting, though not strictly required.
279
-
280
- Error Handling in query: Made error handling slightly more robust, checking for token availability, using response.raise_for_status(), catching specific request exceptions (like Timeout, 503), and providing clearer Gradio errors (gr.Error, gr.Warning). Also added basic translation error handling.
281
-
282
- API Payload: Adjusted the payload structure slightly based on common inference API patterns (e.g., num_inference_steps, guidance_scale). Added notes that the specific model might ignore some parameters. Handles -1 seed better.
283
-
284
- Default Negative Prompt: Stored the default negative prompt in a variable for reuse in examples.
285
-
286
- example_list: Defined a list of lists. Each inner list contains values for all the inputs to the query function, in the correct order. Includes diverse prompts and some parameter variations. Added a Russian example.
287
-
288
- gr.Examples Instantiation:
289
-
290
- Placed gr.Examples(...) within the gr.Blocks context, after the main input/output components.
291
-
292
- examples=example_list: Passed the defined list.
293
-
294
- inputs=[...]: Listed all the input components (gr.Textbox, gr.Slider, etc.) in the exact order corresponding to the data in example_list.
295
-
296
- outputs=image_output: Specified the output component.
297
-
298
- fn=query: Crucially, provided the query function. This tells Gradio how to generate the results for caching.
299
-
300
- cache_examples=True: This enables the caching mechanism.
301
-
302
- Added label and examples_per_page for better UI.
303
-
304
- run_on_click is typically False or omitted when cache_examples=True, as the point is to show the pre-computed result. Set it to True only if you want clicking an example to re-run the generation even if it's cached (useful if you want users to easily try variations from an example starting point).
305
-
306
- UI Tweaks: Increased prompt textbox lines, adjusted slider steps/ranges, added info text to some sliders/radios.
307
-
308
- Theme Loading: Added a try...except block for loading the custom theme to fall back gracefully if it's not found.
309
-
310
- API Token Handling: Added basic handling for multiple tokens via an environment variable HF_EXTRA_TOKENS (comma-separated) and rotation.
311
-
312
- Before Running:
313
-
314
- Update requirements.txt: Ensure gradio (version >= 4.x recommended for latest features/fixes), requests, pillow, deep-translator are listed. You likely don't need langdetect anymore if you removed it.
315
-
316
- requests
317
- pillow
318
- deep-translator
319
- gradio>=4.44.1 # Use the version suggested or newer
320
- IGNORE_WHEN_COPYING_START
321
- content_copy
322
- download
323
- Use code with caution.
324
- Txt
325
- IGNORE_WHEN_COPYING_END
326
-
327
- Set Environment Variables: Make sure HF_READ_TOKEN is set in your Space secrets. Optionally set HF_EXTRA_TOKENS if you have more tokens.
328
-
329
- Commit and Push: Save app.py and requirements.txt, commit, and push to your Space.
330
-
331
- The first time the Space builds after these changes, it will take longer as it runs query for each example to build the cache. Subsequent loads will be faster, and clicking examples will show results instantly.
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import requests
3
  import io
 
264
  # share=False is correct for Hugging Face Spaces deployment
265
  # show_api=False is fine unless you specifically need to expose the API endpoint documentation
266
  # debug=True can be useful for development but remove for production/sharing
267
+ app.launch(show_api=False, share=False)