nakas's picture
Update app.py
f189ec9 verified
raw
history blame
3.59 kB
import gradio as gr
import sys
import tempfile
import subprocess
import requests
import time
import os
import re
import atexit
# Print Python version and gradio version for debugging
print(f"Python version: {sys.version}")
print(f"Gradio version: {gr.__version__}")
# Create a direct interface that doesn't use any subprocess
with gr.Blocks() as demo:
gr.Markdown("# 🤖 Simple Gradio App Demo")
gr.Markdown("Select a simple demo app to try:")
app_type = gr.Radio(
["Hello World", "Calculator", "Text Analysis"],
label="Choose an App Type",
value="Hello World"
)
with gr.Row():
name_input = gr.Textbox(label="Your Name", value="World")
greeting_output = gr.Textbox(label="Result")
with gr.Row(visible=False) as calculator_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")
with gr.Row(visible=False) as text_row:
text_input = gr.Textbox(label="Enter some text", lines=3)
text_result = gr.Textbox(label="Analysis")
# 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}"
# Switch UI based on selection
def update_ui(choice):
if choice == "Hello World":
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
elif choice == "Calculator":
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
else: # Text Analysis
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
app_type.change(
update_ui,
inputs=[app_type],
outputs=[
gr.Row.update(visible=True),
calculator_row,
text_row
]
)
# Connect buttons to functions
name_input.submit(greet, inputs=[name_input], outputs=[greeting_output])
# Calculator connections
calc_button = gr.Button("Calculate")
calc_button.click(
calculate,
inputs=[num1, num2, operation],
outputs=[calc_result]
)
# Text analysis connection
analyze_button = gr.Button("Analyze Text")
analyze_button.click(
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
Select different app types above to try them out.
""")
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)