Views
No views yet
AIDC-AI/Ovis2.5-9B, adapted for a variety of multimodal tasks including:1curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
2sudo apt-get install git-lfs
3git-lfs clone https://huggingface.co/chio4696/Ovis-2.5-SFT-27331import torch
2import requests
3from PIL import Image
4from transformers import AutoModelForCausalLM
5import pandas as pd
6import os
7
8IMAGE_PATH = 'AI-Chellenge-Ovis2_5/data/image/'
9
10model = AutoModelForCausalLM.from_pretrained(
11 "Ovis-2.5-SFT-2733/converted/default",
12 torch_dtype=torch.bfloat16,
13 trust_remote_code=True
14).cuda()
15
16def inference_on_data(row):
17 image_path = ""
18 task = row['task']
19 question = row['question']
20 if task == 'captioning':
21 text = 'Generate a single, detailed, and objective descriptive paragraph for the given image. Each description must begin with the phrase "The image is..." or "The image shows...", followed by a structured analysis that moves from the main subject to its details, and then to the background elements. You must use positional language, such as "on the left" or "at the top of the cover" to clearly orient the reader. If any text is visible in the image, transcribe it exactly and describe its visual characteristics like color and style. Conclude the entire description with a sentence that summarizes the overall atmosphere of the image, using a phrase like "The overall mood of the image is...". Throughout the paragraph, maintain a strictly factual, declarative tone with specific, descriptive vocabulary, avoiding any personal opinions or interpretations.'
22 image_path = os.path.join(IMAGE_PATH, row['input'])
23 elif task == 'vqa':
24 text = f'Given a document image and a question, extract the precise answer. Your response must be only the literal text found in the image, with no extra words or explanation.\n\nQuestion: {question}'
25 image_path = os.path.join(IMAGE_PATH, row['input'])
26 elif task == 'summarization':
27 text = f"Generate a summary of the following legislative text. Start with the bill's official title, then state its primary purpose and key provisions. Use formal, objective language and focus on the actions the bill takes, such as what it amends, requires, prohibits, or establishes.\n\nText: {row['input']}"
28 elif task == 'text_qa':
29 text = f"Given a context and a question, extract the most concise, direct answer from the text. Your answer should be a short phrase, not a complete sentence.\n\nContext: {row['input']}\n\nQuestion: {question}"
30 elif task == 'math_reasoning':
31 text = f"Given a math word problem, solve the question by generating a step-by-step reasoning process. After detailing all the steps in your reasoning, you must conclude your response by placing the final numerical answer on its own separate line, prefixed with #### .\n\nQuestion: {row['input']}"
32 messages = [{
33 "role": "user",
34 "content": [
35 {"type": "image", "image": Image.open(image_path) if image_path else None},
36 {"type": "text", "text": text},
37 ],
38 }]
39
40 input_ids, pixel_values, grid_thws = model.preprocess_inputs(
41 messages=messages,
42 add_generation_prompt=True,
43 enable_thinking=True
44 )
45 input_ids = input_ids.cuda()
46 pixel_values = pixel_values.cuda() if pixel_values is not None else None
47 grid_thws = grid_thws.cuda() if grid_thws is not None else None
48
49 outputs = model.generate(
50 inputs=input_ids,
51 pixel_values=pixel_values,
52 grid_thws=grid_thws,
53 enable_thinking=True,
54 enable_thinking_budget=True,
55 max_new_tokens=8192,
56 thinking_budget=4096,
57 )
58
59 return model.text_tokenizer.decode(outputs[0], skip_special_tokens=True), text
60
61test_df = pd.read_parquet("AI-Chellenge-Ovis2_5/data/converted/deep_chal_multitask_dataset_test_path_converted.parquet") # Should be able to feed the function with local image path
62test_df = test_df.groupby('task').head(5) # Simple task-stratified samples
63
64inference_result = dict()
65for _, row in test_df.iterrows():
66 y, x = inference_on_data(row)
67 inference_result[x] = y
68
69inference_df = pd.DataFrame(inference_result.items(), columns=['In', 'Out'])