-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlx-batch.py
72 lines (59 loc) · 2.3 KB
/
mlx-batch.py
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
import glob
import os
import mlx.core as mx
from mlx_vlm import load, generate
from mlx_vlm.prompt_utils import apply_chat_template
from mlx_vlm.utils import load_config
# Load the model
# model_path = "mlx-community/Qwen2.5-VL-3B-Instruct-4bit"
model_path = "mlx-community/Qwen2-VL-2B-Instruct-bf16"
model, processor = load(model_path)
config = load_config(model_path)
# Prepare input
# image = ["http://images.cocodataset.org/val2017/000000039769.jpg"]
image_paths = glob.glob(os.path.join("imgs/q11/", "*.png"))
# # only first image
# image_path = image_paths[0]
# print(image_path)
# image = [image_paths[0]]
from pydantic import BaseModel, Field
UNIVERSITY_ID_LEN = 8
UNIVERSITY_ID_PATTERN = f"^[0-9]{{{UNIVERSITY_ID_LEN}}}$"
UNIVERSITY_ID_ALIAS = "ufid"
SECTION_NUMBER_PATTERN = r"^\d{5}$"
class QuizSubmissionSummary(BaseModel):
# student_first_name: str
# student_last_name: str
student_full_name: str = Field(
description="Full name of the student in the format First Last"
)
university_id: str = Field(
# try also literal list of UFIDs
# pattern=UNIVERSITY_ID_PATTERN,
alias=UNIVERSITY_ID_ALIAS,
description=f"{UNIVERSITY_ID_LEN}-digit {UNIVERSITY_ID_ALIAS.capitalize()} of the student. If missing, report an empty string",
)
section_number: str = Field(
# pattern=SECTION_NUMBER_PATTERN,
description="5-digit section number of the student. If missing, report an empty string",
)
prompt = f"""You are an expert at grading student quizzes in physics courses.
Please extract the information from the student's submission. Reply only with the requested JSON schema, nothing else. Do not overthink the problem.
Return the information in the following JSON schema:
{QuizSubmissionSummary.model_json_schema(by_alias=True)}
"""
# prompt = "What is the handwritten full name of the person in the image?"
formatted_prompt = apply_chat_template(
processor, config, prompt, num_images=1
)
# Apply chat template
for i in range(len(image_paths)):
image_path = image_paths[i]
# keep only those with "page-3" in the name
if "page-3" not in image_path:
continue
image = [image_path]
print(image_path)
# Generate output
output = generate(model, processor, formatted_prompt, image, verbose=True)
print(output)