wuff-mann commited on
Commit
b662a8d
·
verified ·
1 Parent(s): 431a319

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +159 -159
app.py CHANGED
@@ -1,160 +1,160 @@
1
- import os
2
- import time
3
- import gradio as gr
4
- from mann_engram_en.router import MANNEngramRouter
5
-
6
- # 强制缓存路径,防止每次重启重复下载大模型
7
- os.environ["HF_HOME"] = "/home/user/.cache/huggingface"
8
-
9
- # 全局引擎变量,确保只加载一次,避免内存溢出
10
- router_engine = None
11
-
12
- def init_engine():
13
- global router_engine
14
- if router_engine is None:
15
- print("Initializing Tensor Routing Engines (1.5B Local Reasoning + SiGLIP)...")
16
- # 采用 1.5B 模型:在免费 CPU 上平衡“高智商”与“低延迟”的最佳选择
17
- router_engine = MANNEngramRouter(
18
- ckpt_path="./weights/skew_model_v4full_en.pt",
19
- enable_local_intent=True,
20
- local_intent_model="Qwen/Qwen2.5-1.5B-Instruct"
21
- )
22
- return router_engine
23
-
24
- def process_input(query, files):
25
- """处理前端数据,调用 SDK 并记录性能指标"""
26
- if not query:
27
- return "⚠️ Please enter a clinical query.", "", "Error: Missing input.", []
28
-
29
- engine = init_engine()
30
-
31
- file_paths = []
32
- image_paths = []
33
-
34
- # 拆分上传的文件
35
- if files:
36
- for f in files:
37
- ext = f.name.lower().split('.')[-1]
38
- if ext in ['jpg', 'jpeg', 'png', 'bmp', 'webp']:
39
- image_paths.append(f.name)
40
- else:
41
- file_paths.append(f.name)
42
-
43
- # 启动性能探针
44
- start_time = time.time()
45
-
46
- # 调用核心 SDK
47
- results = engine.process_session(
48
- raw_chat_input=query,
49
- file_paths=file_paths,
50
- image_paths=image_paths,
51
- top_p=0.85
52
- )
53
-
54
- # 停止探针
55
- latency = time.time() - start_time
56
-
57
- # 提取返回值
58
- intent = results.get("core_query", "N/A")
59
- context = results.get("purified_context", "N/A")
60
-
61
- # 构建高信息密度的统计报告
62
- stats_dict = results.get("stats", {})
63
- orig_text = stats_dict.get("original_text_chunks", 0)
64
- ret_text = stats_dict.get("retained_text_chunks", 0)
65
- noise_filtered = orig_text - ret_text
66
-
67
- stats_report = (
68
- f"✅ Extraction Complete\n"
69
- f"---------------------------\n"
70
- f"📉 Noise Filtered: {noise_filtered} chunks\n"
71
- f"📄 Text Retained: {ret_text} / {orig_text} chunks\n"
72
- f"🖼️ Images Routed: {stats_dict.get('retained_images', 0)} / {stats_dict.get('original_images', 0)}\n"
73
- f"⏱️ CPU Latency: {latency:.2f} seconds"
74
- )
75
-
76
- # 获取精选核心图片
77
- routed_images = results.get("purified_images_pil", [])
78
-
79
- return intent, context, stats_report, routed_images
80
-
81
- # ==========================================
82
- # 极客风与专业度并存的 UI 架构
83
- # ==========================================
84
- with gr.Blocks(title="MANN-Engram Router", css=".gradio-container {max-width: 1200px !important;}") as demo:
85
-
86
- # --- 1. 顶部标语与架构说明 ---
87
- gr.Markdown(
88
- """
89
- # 🧠 MANN-Engram: Edge-Cloud Multimodal Semantic Router
90
- ### A Privacy-First, Zero-Hallucination Shield for Clinical Vision-Language Models.
91
- """
92
- )
93
-
94
- with gr.Accordion("⚙️ System Architecture & Capabilities (Click to expand)", open=False):
95
- gr.Markdown(
96
- """
97
- * **Edge-side Intent Extraction (Qwen 1.5B):** Runs 100% locally to parse messy, emotion-heavy patient inputs into clean medical vectors without leaking sensitive data to the cloud.
98
- * **Multimodal Tensor Routing (SiGLIP + MANN):** Projects clinical text and medical imaging (CT, MRI, X-ray) into a shared latent space. It calculates semantic distances to dynamically filter out irrelevant exams and noisy context.
99
- * **Zero-Hallucination Guarantee:** The router does *not* generate new medical facts. It acts purely as a strict extraction and filtering pipeline, preparing a pristine context window for downstream heavy-duty VLMs.
100
- """
101
- )
102
-
103
- gr.Markdown("---")
104
-
105
- # --- 2. 核心交互区 ---
106
- with gr.Row():
107
- # 左侧:输入控制台
108
- with gr.Column(scale=5):
109
- gr.Markdown("### 📥 Input Console")
110
- query_input = gr.Textbox(
111
- label="Messy Clinical Query & Complaints",
112
- lines=6,
113
- placeholder="Enter patient complaints, including irrelevant noise, emotions, and specific medical symptoms..."
114
- )
115
- file_input = gr.File(label="Upload Patient File Dump (Images & Docs)", file_count="multiple")
116
- submit_btn = gr.Button("🚀 Trigger Semantic Router", variant="primary", size="lg")
117
-
118
- # 右侧:输出仪表盘
119
- with gr.Column(scale=5):
120
- gr.Markdown("### 📤 Purified Output Dashboard")
121
- out_intent = gr.Textbox(label="🎯 Purified Core Intent", interactive=False)
122
- out_context = gr.Textbox(label="📄 Filtered High-Value Context", lines=4, interactive=False)
123
-
124
- with gr.Row():
125
- out_stats = gr.Textbox(label="📊 Compression Stats", interactive=False, lines=5)
126
-
127
- out_gallery = gr.Gallery(
128
- label="🔭 Latent Space Routed Images (Core Evidence)",
129
- columns=2,
130
- object_fit="contain",
131
- height="auto"
132
- )
133
-
134
- # --- 3. 一键演示用例 (极具产品感的细节) ---
135
- gr.Markdown("---")
136
- gr.Markdown("### 🧪 Quick Try: 'Hell-Mode' Clinical Scenarios")
137
- gr.Examples(
138
- examples=[
139
- [
140
- "Doctor, you have to help me. The food in this ward is absolutely terrible, it's giving me severe stomach aches and diarrhea (see my abdominal scan). Also, my legs have been cramping up lately when I walk to the bathroom, and I have this chronic smoker's cough that won't go away. But honestly, the reason I came to the ER yesterday is that my vision suddenly blurred, I had a seizure, and the left side of my body went numb. My family is threatening to sue the hospital if you don't look at my head scans immediately. Is the mass growing?",
141
- None
142
- ],
143
- [
144
- "I am so frustrated with the billing department, they charged me twice for my prenatal visits last year! Back then, I had a fetal MRI and a pelvic scan because of some complications. But please, look at my 3-year-old son now! He has been coughing violently for three days, his fever is 103F, and his breathing is super fast. I'm terrified it's pneumonia. The nurse took an X-ray of his chest an hour ago. Please tell me if his lungs are clear!",
145
- None
146
- ]
147
- ],
148
- inputs=[query_input, file_input],
149
- label="Click a scenario below to auto-fill the input (You will still need to upload the corresponding test images manually):"
150
- )
151
-
152
- # 绑定核心逻辑
153
- submit_btn.click(
154
- fn=process_input,
155
- inputs=[query_input, file_input],
156
- outputs=[out_intent, out_context, out_stats, out_gallery]
157
- )
158
-
159
- if __name__ == "__main__":
160
  demo.launch(theme=gr.themes.Soft())
 
