WACATW commited on
Commit
fffc332
·
verified ·
1 Parent(s): e7f311a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -10
app.py CHANGED
@@ -1,29 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import torch
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  from PIL import Image
5
 
6
  # ============================================================
7
- # 模型初始化(纯 CPU 环境,直接加载到内存)
8
  # ============================================================
9
  model_id = "vikhyatk/moondream2"
10
  revision = "2024-08-26"
11
 
12
  print("正在加载 Moondream2 模型...")
13
  tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
14
-
15
- # 注意:CPU 环境下我们使用 float32 精度,兼容性最好
16
  model = AutoModelForCausalLM.from_pretrained(
17
  model_id,
18
  trust_remote_code=True,
19
  revision=revision,
20
- torch_dtype=torch.float32,
21
- low_cpu_mem_usage=True # 降低 CPU 内存占用
22
  )
23
  print("模型加载完成。")
24
 
25
  # ============================================================
26
- # 推理函数(纯 CPU 推理)
27
  # ============================================================
28
  def answer_question(image: Image.Image, question: str):
29
  if image is None:
@@ -31,10 +52,8 @@ def answer_question(image: Image.Image, question: str):
31
  if not question or not question.strip():
32
  return "⚠️ 请输入你的问题。"
33
 
34
- # Moondream2 的标准问答流程
35
  enc_image = model.encode_image(image)
36
  answer = model.answer_question(enc_image, question, tokenizer)
37
-
38
  return answer
39
 
40
  # ============================================================
@@ -52,9 +71,13 @@ demo = gr.Interface(
52
  ],
53
  outputs=gr.Textbox(label="🤖 AI 回答", lines=8),
54
  title="📚 作业识图 AI (CPU 版)",
55
- description="上传作业照片,输入问题,AI 会尝试识别并给出答案。当前运行在 CPU basic 上,推理速度可能稍慢,请耐心等待。",
56
  theme=gr.themes.Soft(),
57
  )
58
 
59
  if __name__ == "__main__":
60
- demo.launch()
 
 
 
 
 
1
+ import os
2
+ os.environ.setdefault("SYSTEM", "spaces")
3
+
4
+ # ===== Monkey Patch: 修复 gradio_client 的 bool-schema Bug =====
5
+ import gradio_client.utils as _gcu
6
+
7
+ if hasattr(_gcu, "_json_schema_to_python_type"):
8
+ _orig = _gcu._json_schema_to_python_type
9
+ def _safe(schema, defs=None):
10
+ if isinstance(schema, bool):
11
+ return "Any"
12
+ return _orig(schema, defs)
13
+ _gcu._json_schema_to_python_type = _safe
14
+
15
+ if hasattr(_gcu, "get_type"):
16
+ _orig_get_type = _gcu.get_type
17
+ def _safe_get_type(schema):
18
+ if not isinstance(schema, dict):
19
+ return "Any"
20
+ return _orig_get_type(schema)
21
+ _gcu.get_type = _safe_get_type
22
+ # ===== Monkey Patch 结束 =====
23
+
24
  import gradio as gr
25
  import torch
26
  from transformers import AutoModelForCausalLM, AutoTokenizer
27
  from PIL import Image
28
 
29
  # ============================================================
30
+ # 模型初始化
31
  # ============================================================
32
  model_id = "vikhyatk/moondream2"
33
  revision = "2024-08-26"
34
 
35
  print("正在加载 Moondream2 模型...")
36
  tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
 
 
37
  model = AutoModelForCausalLM.from_pretrained(
38
  model_id,
39
  trust_remote_code=True,
40
  revision=revision,
41
+ torch_dtype=torch.float32,
42
+ low_cpu_mem_usage=True,
43
  )
44
  print("模型加载完成。")
45
 
46
  # ============================================================
47
+ # 推理函数
48
  # ============================================================
49
  def answer_question(image: Image.Image, question: str):
50
  if image is None:
 
52
  if not question or not question.strip():
53
  return "⚠️ 请输入你的问题。"
54
 
 
55
  enc_image = model.encode_image(image)
56
  answer = model.answer_question(enc_image, question, tokenizer)
 
57
  return answer
58
 
59
  # ============================================================
 
71
  ],
72
  outputs=gr.Textbox(label="🤖 AI 回答", lines=8),
73
  title="📚 作业识图 AI (CPU 版)",
74
+ description="上传作业照片,输入问题,AI 会尝试识别并给出答案。",
75
  theme=gr.themes.Soft(),
76
  )
77
 
78
  if __name__ == "__main__":
79
+ demo.launch(
80
+ server_name="0.0.0.0",
81
+ server_port=7860,
82
+ share=True,
83
+ )