Spaces:
Sleeping
Sleeping
import gradio as gr | |
import sys | |
# Print version info for debugging | |
print(f"Python version: {sys.version}") | |
print(f"Gradio version: {gr.__version__}") | |
# Hello World function | |
def greet(name): | |
return f"Hello, {name}!" | |
# Calculator function | |
def calculate(num1, num2, op): | |
if op == "Add": | |
return str(num1 + num2) | |
elif op == "Subtract": | |
return str(num1 - num2) | |
elif op == "Multiply": | |
return str(num1 * num2) | |
elif op == "Divide": | |
if num2 == 0: | |
return "Error: Division by zero" | |
return str(num1 / num2) | |
# Text Analysis function | |
def analyze_text(text): | |
if not text: | |
return "Please enter some text" | |
num_chars = len(text) | |
num_words = len(text.split()) | |
num_lines = len(text.splitlines()) | |
return f"Characters: {num_chars}\nWords: {num_words}\nLines: {num_lines}" | |
# Create a tab-based interface with simple demos | |
with gr.Blocks() as demo: | |
gr.Markdown("# 🤖 Simple Gradio Demo Apps") | |
with gr.Tab("Hello World"): | |
with gr.Row(): | |
name_input = gr.Textbox(label="Your Name", value="World") | |
greeting_output = gr.Textbox(label="Greeting") | |
greet_btn = gr.Button("Say Hello") | |
greet_btn.click(fn=greet, inputs=name_input, outputs=greeting_output) | |
with gr.Tab("Calculator"): | |
with gr.Row(): | |
num1 = gr.Number(label="First Number", value=5) | |
num2 = gr.Number(label="Second Number", value=3) | |
operation = gr.Radio( | |
["Add", "Subtract", "Multiply", "Divide"], | |
label="Operation", | |
value="Add" | |
) | |
calc_result = gr.Textbox(label="Result") | |
calc_btn = gr.Button("Calculate") | |
calc_btn.click(fn=calculate, inputs=[num1, num2, operation], outputs=calc_result) | |
with gr.Tab("Text Analysis"): | |
text_input = gr.Textbox(label="Enter text to analyze", lines=5) | |
text_result = gr.Textbox(label="Analysis Result") | |
analyze_btn = gr.Button("Analyze Text") | |
analyze_btn.click(fn=analyze_text, inputs=text_input, outputs=text_result) | |
gr.Markdown(""" | |
### About This Demo | |
This is a simple demonstration of Gradio functionality with: | |
1. A Hello World text generator | |
2. A basic calculator | |
3. A text analysis tool | |
Use the tabs above to try different demo apps. | |
""") | |
if __name__ == "__main__": | |
demo.launch(server_name="0.0.0.0", server_port=7860) |