Spaces:
Sleeping
Sleeping
File size: 4,650 Bytes
de31f38 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
import gradio as gr
import random
def greet(name, is_morning, temperature):
"""
A simple greeting function that demonstrates multiple input types
"""
salutation = "Good morning" if is_morning else "Hello"
if name == "":
name = "stranger"
# Add some weather context
if temperature > 25:
weather_comment = "It's quite warm today!"
elif temperature < 10:
weather_comment = "It's quite cold today!"
else:
weather_comment = "The weather is nice today!"
# Add some randomness for fun
emoji = random.choice(["π", "π", "π", "π", "π"])
return f"{salutation} {name}! {weather_comment} {emoji}"
def analyze_text(text):
"""
Simple text analysis function
"""
if not text:
return "Please enter some text to analyze."
word_count = len(text.split())
char_count = len(text)
char_count_no_spaces = len(text.replace(" ", ""))
return f"""
π Text Analysis Results:
β’ Word count: {word_count}
β’ Character count: {char_count}
β’ Character count (no spaces): {char_count_no_spaces}
β’ Average word length: {char_count_no_spaces/word_count:.1f} characters
"""
# Create a tabbed interface with multiple demos
with gr.Blocks(title="TEST Gradio Space Demo", theme=gr.themes.Soft()) as demo:
gr.Markdown("# π TEST Gradio Space Demo")
gr.Markdown("Welcome to this demo space! This showcases different Gradio features.")
with gr.Tab("Greeting Generator"):
gr.Markdown("### Generate a personalized greeting")
with gr.Row():
with gr.Column():
name_input = gr.Textbox(
label="Your Name",
placeholder="Enter your name...",
value="World"
)
morning_checkbox = gr.Checkbox(
label="Is it morning?",
value=True
)
temp_slider = gr.Slider(
minimum=-10,
maximum=40,
value=20,
label="Temperature (Β°C)"
)
greet_btn = gr.Button("Generate Greeting", variant="primary")
with gr.Column():
greeting_output = gr.Textbox(
label="Your Greeting",
lines=3,
interactive=False
)
greet_btn.click(
fn=greet,
inputs=[name_input, morning_checkbox, temp_slider],
outputs=greeting_output
)
# Also trigger on enter in the name field
name_input.submit(
fn=greet,
inputs=[name_input, morning_checkbox, temp_slider],
outputs=greeting_output
)
with gr.Tab("Text Analyzer"):
gr.Markdown("### Analyze your text")
with gr.Row():
with gr.Column():
text_input = gr.Textbox(
label="Enter text to analyze",
lines=5,
placeholder="Type or paste your text here..."
)
analyze_btn = gr.Button("Analyze Text", variant="primary")
with gr.Column():
analysis_output = gr.Textbox(
label="Analysis Results",
lines=8,
interactive=False
)
analyze_btn.click(
fn=analyze_text,
inputs=text_input,
outputs=analysis_output
)
text_input.submit(
fn=analyze_text,
inputs=text_input,
outputs=analysis_output
)
with gr.Tab("About"):
gr.Markdown("""
### About this Space
This is a demonstration Gradio Space that shows:
- β
Multiple tabs with different interfaces
- β
Various input types (text, checkbox, slider)
- β
Interactive buttons and submit actions
- β
Simple text processing functions
- β
Modern UI with themes
### How to deploy your own:
1. Create files: `app.py`, `requirements.txt`, `README.md`
2. Push to a Hugging Face Space repository
3. Your app will be live automatically!
### API Access
This app also provides REST API endpoints. Check the "Use via API" section below for details.
""")
if __name__ == "__main__":
demo.launch() |