Spaces:
Running
Running
Create demo.py
Browse files
demo.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# 简单的文本处理函数
|
| 4 |
+
def process_text(text):
|
| 5 |
+
return f"你输入了: {text}\n字符数: {len(text)}"
|
| 6 |
+
|
| 7 |
+
# 简单的计算函数
|
| 8 |
+
def calculator(a, b, operation):
|
| 9 |
+
if operation == "加法":
|
| 10 |
+
return f"{a} + {b} = {a + b}"
|
| 11 |
+
elif operation == "减法":
|
| 12 |
+
return f"{a} - {b} = {a - b}"
|
| 13 |
+
elif operation == "乘法":
|
| 14 |
+
return f"{a} × {b} = {a * b}"
|
| 15 |
+
elif operation == "除法":
|
| 16 |
+
if b == 0:
|
| 17 |
+
return "错误:除数不能为零"
|
| 18 |
+
return f"{a} ÷ {b} = {a / b:.2f}"
|
| 19 |
+
|
| 20 |
+
# 创建 Gradio 界面
|
| 21 |
+
with gr.Blocks(title="简单演示") as demo:
|
| 22 |
+
gr.Markdown("# 🎯 超级简单的 Gradio 演示")
|
| 23 |
+
|
| 24 |
+
with gr.Tab("文本处理"):
|
| 25 |
+
with gr.Row():
|
| 26 |
+
with gr.Column():
|
| 27 |
+
text_input = gr.Textbox(label="输入文本", placeholder="在这里输入文字...")
|
| 28 |
+
text_button = gr.Button("处理文本")
|
| 29 |
+
with gr.Column():
|
| 30 |
+
text_output = gr.Textbox(label="处理结果", interactive=False)
|
| 31 |
+
|
| 32 |
+
with gr.Tab("简单计算器"):
|
| 33 |
+
with gr.Row():
|
| 34 |
+
with gr.Column():
|
| 35 |
+
num1 = gr.Number(label="第一个数字", value=10)
|
| 36 |
+
num2 = gr.Number(label="第二个数字", value=5)
|
| 37 |
+
operation = gr.Radio(
|
| 38 |
+
choices=["加法", "减法", "乘法", "除法"],
|
| 39 |
+
label="选择运算",
|
| 40 |
+
value="加法"
|
| 41 |
+
)
|
| 42 |
+
calc_button = gr.Button("计算")
|
| 43 |
+
with gr.Column():
|
| 44 |
+
calc_output = gr.Textbox(label="计算结果", interactive=False)
|
| 45 |
+
|
| 46 |
+
with gr.Tab("关于"):
|
| 47 |
+
gr.Markdown("""
|
| 48 |
+
## 关于这个演示
|
| 49 |
+
|
| 50 |
+
这是一个超级简单的 Gradio 演示,包含:
|
| 51 |
+
- 📝 文本处理功能
|
| 52 |
+
- 🧮 简单计算器
|
| 53 |
+
- 🎨 美观的界面
|
| 54 |
+
|
| 55 |
+
不需要任何模型推理!
|
| 56 |
+
""")
|
| 57 |
+
|
| 58 |
+
# 连接事件处理
|
| 59 |
+
text_button.click(
|
| 60 |
+
fn=process_text,
|
| 61 |
+
inputs=text_input,
|
| 62 |
+
outputs=text_output
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
calc_button.click(
|
| 66 |
+
fn=calculator,
|
| 67 |
+
inputs=[num1, num2, operation],
|
| 68 |
+
outputs=calc_output
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# 启动应用
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
demo.launch(share=True) # share=True 会生成一个公共链接
|