File size: 5,642 Bytes
852403f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
692cb96
852403f
 
32f4a1a
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# main.py
import gradio as gr
from analyzer import analyze_teacher_dashboard
from cluster_insight import cluster_and_visualize


# ================== LLM 预留接口(未来接入通义千问)==================
# main.py → analyze_report 内
from qwen_api import call_qwen

def generate_teaching_advice(sankey_b64, cluster_stats):
    # 1. 构造 Prompt(结构化、专业)
    prompt = f"""
你是一名GIS实验教学专家,基于以下分析结果,生成教学优化方案:

【桑基图分析】
- 学生反馈从 s1→s4 的主要流向:核密度 → 参数设置 → 应用场景
- 最粗路径:核密度分析 → 搜索半径选择 → 城市规划应用

【聚类分析】
"""
    for s in cluster_stats[:3]:  # 取 Top 3 聚类
        prompt += f"- 聚类 {s['cluster_id']}{s['keyword']}{s['size']}条,占{s['ratio']:.1%})\n"
        prompt += f"  代表句:{s['rep_sentence'][:100]}\n"

    prompt += """
        【要求】
        1. 诊断核心教学痛点(3条)
        2. 提出针对性优化措施(微课/演示/作业)
        3. 设计 1 个 2 分钟微课脚本(标题+3步演示)
        4. 建议 1 个课后作业(验证学生掌握)
        
        【输出格式】
        # 教学优化方案
        ## 1. 核心痛点
        ## 2. 优化措施
        ## 3. 微课脚本
        ## 4. 课后作业
        """

    # 2. 调用通义千问
    advice = call_qwen(prompt)
    return f"<pre style='background:#f8f9fa; padding:15px; border-radius:8px; white-space: pre-wrap;'>{advice}</pre>"


# ================== Gradio 界面 ==================
def analyze_report(file):
    if not file:
        return "请上传 Excel 文件", None

    try:
        # 1. 分析 → 桑基图
        sankey_b64 = analyze_teacher_dashboard(excel_path = file.name)
        # 2. 聚类图
        cluster_b64, cluster_stats  = cluster_and_visualize( excel_path=file.name )
        # print(cluster_b64)
        # print(cluster_stats)
        # 3. 生成教学建议
        advice = generate_teaching_advice(sankey_b64, cluster_stats)

        # 4. 聚类统计表格
        stats_table = """
                <h3>聚类主题统计</h3>
                <table border="1" style="width:100%; border-collapse: collapse; text-align:center; font-size:14px;">
                    <tr style="background:#f0f0f0;">
                        <th>聚类</th><th>主题关键词</th><th>反馈数</th><th>占比</th><th>代表句</th>
                    </tr>
                """
        for s in cluster_stats:
            stats_table += f"""
                    <tr>
                        <td>{s['cluster_id']}</td>
                        <td><strong>{s['keyword']}</strong></td>
                        <td>{s['size']}</td>
                        <td>{s['ratio']:.1%}</td>
                        <td style="text-align:left; max-width:300px;">{s['rep_sentence'][:60]}...</td>
                    </tr>
                    """
        stats_table += "</table>"

        # 5. 最终 HTML 输出
        html = f"""
                <div style="font-family: 'Microsoft YaHei', sans-serif; max-width: 1000px; margin: 0 auto; padding: 20px;">
                    <h1 style="text-align:center; color:#1e88e5;">EGISInsight</h1>
                    <p style="text-align:center; color:#555; font-size:16px;">
                        GIS 教学智能体 · 循证教学优化
                    </p>
                    <hr style="border: 1px solid #eee; margin: 30px 0;">

                    <h2 style="color:#1976d2;">1. 实验报告反馈</h2>
                    <img src="data:image/png;base64,{sankey_b64}" 
                         style="width:100%; border-radius:8px; box-shadow: 0 4px 12px rgba(0,0,0,0.1);">

                    <h2 style="color:#388e3c; margin-top:40px;">2. 学生反馈聚类</h2>
                    <img src="data:image/png;base64,{cluster_b64}" 
                         style="width:100%; border-radius:8px; box-shadow: 0 4px 12px rgba(0,0,0,0.1);">

                    <div style="margin-top:30px;">
                        {stats_table}
                    </div>

                    <div style="margin-top:30px; padding:20px; background:#f8f9fa; border-radius:8px; text-align:left;">
                        {advice}
                    </div>

                    <p style="text-align:center; color:#999; font-size:13px; margin-top:40px;">
                        EGISInsight © 2025 | 从数据到教学内容改革
                    </p>
                </div>
                """
        return html, None

    except Exception as e:
        return f"分析失败:{str(e)}", None


# ================== Gradio UI ==================
with gr.Blocks(title="教学智能体 · 实验报告分析") as demo:
    gr.Markdown("# GIS实验报告智能分析系统")
    gr.Markdown("**上传融合后的学生反馈 Excel → 一键生成教学决策图**")

    with gr.Row():
        file_input = gr.File(
            label="上传 ex02.xlsx(含 s1-s4 列)",
            file_types=[".xlsx"]
        )

    with gr.Row():
        output = gr.HTML(label="分析结果")

    file_input.change(analyze_report, inputs=file_input, outputs=output)

    gr.Markdown("---")
    gr.Markdown("**后续将接入通义千问大模型,自动生成教案、微课脚本、作业设计**")

# ================== 启动 ==================
if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        # server_port=7860,
        share=False  # 改 True 可生成公网链接
        # share=True  # 改 True 可生成公网链接
    )