nakas commited on
Commit
1f2f75f
·
verified ·
1 Parent(s): f189ec9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -93
app.py CHANGED
@@ -1,118 +1,81 @@
1
  import gradio as gr
2
  import sys
3
- import tempfile
4
- import subprocess
5
- import requests
6
- import time
7
- import os
8
- import re
9
- import atexit
10
 
11
- # Print Python version and gradio version for debugging
12
  print(f"Python version: {sys.version}")
13
  print(f"Gradio version: {gr.__version__}")
14
 
15
- # Create a direct interface that doesn't use any subprocess
16
- with gr.Blocks() as demo:
17
- gr.Markdown("# 🤖 Simple Gradio App Demo")
18
- gr.Markdown("Select a simple demo app to try:")
19
-
20
- app_type = gr.Radio(
21
- ["Hello World", "Calculator", "Text Analysis"],
22
- label="Choose an App Type",
23
- value="Hello World"
24
- )
25
-
26
- with gr.Row():
27
- name_input = gr.Textbox(label="Your Name", value="World")
28
- greeting_output = gr.Textbox(label="Result")
29
-
30
- with gr.Row(visible=False) as calculator_row:
31
- num1 = gr.Number(label="First Number", value=5)
32
- num2 = gr.Number(label="Second Number", value=3)
33
- operation = gr.Radio(["Add", "Subtract", "Multiply", "Divide"], label="Operation", value="Add")
34
- calc_result = gr.Textbox(label="Result")
 
35
 
36
- with gr.Row(visible=False) as text_row:
37
- text_input = gr.Textbox(label="Enter some text", lines=3)
38
- text_result = gr.Textbox(label="Analysis")
39
 
40
- # Hello World function
41
- def greet(name):
42
- return f"Hello, {name}!"
 
 
43
 
44
- # Calculator function
45
- def calculate(num1, num2, op):
46
- if op == "Add":
47
- return str(num1 + num2)
48
- elif op == "Subtract":
49
- return str(num1 - num2)
50
- elif op == "Multiply":
51
- return str(num1 * num2)
52
- elif op == "Divide":
53
- if num2 == 0:
54
- return "Error: Division by zero"
55
- return str(num1 / num2)
56
 
57
- # Text Analysis function
58
- def analyze_text(text):
59
- if not text:
60
- return "Please enter some text"
61
 
62
- num_chars = len(text)
63
- num_words = len(text.split())
64
- num_lines = len(text.splitlines())
 
 
65
 
66
- return f"Characters: {num_chars}\nWords: {num_words}\nLines: {num_lines}"
67
-
68
- # Switch UI based on selection
69
- def update_ui(choice):
70
- if choice == "Hello World":
71
- return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
72
- elif choice == "Calculator":
73
- return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
74
- else: # Text Analysis
75
- return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
76
-
77
- app_type.change(
78
- update_ui,
79
- inputs=[app_type],
80
- outputs=[
81
- gr.Row.update(visible=True),
82
- calculator_row,
83
- text_row
84
- ]
85
- )
86
-
87
- # Connect buttons to functions
88
- name_input.submit(greet, inputs=[name_input], outputs=[greeting_output])
89
 
90
- # Calculator connections
91
- calc_button = gr.Button("Calculate")
92
- calc_button.click(
93
- calculate,
94
- inputs=[num1, num2, operation],
95
- outputs=[calc_result]
96
- )
97
 
98
- # Text analysis connection
99
- analyze_button = gr.Button("Analyze Text")
100
- analyze_button.click(
101
- analyze_text,
102
- inputs=[text_input],
103
- outputs=[text_result]
104
- )
105
-
106
  gr.Markdown("""
107
  ### About This Demo
108
 
109
  This is a simple demonstration of Gradio functionality with:
110
 
111
  1. A Hello World text generator
112
- 2. A basic calculator
113
  3. A text analysis tool
114
 
115
- Select different app types above to try them out.
116
  """)
117
 
118
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import sys
 
 
 
 
 
 
 
3
 
4
+ # Print version info for debugging
5
  print(f"Python version: {sys.version}")
6
  print(f"Gradio version: {gr.__version__}")
7
 
8
+ # Hello World function
9
+ def greet(name):
10
+ return f"Hello, {name}!"
11
+
12
+ # Calculator function
13
+ def calculate(num1, num2, op):
14
+ if op == "Add":
15
+ return str(num1 + num2)
16
+ elif op == "Subtract":
17
+ return str(num1 - num2)
18
+ elif op == "Multiply":
19
+ return str(num1 * num2)
20
+ elif op == "Divide":
21
+ if num2 == 0:
22
+ return "Error: Division by zero"
23
+ return str(num1 / num2)
24
+
25
+ # Text Analysis function
26
+ def analyze_text(text):
27
+ if not text:
28
+ return "Please enter some text"
29
 
30
+ num_chars = len(text)
31
+ num_words = len(text.split())
32
+ num_lines = len(text.splitlines())
33
 
34
+ return f"Characters: {num_chars}\nWords: {num_words}\nLines: {num_lines}"
35
+
36
+ # Create a tab-based interface with simple demos
37
+ with gr.Blocks() as demo:
38
+ gr.Markdown("# 🤖 Simple Gradio Demo Apps")
39
 
40
+ with gr.Tab("Hello World"):
41
+ with gr.Row():
42
+ name_input = gr.Textbox(label="Your Name", value="World")
43
+ greeting_output = gr.Textbox(label="Greeting")
44
+
45
+ greet_btn = gr.Button("Say Hello")
46
+ greet_btn.click(fn=greet, inputs=name_input, outputs=greeting_output)
 
 
 
 
 
47
 
48
+ with gr.Tab("Calculator"):
49
+ with gr.Row():
50
+ num1 = gr.Number(label="First Number", value=5)
51
+ num2 = gr.Number(label="Second Number", value=3)
52
 
53
+ operation = gr.Radio(
54
+ ["Add", "Subtract", "Multiply", "Divide"],
55
+ label="Operation",
56
+ value="Add"
57
+ )
58
 
59
+ calc_result = gr.Textbox(label="Result")
60
+ calc_btn = gr.Button("Calculate")
61
+ calc_btn.click(fn=calculate, inputs=[num1, num2, operation], outputs=calc_result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ with gr.Tab("Text Analysis"):
64
+ text_input = gr.Textbox(label="Enter text to analyze", lines=5)
65
+ text_result = gr.Textbox(label="Analysis Result")
66
+ analyze_btn = gr.Button("Analyze Text")
67
+ analyze_btn.click(fn=analyze_text, inputs=text_input, outputs=text_result)
 
 
68
 
 
 
 
 
 
 
 
 
69
  gr.Markdown("""
70
  ### About This Demo
71
 
72
  This is a simple demonstration of Gradio functionality with:
73
 
74
  1. A Hello World text generator
75
+ 2. A basic calculator
76
  3. A text analysis tool
77
 
78
+ Use the tabs above to try different demo apps.
79
  """)
80
 
81
  if __name__ == "__main__":