Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import time
|
| 4 |
+
import os
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
def greet(name):
|
| 9 |
+
"""簡單的文字處理函數"""
|
| 10 |
+
return f"你好,{name}!歡迎使用Hugging Face空間測試應用!"
|
| 11 |
+
|
| 12 |
+
def process_image(img):
|
| 13 |
+
"""基本圖像處理函數"""
|
| 14 |
+
if img is None:
|
| 15 |
+
return None, "請上傳一張圖片"
|
| 16 |
+
|
| 17 |
+
# 獲取圖像尺寸資訊
|
| 18 |
+
img_array = np.array(img)
|
| 19 |
+
height, width = img_array.shape[:2]
|
| 20 |
+
|
| 21 |
+
# 創建簡單的處理效果:水平翻轉
|
| 22 |
+
flipped = np.fliplr(img_array)
|
| 23 |
+
|
| 24 |
+
return flipped, f"圖像已處理!原始尺寸: {width}x{height}像素"
|
| 25 |
+
|
| 26 |
+
def plot_function(formula, x_min, x_max):
|
| 27 |
+
"""繪製數學函數圖像"""
|
| 28 |
+
try:
|
| 29 |
+
x = np.linspace(x_min, x_max, 100)
|
| 30 |
+
# 安全地評估函數表達式
|
| 31 |
+
# 注意:這種方式在實際應用中可能存在安全風險,但對於簡單演示足夠了
|
| 32 |
+
y = eval(formula)
|
| 33 |
+
|
| 34 |
+
plt.figure(figsize=(10, 6))
|
| 35 |
+
plt.plot(x, y)
|
| 36 |
+
plt.grid(True)
|
| 37 |
+
plt.title(f"函數: {formula}")
|
| 38 |
+
plt.xlabel("X")
|
| 39 |
+
plt.ylabel("Y")
|
| 40 |
+
|
| 41 |
+
# 保存到臨時文件
|
| 42 |
+
temp_file = "temp_plot.png"
|
| 43 |
+
plt.savefig(temp_file)
|
| 44 |
+
plt.close()
|
| 45 |
+
|
| 46 |
+
return Image.open(temp_file), f"繪製完成: {formula}, x範圍: [{x_min}, {x_max}]"
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return None, f"錯誤: {str(e)}"
|
| 49 |
+
|
| 50 |
+
def countdown(seconds):
|
| 51 |
+
"""測試長時間運行的處理"""
|
| 52 |
+
for i in range(seconds, 0, -1):
|
| 53 |
+
time.sleep(1)
|
| 54 |
+
yield f"倒數計時: {i}秒..."
|
| 55 |
+
yield "完成!"
|
| 56 |
+
|
| 57 |
+
# 創建Gradio界面
|
| 58 |
+
with gr.Blocks(title="Hugging Face部署測試") as demo:
|
| 59 |
+
gr.Markdown("# Hugging Face部署測試應用")
|
| 60 |
+
gr.Markdown("此應用旨在測試您的Hugging Face空間是否能正常部署和運行基本功能。")
|
| 61 |
+
|
| 62 |
+
with gr.Tab("文字處理"):
|
| 63 |
+
with gr.Row():
|
| 64 |
+
name_input = gr.Textbox(label="請輸入您的名字")
|
| 65 |
+
greet_output = gr.Textbox(label="問候訊息")
|
| 66 |
+
greet_button = gr.Button("問候")
|
| 67 |
+
greet_button.click(fn=greet, inputs=name_input, outputs=greet_output)
|
| 68 |
+
|
| 69 |
+
with gr.Tab("圖像處理"):
|
| 70 |
+
with gr.Row():
|
| 71 |
+
image_input = gr.Image(label="上傳圖片", type="numpy")
|
| 72 |
+
image_output = gr.Image(label="處理結果")
|
| 73 |
+
image_info = gr.Textbox(label="圖像信息")
|
| 74 |
+
process_button = gr.Button("處理圖片")
|
| 75 |
+
process_button.click(fn=process_image, inputs=image_input, outputs=[image_output, image_info])
|
| 76 |
+
|
| 77 |
+
with gr.Tab("函數繪圖"):
|
| 78 |
+
with gr.Row():
|
| 79 |
+
formula_input = gr.Textbox(label="函數表達式 (使用x作為變數)", value="np.sin(x)")
|
| 80 |
+
x_min = gr.Number(label="X最小值", value=-10)
|
| 81 |
+
x_max = gr.Number(label="X最大值", value=10)
|
| 82 |
+
plot_output = gr.Image(label="函數圖像")
|
| 83 |
+
plot_info = gr.Textbox(label="繪圖信息")
|
| 84 |
+
plot_button = gr.Button("繪製圖像")
|
| 85 |
+
plot_button.click(fn=plot_function, inputs=[formula_input, x_min, x_max], outputs=[plot_output, plot_info])
|
| 86 |
+
|
| 87 |
+
with gr.Tab("長時間處理"):
|
| 88 |
+
seconds_input = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="等待秒數")
|
| 89 |
+
countdown_output = gr.Textbox(label="倒數計時")
|
| 90 |
+
countdown_button = gr.Button("開始倒數")
|
| 91 |
+
countdown_button.click(fn=countdown, inputs=seconds_input, outputs=countdown_output)
|
| 92 |
+
|
| 93 |
+
gr.Markdown("""
|
| 94 |
+
## 功能測試說明
|
| 95 |
+
|
| 96 |
+
此應用包含四個基本功能區塊,用於測試不同類型的功能:
|
| 97 |
+
|
| 98 |
+
1. **文字處理** - 測試基本文字輸入輸出
|
| 99 |
+
2. **圖像處理** - 測試圖像上傳和處理
|
| 100 |
+
3. **函數繪圖** - 測試matplotlib繪圖功能
|
| 101 |
+
4. **長時間處理** - 測試較長時間運行的處理函數
|
| 102 |
+
|
| 103 |
+
如果這些功能都能正常運行,則表明您的Hugging Face空間配置正確。
|
| 104 |
+
""")
|
| 105 |
+
|
| 106 |
+
gr.Markdown(f"應用運行環境信息: Python + Gradio {gr.__version__}")
|
| 107 |
+
|
| 108 |
+
# 系統信息檢查
|
| 109 |
+
def check_system_info():
|
| 110 |
+
"""收集系統信息"""
|
| 111 |
+
import sys
|
| 112 |
+
import platform
|
| 113 |
+
info = [
|
| 114 |
+
f"Python版本: {sys.version}",
|
| 115 |
+
f"平台: {platform.platform()}",
|
| 116 |
+
f"Gradio版本: {gr.__version__}",
|
| 117 |
+
f"NumPy版本: {np.__version__}",
|
| 118 |
+
f"Matplotlib版本: {plt.matplotlib.__version__}",
|
| 119 |
+
]
|
| 120 |
+
print("\n".join(info))
|
| 121 |
+
print("系統檢查完成,準備啟動應用...")
|
| 122 |
+
|
| 123 |
+
# 為確保應用在Hugging Face上正確執行
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
check_system_info()
|
| 126 |
+
demo.launch()
|