KalvinPhan commited on
Commit
3bae58d
·
verified ·
1 Parent(s): 0fc7f5e

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +58 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from PIL import Image
4
+ from transformers import AutoModel, AutoTokenizer
5
+
6
+ # =========================
7
+ # 1️⃣ LOAD MODEL
8
+ # =========================
9
+ model_name = "MathLLMs/MathCoder-VL-8B"
10
+
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
12
+ model = AutoModel.from_pretrained(
13
+ model_name,
14
+ torch_dtype=torch.float32,
15
+ low_cpu_mem_usage=True,
16
+ device_map="cpu",
17
+ trust_remote_code=True
18
+ )
19
+ model.eval()
20
+
21
+ # =========================
22
+ # 2️⃣ XỬ LÝ ẢNH + VĂN BẢN
23
+ # =========================
24
+ def solve_math(image, question):
25
+ if image is None and (not question or question.strip() == ""):
26
+ return "⚠️ Hãy tải ảnh hoặc nhập câu hỏi."
27
+
28
+ # Model multimodal — gọi forward tự động xử lý cả 2 modality
29
+ with torch.no_grad():
30
+ response, _ = model.chat(tokenizer, image=image, question=question)
31
+ return response
32
+
33
+
34
+ # =========================
35
+ # 3️⃣ GIAO DIỆN GRADIO
36
+ # =========================
37
+ with gr.Blocks(theme="soft") as demo:
38
+ gr.Markdown("## 🧮 MathCoder-VL-8B — Multimodal Reasoning (CPU Demo)")
39
+ gr.Markdown(
40
+ "Model có thể nhận **ảnh + câu hỏi toán học**, sau đó sinh lời giải bằng ngôn ngữ tự nhiên."
41
+ )
42
+
43
+ with gr.Row():
44
+ with gr.Column(scale=1):
45
+ image_input = gr.Image(type="pil", label="Tải ảnh toán học (optional)")
46
+ text_input = gr.Textbox(
47
+ label="Câu hỏi hoặc mô tả",
48
+ placeholder="Ví dụ: Hãy giải bài toán trong hình, hoặc nhập đề bài...",
49
+ lines=3
50
+ )
51
+ btn = gr.Button("💡 Giải toán")
52
+
53
+ with gr.Column(scale=1):
54
+ output = gr.Textbox(label="Lời giải", lines=15)
55
+
56
+ btn.click(fn=solve_math, inputs=[image_input, text_input], outputs=output)
57
+
58
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+
2
+ torch>=2.2.0
3
+ transformers>=4.40.0
4
+ accelerate
5
+ gradio>=4.0.0
6
+ Pillow