File size: 2,282 Bytes
8555c0a
45f7a70
 
8555c0a
d9d5680
45f7a70
 
8555c0a
45f7a70
 
 
 
 
 
 
 
 
d9d5680
 
 
45f7a70
 
d9d5680
45f7a70
d9d5680
 
45f7a70
d9d5680
45f7a70
 
d9d5680
 
 
 
 
 
 
 
 
 
 
 
45f7a70
d9d5680
45f7a70
 
 
d9d5680
45f7a70
d9d5680
 
 
 
 
45f7a70
d9d5680
 
 
 
 
 
 
 
45f7a70
d9d5680
45f7a70
 
 
d9d5680
 
 
 
 
 
 
 
45f7a70
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import gradio as gr
import json
import os

# 配置路径
DATA_PATH = "data/students.json"
PHOTO_DIR = "data/photos"

def load_data():
    if os.path.exists(DATA_PATH):
        with open(DATA_PATH, 'r', encoding='utf-8') as f:
            return json.load(f)
    return []

students_data = load_data()

def search_student(name):
    # 清除输入空格
    target_name = name.strip()
    result = next((s for s in students_data if s["name"] == target_name), None)
    
    if not result:
        return "❌ 未找到该同学信息", "", "无", None
    
    # 获取字段信息
    stu_class = result.get("class", "初59级二班")
    status = result.get("status", "未知")
    phone = result.get("phone", "").strip()
    photo_path = result.get("photo", "")
    
    # 格式化文本输出
    info_html = f"""
    <div style='line-height: 1.8; font-size: 16px;'>
        <b>基本信息:</b><br>
        📍 班级:{stu_class}<br>
        📌 状态:{status}
    </div>
    """
    
    phone_display = phone if phone else "未登记"
    
    # 照片逻辑
    if not photo_path or not os.path.exists(photo_path):
        display_photo = None
    else:
        display_photo = photo_path

    return info_html, phone_display, display_photo

# 构建美化界面
with gr.Blocks(title="初59级二班同学录") as demo:
    gr.Markdown("# 🎓 初59级二班同学信息查询系统")
    gr.Markdown("---")
    
    with gr.Row():
        with gr.Column(scale=1):
            input_name = gr.Textbox(label="输入同学姓名", placeholder="请输入完整姓名...", lines=1)
            search_btn = gr.Button("🔍 立即查询", variant="primary")
            output_phone = gr.Label(label="联系电话")
            
        with gr.Column(scale=2):
            output_info = gr.HTML(label="详细状态")
            output_photo = gr.Image(label="同学照片", type="filepath")

    # 绑定事件
    search_btn.click(
        fn=search_student,
        inputs=input_name,
        outputs=[output_info, output_phone, output_photo]
    )
    
    # 支持回车搜索
    input_name.submit(
        fn=search_student,
        inputs=input_name,
        outputs=[output_info, output_phone, output_photo]
    )

if __name__ == "__main__":
    demo.launch()