Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
12 |
print(f"Python version: {sys.version}")
|
13 |
print(f"Gradio version: {gr.__version__}")
|
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 |
-
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 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
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 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
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 |
-
|
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__":
|