Spaces:
Sleeping
Sleeping
Commit Β·
de31f38
1
Parent(s): 1729aea
Add comprehensive Gradio space demo with greeting generator and text analyzer
Browse files- .gitignore +75 -0
- README.md +63 -1
- app.py +147 -0
- requirements.txt +1 -0
.gitignore
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Virtual environment
|
| 2 |
+
env/
|
| 3 |
+
venv/
|
| 4 |
+
.env
|
| 5 |
+
.venv
|
| 6 |
+
|
| 7 |
+
# Python cache
|
| 8 |
+
__pycache__/
|
| 9 |
+
*.py[cod]
|
| 10 |
+
*$py.class
|
| 11 |
+
*.so
|
| 12 |
+
|
| 13 |
+
# Distribution / packaging
|
| 14 |
+
.Python
|
| 15 |
+
build/
|
| 16 |
+
develop-eggs/
|
| 17 |
+
dist/
|
| 18 |
+
downloads/
|
| 19 |
+
eggs/
|
| 20 |
+
.eggs/
|
| 21 |
+
lib/
|
| 22 |
+
lib64/
|
| 23 |
+
parts/
|
| 24 |
+
sdist/
|
| 25 |
+
var/
|
| 26 |
+
wheels/
|
| 27 |
+
*.egg-info/
|
| 28 |
+
.installed.cfg
|
| 29 |
+
*.egg
|
| 30 |
+
|
| 31 |
+
# PyInstaller
|
| 32 |
+
*.manifest
|
| 33 |
+
*.spec
|
| 34 |
+
|
| 35 |
+
# Unit test / coverage reports
|
| 36 |
+
htmlcov/
|
| 37 |
+
.tox/
|
| 38 |
+
.coverage
|
| 39 |
+
.coverage.*
|
| 40 |
+
.cache
|
| 41 |
+
nosetests.xml
|
| 42 |
+
coverage.xml
|
| 43 |
+
*.cover
|
| 44 |
+
.hypothesis/
|
| 45 |
+
.pytest_cache/
|
| 46 |
+
|
| 47 |
+
# Jupyter Notebook
|
| 48 |
+
.ipynb_checkpoints
|
| 49 |
+
|
| 50 |
+
# pyenv
|
| 51 |
+
.python-version
|
| 52 |
+
|
| 53 |
+
# Environments
|
| 54 |
+
.env
|
| 55 |
+
.venv
|
| 56 |
+
env/
|
| 57 |
+
venv/
|
| 58 |
+
ENV/
|
| 59 |
+
env.bak/
|
| 60 |
+
venv.bak/
|
| 61 |
+
|
| 62 |
+
# IDE files
|
| 63 |
+
.vscode/
|
| 64 |
+
.idea/
|
| 65 |
+
*.swp
|
| 66 |
+
*.swo
|
| 67 |
+
*~
|
| 68 |
+
|
| 69 |
+
# macOS
|
| 70 |
+
.DS_Store
|
| 71 |
+
|
| 72 |
+
# Windows
|
| 73 |
+
Thumbs.db
|
| 74 |
+
ehthumbs.db
|
| 75 |
+
Desktop.ini
|
README.md
CHANGED
|
@@ -7,6 +7,68 @@ sdk: gradio
|
|
| 7 |
sdk_version: 5.32.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
sdk_version: 5.32.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# TEST Gradio Space Demo
|
| 14 |
+
|
| 15 |
+
This is a comprehensive Gradio space demonstration that showcases various features and capabilities of the Gradio framework.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
|
| 19 |
+
### π― Greeting Generator
|
| 20 |
+
- Personalized greeting generation with multiple input types
|
| 21 |
+
- Name input field with placeholder text
|
| 22 |
+
- Morning/evening toggle checkbox
|
| 23 |
+
- Temperature slider for weather context
|
| 24 |
+
- Dynamic emoji selection
|
| 25 |
+
|
| 26 |
+
### π Text Analyzer
|
| 27 |
+
- Word count analysis
|
| 28 |
+
- Character count (with and without spaces)
|
| 29 |
+
- Average word length calculation
|
| 30 |
+
- Real-time text processing
|
| 31 |
+
|
| 32 |
+
### π About Section
|
| 33 |
+
- Documentation and usage instructions
|
| 34 |
+
- Feature overview
|
| 35 |
+
- Deployment guidelines
|
| 36 |
+
|
| 37 |
+
## Demo Highlights
|
| 38 |
+
|
| 39 |
+
- **Multiple Tabs**: Organized interface with different functionalities
|
| 40 |
+
- **Various Input Types**: Text fields, checkboxes, sliders, and buttons
|
| 41 |
+
- **Interactive Elements**: Submit on button click or Enter key
|
| 42 |
+
- **Modern UI**: Clean design with Gradio's Soft theme
|
| 43 |
+
- **API Access**: REST API endpoints for programmatic access
|
| 44 |
+
|
| 45 |
+
## How to Use
|
| 46 |
+
|
| 47 |
+
1. **Greeting Generator**: Enter your name, select morning/evening, adjust temperature, and generate a personalized greeting
|
| 48 |
+
2. **Text Analyzer**: Paste any text to get detailed analysis including word count and character statistics
|
| 49 |
+
3. **About**: Learn more about the demo and how to create your own Gradio space
|
| 50 |
+
|
| 51 |
+
## Local Development
|
| 52 |
+
|
| 53 |
+
To run this locally:
|
| 54 |
+
|
| 55 |
+
```bash
|
| 56 |
+
pip install gradio
|
| 57 |
+
python app.py
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
The app will be available at `http://localhost:7860`
|
| 61 |
+
|
| 62 |
+
## Deployment
|
| 63 |
+
|
| 64 |
+
This space demonstrates how easy it is to deploy Gradio apps on Hugging Face Spaces:
|
| 65 |
+
|
| 66 |
+
1. Create your `app.py` with Gradio interface
|
| 67 |
+
2. Add `requirements.txt` with dependencies
|
| 68 |
+
3. Include `README.md` with metadata
|
| 69 |
+
4. Push to Hugging Face Spaces repository
|
| 70 |
+
5. Your app goes live automatically!
|
| 71 |
+
|
| 72 |
+
## API Usage
|
| 73 |
+
|
| 74 |
+
This space provides REST API endpoints. Once deployed, you can access the API documentation and test the endpoints directly in the interface.
|
app.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
def greet(name, is_morning, temperature):
|
| 5 |
+
"""
|
| 6 |
+
A simple greeting function that demonstrates multiple input types
|
| 7 |
+
"""
|
| 8 |
+
salutation = "Good morning" if is_morning else "Hello"
|
| 9 |
+
if name == "":
|
| 10 |
+
name = "stranger"
|
| 11 |
+
|
| 12 |
+
# Add some weather context
|
| 13 |
+
if temperature > 25:
|
| 14 |
+
weather_comment = "It's quite warm today!"
|
| 15 |
+
elif temperature < 10:
|
| 16 |
+
weather_comment = "It's quite cold today!"
|
| 17 |
+
else:
|
| 18 |
+
weather_comment = "The weather is nice today!"
|
| 19 |
+
|
| 20 |
+
# Add some randomness for fun
|
| 21 |
+
emoji = random.choice(["π", "π", "π", "π", "π"])
|
| 22 |
+
|
| 23 |
+
return f"{salutation} {name}! {weather_comment} {emoji}"
|
| 24 |
+
|
| 25 |
+
def analyze_text(text):
|
| 26 |
+
"""
|
| 27 |
+
Simple text analysis function
|
| 28 |
+
"""
|
| 29 |
+
if not text:
|
| 30 |
+
return "Please enter some text to analyze."
|
| 31 |
+
|
| 32 |
+
word_count = len(text.split())
|
| 33 |
+
char_count = len(text)
|
| 34 |
+
char_count_no_spaces = len(text.replace(" ", ""))
|
| 35 |
+
|
| 36 |
+
return f"""
|
| 37 |
+
π Text Analysis Results:
|
| 38 |
+
|
| 39 |
+
β’ Word count: {word_count}
|
| 40 |
+
β’ Character count: {char_count}
|
| 41 |
+
β’ Character count (no spaces): {char_count_no_spaces}
|
| 42 |
+
β’ Average word length: {char_count_no_spaces/word_count:.1f} characters
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
# Create a tabbed interface with multiple demos
|
| 46 |
+
with gr.Blocks(title="TEST Gradio Space Demo", theme=gr.themes.Soft()) as demo:
|
| 47 |
+
gr.Markdown("# π TEST Gradio Space Demo")
|
| 48 |
+
gr.Markdown("Welcome to this demo space! This showcases different Gradio features.")
|
| 49 |
+
|
| 50 |
+
with gr.Tab("Greeting Generator"):
|
| 51 |
+
gr.Markdown("### Generate a personalized greeting")
|
| 52 |
+
|
| 53 |
+
with gr.Row():
|
| 54 |
+
with gr.Column():
|
| 55 |
+
name_input = gr.Textbox(
|
| 56 |
+
label="Your Name",
|
| 57 |
+
placeholder="Enter your name...",
|
| 58 |
+
value="World"
|
| 59 |
+
)
|
| 60 |
+
morning_checkbox = gr.Checkbox(
|
| 61 |
+
label="Is it morning?",
|
| 62 |
+
value=True
|
| 63 |
+
)
|
| 64 |
+
temp_slider = gr.Slider(
|
| 65 |
+
minimum=-10,
|
| 66 |
+
maximum=40,
|
| 67 |
+
value=20,
|
| 68 |
+
label="Temperature (Β°C)"
|
| 69 |
+
)
|
| 70 |
+
greet_btn = gr.Button("Generate Greeting", variant="primary")
|
| 71 |
+
|
| 72 |
+
with gr.Column():
|
| 73 |
+
greeting_output = gr.Textbox(
|
| 74 |
+
label="Your Greeting",
|
| 75 |
+
lines=3,
|
| 76 |
+
interactive=False
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
greet_btn.click(
|
| 80 |
+
fn=greet,
|
| 81 |
+
inputs=[name_input, morning_checkbox, temp_slider],
|
| 82 |
+
outputs=greeting_output
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# Also trigger on enter in the name field
|
| 86 |
+
name_input.submit(
|
| 87 |
+
fn=greet,
|
| 88 |
+
inputs=[name_input, morning_checkbox, temp_slider],
|
| 89 |
+
outputs=greeting_output
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
with gr.Tab("Text Analyzer"):
|
| 93 |
+
gr.Markdown("### Analyze your text")
|
| 94 |
+
|
| 95 |
+
with gr.Row():
|
| 96 |
+
with gr.Column():
|
| 97 |
+
text_input = gr.Textbox(
|
| 98 |
+
label="Enter text to analyze",
|
| 99 |
+
lines=5,
|
| 100 |
+
placeholder="Type or paste your text here..."
|
| 101 |
+
)
|
| 102 |
+
analyze_btn = gr.Button("Analyze Text", variant="primary")
|
| 103 |
+
|
| 104 |
+
with gr.Column():
|
| 105 |
+
analysis_output = gr.Textbox(
|
| 106 |
+
label="Analysis Results",
|
| 107 |
+
lines=8,
|
| 108 |
+
interactive=False
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
analyze_btn.click(
|
| 112 |
+
fn=analyze_text,
|
| 113 |
+
inputs=text_input,
|
| 114 |
+
outputs=analysis_output
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
text_input.submit(
|
| 118 |
+
fn=analyze_text,
|
| 119 |
+
inputs=text_input,
|
| 120 |
+
outputs=analysis_output
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
with gr.Tab("About"):
|
| 124 |
+
gr.Markdown("""
|
| 125 |
+
### About this Space
|
| 126 |
+
|
| 127 |
+
This is a demonstration Gradio Space that shows:
|
| 128 |
+
|
| 129 |
+
- β
Multiple tabs with different interfaces
|
| 130 |
+
- β
Various input types (text, checkbox, slider)
|
| 131 |
+
- β
Interactive buttons and submit actions
|
| 132 |
+
- β
Simple text processing functions
|
| 133 |
+
- β
Modern UI with themes
|
| 134 |
+
|
| 135 |
+
### How to deploy your own:
|
| 136 |
+
|
| 137 |
+
1. Create files: `app.py`, `requirements.txt`, `README.md`
|
| 138 |
+
2. Push to a Hugging Face Space repository
|
| 139 |
+
3. Your app will be live automatically!
|
| 140 |
+
|
| 141 |
+
### API Access
|
| 142 |
+
|
| 143 |
+
This app also provides REST API endpoints. Check the "Use via API" section below for details.
|
| 144 |
+
""")
|
| 145 |
+
|
| 146 |
+
if __name__ == "__main__":
|
| 147 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio
|