Views
No views yet

1conda create -n Lang2Act python=3.10
2conda activate Lang2Act
3git clone https://github.com/NEUIR/Lang2Act.git
4cd Lang2Act
5pip install -r requirements.txtNote: If you do not want to train the model from scratch, you may skip this section and directly proceed to Inference and Evaluation
bash scripts/run_actionrl.shbash scripts/run_toolsrl.shbash scripts/predict.shNote: The inference script uses a trained Lang2Act checkpoint and automatically downloads the required test datasets from Hugging Face.
export SILICONFLOW_API_KEY="your_actual_api_key"bash scripts/eval.sh1from transformers import AutoProcessor
2from vllm import LLM, SamplingParams
3from qwen_vl_utils import process_vision_info
4
5Lang2Act_PROMPT_TEMPLATE = (
6 "You are a specialized AI assistant for visual question answering.\n"
7 "Your task is to answer the user's question by analyzing the provided images.\n\n"
8 "Your response must strictly follow this XML format:\n"
9 "<think>...</think>\n"
10 "<description>...</description>\n"
11 "<answer>...</answer>\n\n"
12 "Guidance for each tag:\n"
13 "1. `<think>`: Analyze all {num_images} images and state which image(s) are relevant to the question.\n"
14 "2. `<description>`: Focusing *only* on the selected image(s), describe your evidence-gathering steps using the tools below.\n"
15 "3. `<answer>`: Provide only the final, concise answer.\n\n"
16 "Available Tools for `<description>`:\n"
17 " - `<tool name=\"locate_visual_element\" args=\"Image k: structural hint/description\">Locate specific visual elements or regions.</tool>`\n"
18 " - `<tool name=\"read_text_element\" args=\"Image k: locator/region\">Read and transcribe visible text.</tool>`\n"
19 " - `<tool name=\"read_numeric_value\" args=\"Image k: data point/visual element\">Extract numeric values.</tool>`\n"
20 " - `<tool name=\"identify_entity_attribute\" args=\"Image k: entity\">Identify entity attributes.</tool>`\n"
21 " - `<tool name=\"compare_values\" args=\"Image k: value A vs value B\">Compare values.</tool>`\n"
22 " - `<tool name=\"compute_percentage\" args=\"part_value, total_value\">Compute percentages.</tool>`\n"
23 " - `<tool name=\"infer_missing_information\" args=\"Image k: existing data\">Infer missing information.</tool>`\n\n"
24)
25
26model_path = "xiongyq/Lang2Act-7B"
27
28processor = AutoProcessor.from_pretrained(
29 model_path,
30 trust_remote_code=True,
31 padding_side="left"
32)
33
34imgs = ["imgpath1", "imgpath2", "...", "imgpathN"]
35query = "What xxx?"
36input_prompt = Lang2Act_PROMPT_TEMPLATE.format(num_images=len(imgs)) + f"\nQuestion: {query}"
37
38content = [{"type": "text", "text": input_prompt}]
39for img_path in imgs:
40 content.append({
41 "type": "image",
42 "image": img_path
43 })
44
45messages = [{
46 "role": "user",
47 "content": content,
48}]
49
50llm = LLM(
51 model=model_path,
52 tensor_parallel_size=1,
53 dtype="bfloat16",
54 limit_mm_per_prompt={"image": 5, "video": 0},
55)
56
57sampling_params = SamplingParams(
58 temperature=0.1,
59 repetition_penalty=1.05,
60 max_tokens=2048,
61)
62
63prompt = processor.apply_chat_template(
64 messages,
65 tokenize=False,
66 add_generation_prompt=True,
67)
68
69image_inputs, _ = process_vision_info(messages)
70
71inputs = [{
72 "prompt": prompt,
73 "multi_modal_data": {"image": image_inputs},
74}]
75
76outputs = llm.generate(
77 inputs,
78 sampling_params=sampling_params,
79)
80
81print(outputs[0].outputs[0].text)
82yqxiong54@gmail.com