import gradio as gr
import os
import shutil
import datetime
import socket
import time
import random
from pathlib import Path
# --- 1. 基础配置 ---
SAVE_DIR = "数据资产归档"
if not os.path.exists(SAVE_DIR):
os.makedirs(SAVE_DIR)
# 虽然不显示在网页上了,但后台依然保留链接捕获,方便你在黑窗口查看
public_url_storage = {"url": ""}
def handle_upload(name, files):
if not name or not name.strip():
return gr.update(
value="### <div style='color:#d93025; background:#ffebee; padding:15px; border-radius:10px;'>⚠ 身份核验失败:请输入姓名</div>")
if not files:
return gr.update(
value="### <div style='color:#d93025; background:#ffebee; padding:15px; border-radius:10px;'>⚠ 未检测到资产文件</div>")
try:
user_folder = os.path.join(SAVE_DIR, name.strip())
if not os.path.exists(user_folder):
os.makedirs(user_folder)
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
file_list_html = "".join([f"<li>{Path(f.name).name}</li>" for f in files])
for file in files:
shutil.copy(file.name, os.path.join(user_folder, Path(file.name).name))
return f"""
<div style='background:#e8f5e9; border-left:8px solid #2e7d32; padding:20px; border-radius:12px; margin-top:15px;'>
<h3 style='color:#1b5e20; margin:0;'>✅ 归档存证成功</h3>
<p style='color:#388e3c;'><b>权属人:</b>{name}<br><b>保存目录:</b>{SAVE_DIR}/{name}/</p>
<ul style='color:#2e7d32; font-size:13px; margin:5px 0;'>{file_list_html}</ul>
</div>
"""
except Exception as e:
return f"### <div style='color:white; background:#b71c1c; padding:20px; border-radius:10px;'>❌ 传输故障: {str(e)}</div>"
def get_random_gradient():
"""生成随机的静态 CSS 渐变背景"""
gradients = [
"linear-gradient(135deg, #667eea 0%, #764ba2 100%)",
"linear-gradient(135deg, #f093fb 0%, #f5576c 100%)",
"linear-gradient(135deg, #5ee7df 0%, #b490ca 100%)",
"linear-gradient(135deg, #c3cfe2 0%, #c3cfe2 100%)",
"linear-gradient(135deg, #f6d365 0%, #fda085 100%)",
"linear-gradient(135deg, #a1c4fd 0%, #c2e9fb 100%)",
"linear-gradient(135deg, #89f7fe 0%, #66a6ff 100%)",
"linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)"
]
return random.choice(gradients)
def create_pro_ui():
bg_style = get_random_gradient()
custom_css = f"""
.gradio-container {{
background: {bg_style} !important;
min-height: 100vh !important;
display: flex !important;
align-items: center !important;
justify-content: center !important;
}}
.main-box {{
background: white !important;
border-radius: 28px !important;
box-shadow: 0 30px 60px rgba(0,0,0,0.2) !important;
padding: 40px !important;
max-width: 520px !important;
width: 100% !important;
margin: auto !important;
}}
.title-text h1 {{ font-size: 28px !important; font-weight: 800 !important; color: #1e293b !important; text-align: center !important; margin-bottom: 30px; }}
.send-btn {{ background: #0078d4 !important; color: white !important; border-radius: 12px !important; height: 55px !important; font-weight: 700 !important; border:none !important; cursor:pointer !important; width: 100% !important; margin-top: 10px; }}
footer {{ display: none !important; }}
"""
with gr.Blocks(title="数字资产归档系统", css=custom_css) as app:
with gr.Column(elem_classes=["main-box"]):
gr.HTML(
'<div style="text-align:center; margin-bottom:15px;"><svg width="55" height="55" viewBox="0 0 48 48" fill="none"><path d="M24 44C35.0457 44 44 35.0457 44 24C44 12.9543 35.0457 4 24 4C12.9543 4 4 12.9543 4 24C4 35.0457 12.9543 44 24 44Z" fill="#0078d4"/><path d="M16 24L22 30L32 18" stroke="white" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/></svg></div>')
gr.Markdown("# 资产原名归档中心", elem_classes=["title-text"])
# --- 删除了之前的 get_dynamic_url 和 timer 刷新区 ---
with gr.Group():
name_input = gr.Textbox(label="权属人身份核验", placeholder="请输入姓名")
file_input = gr.File(label="载入数字资产", file_count="multiple")
submit_btn = gr.Button("🔗 开始同步归档", elem_classes=["send-btn"])
output_msg = gr.HTML()
submit_btn.click(fn=handle_upload, inputs=[name_input, file_input], outputs=output_msg)
return app
if __name__ == "__main__":
demo = create_pro_ui()
port = 7860
success = False
while not success and port < 7900:
try:
print(f"正在尝试启动端口: {port}...")
# 依然保留 share=True,外网链接请在启动后的控制台(黑窗口)复制
_, _, share_url = demo.launch(
server_name="0.0.0.0",
server_port=port,
share=True,
prevent_thread_lock=True
)
success = True
if share_url:
print(f"\n✅ 外网地址已生成(请手动复制): {share_url}\n")
except Exception:
port += 1
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
demo.close()