File size: 3,590 Bytes
6e19f63
f189ec9
1368c37
018aa15
f189ec9
ece917e
f189ec9
 
018aa15
07aeb52
f189ec9
 
 
07aeb52
f189ec9
018aa15
f189ec9
 
07aeb52
f189ec9
 
 
 
 
e9c6962
9833992
f189ec9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07aeb52
f189ec9
 
9e8ab62
f189ec9
 
 
 
 
 
44abb7c
 
f189ec9
 
 
 
 
 
07aeb52
f189ec9
 
 
07aeb52
f189ec9
 
 
 
 
 
 
 
7928d62
fc0b4cb
9833992
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
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)