1
+ import os
2
+ import time
3
+ import gradio as gr
4
+ from mann_engram_en.router import MANNEngramRouter
5
+
6
+ # 强制缓存路径,防止每次重启重复下载大模型
7
+ os.environ["HF_HOME"] = "/home/user/.cache/huggingface"
8
+
9
+ # 全局引擎变量,确保只加载一次,避免内存溢出
10
+ router_engine = None
11
+
12
+ def init_engine():
13
+ global router_engine
14
+ if router_engine is None:
15
+ print("Initializing Tensor Routing Engines (1.5B Local Reasoning + SiGLIP)...")
16
+ # 采用 1.5B 模型:在免费 CPU 上平衡“高智商”与“低延迟”的最佳选择
17
+ router_engine = MANNEngramRouter(
18
+ ckpt_path="./weights/skew_model_v4full_en.pt",
19
+ enable_local_intent=True,
20
+ local_intent_model="Qwen/Qwen2.5-1.5B-Instruct"
21
+ )
22
+ return router_engine
23
+
24
+ def process_input(query, files):
25
+ """处理前端数据,调用 SDK 并记录性能指标"""
26
+ if not query:
27
+ return "⚠️ Please enter a clinical query.", "", "Error: Missing input.", []
28
+
29
+ engine = init_engine()
30
+
31
+ file_paths = []
32
+ image_paths = []
33
+
34
+ # 拆分上传的文件
35
+ if files:
36
+ for f in files:
37
+ ext = f.name.lower().split('.')[-1]
38
+ if ext in ['jpg', 'jpeg', 'png', 'bmp', 'webp']:
39
+ image_paths.append(f.name)
40
+ else:
41
+ file_paths.append(f.name)
42
+
43
+ # 启动性能探针
44
+ start_time = time.time()
45
+
46
+ # 调用核心 SDK
47
+ results = engine.process_session(
48
+ raw_chat_input=query,
49
+ file_paths=file_paths,
50
+ image_paths=image_paths,
51
+ top_p=0.85
52
+ )
53
+
54
+ # 停止探针
55
+ latency = time.time() - start_time
56
+
57
+ # 提取返回值
58
+ intent = results.get("core_query", "N/A")
59
+ context = results.get("purified_context", "N/A")
60
+
61
+ # 构建高信息密度的统计报告
62
+ stats_dict = results.get("stats", {})
63
+ orig_text = stats_dict.get("original_text_chunks", 0)
64
+ ret_text = stats_dict.get("retained_text_chunks", 0)
65
+ noise_filtered = orig_text - ret_text
66
+
67
+ stats_report = (
68
+ f"✅ Extraction Complete\n"
69
+ f"---------------------------\n"
70
+ f"📉 Noise Filtered: {noise_filtered} chunks\n"
71
+ f"📄 Text Retained: {ret_text} / {orig_text} chunks\n"
72
+ f"🖼️ Images Routed: {stats_dict.get('retained_images', 0)} / {stats_dict.get('original_images', 0)}\n"
73
+ f"⏱️ CPU Latency: {latency:.2f} seconds"
74
+ )
75
+
76
+ # 获取精选核心图片
77
+ routed_images = results.get("purified_images_pil", [])
78
+
79
+ return intent, context, stats_report, routed_images
80
+
81
+ # ==========================================
82
+ # 极客风与专业度并存的 UI 架构
83
+ # ==========================================
84
+ with gr.Blocks(title="MANN-Engram Router") as demo:
85
+
86
+ # --- 1. 顶部标语与架构说明 ---
87
+ gr.Markdown(
88
+ """
89
+ # 🧠 MANN-Engram: Edge-Cloud Multimodal Semantic Router
90
+ ### A Privacy-First, Zero-Hallucination Shield for Clinical Vision-Language Models.
91
+ """
92
+ )
93
+
94
+ with gr.Accordion("⚙️ System Architecture & Capabilities (Click to expand)", open=False):
95
+ gr.Markdown(
96
+ """
97
+ * **Edge-side Intent Extraction (Qwen 1.5B):** Runs 100% locally to parse messy, emotion-heavy patient inputs into clean medical vectors without leaking sensitive data to the cloud.
98
+ * **Multimodal Tensor Routing (SiGLIP + MANN):** Projects clinical text and medical imaging (CT, MRI, X-ray) into a shared latent space. It calculates semantic distances to dynamically filter out irrelevant exams and noisy context.
99
+ * **Zero-Hallucination Guarantee:** The router does *not* generate new medical facts. It acts purely as a strict extraction and filtering pipeline, preparing a pristine context window for downstream heavy-duty VLMs.
100
+ """
101
+ )
102
+
103
+ gr.Markdown("---")
104
+
105
+ # --- 2. 核心交互区 ---
106
+ with gr.Row():
107
+ # 左侧:输入控制台
108
+ with gr.Column(scale=5):
109
+ gr.Markdown("### 📥 Input Console")
110
+ query_input = gr.Textbox(
111
+ label="Messy Clinical Query & Complaints",
112
+ lines=6,
113
+ placeholder="Enter patient complaints, including irrelevant noise, emotions, and specific medical symptoms..."
114
+ )
115
+ file_input = gr.File(label="Upload Patient File Dump (Images & Docs)", file_count="multiple")
116
+ submit_btn = gr.Button("🚀 Trigger Semantic Router", variant="primary", size="lg")
117
+
118
+ # 右侧:输出仪表盘
119
+ with gr.Column(scale=5):
120
+ gr.Markdown("### 📤 Purified Output Dashboard")
121
+ out_intent = gr.Textbox(label="🎯 Purified Core Intent", interactive=False)
122
+ out_context = gr.Textbox(label="📄 Filtered High-Value Context", lines=4, interactive=False)
123
+
124
+ with gr.Row():
125
+ out_stats = gr.Textbox(label="📊 Compression Stats", interactive=False, lines=5)
126
+
127
+ out_gallery = gr.Gallery(
128
+ label="🔭 Latent Space Routed Images (Core Evidence)",
129
+ columns=2,
130
+ object_fit="contain",
131
+ height="auto"
132
+ )
133
+
134
+ # --- 3. 一键演示用例 (极具产品感的细节) ---
135
+ gr.Markdown("---")
136
+ gr.Markdown("### 🧪 Quick Try: 'Hell-Mode' Clinical Scenarios")
137
+ gr.Examples(
138
+ examples=[
139
+ [
140
+ "Doctor, you have to help me. The food in this ward is absolutely terrible, it's giving me severe stomach aches and diarrhea (see my abdominal scan). Also, my legs have been cramping up lately when I walk to the bathroom, and I have this chronic smoker's cough that won't go away. But honestly, the reason I came to the ER yesterday is that my vision suddenly blurred, I had a seizure, and the left side of my body went numb. My family is threatening to sue the hospital if you don't look at my head scans immediately. Is the mass growing?",
141
+ None
142
+ ],
143
+ [
144
+ "I am so frustrated with the billing department, they charged me twice for my prenatal visits last year! Back then, I had a fetal MRI and a pelvic scan because of some complications. But please, look at my 3-year-old son now! He has been coughing violently for three days, his fever is 103F, and his breathing is super fast. I'm terrified it's pneumonia. The nurse took an X-ray of his chest an hour ago. Please tell me if his lungs are clear!",
145
+ None
146
+ ]
147
+ ],
148
+ inputs=[query_input, file_input],
149
+ label="Click a scenario below to auto-fill the input (You will still need to upload the corresponding test images manually):"
150
+ )
151
+
152
+ # 绑定核心逻辑
153
+ submit_btn.click(
154
+ fn=process_input,
155
+ inputs=[query_input, file_input],
156
+ outputs=[out_intent, out_context, out_stats, out_gallery]
157
+ )
158
+
159
+ if __name__ == "__main__":
160
  demo.launch(theme=gr.themes.Soft())