TEST / app.py
HarshHrs24's picture
Add comprehensive Gradio space demo with greeting generator and text analyzer
de31f38
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()