Edit model card
YAML Metadata Warning: empty or missing yaml metadata in repo card (https://huggingface.co/docs/hub/model-cards#model-card-metadata)

Qwen2-VL-7B-Instruct Fine-tuned on THFOOD-50

Qwen2-VL-7B-Instruct-Thaifood, was fine-tuned on the THFOOD-50 . with synthetic data generated by gemini-1.5-flash-8b-exp-0827

  • Base Model: Qwen2-VL-2B-Instruct
  • Dataset: THFOOD-50
  • Output: label, general_information, main_ingredients, guide (answer from question), precautions

Usage

You can use the following code to run inference:

from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
from qwen_vl_utils import process_vision_info
import requests
from PIL import Image
from io import BytesIO
import json

# Load the fine-tuned model
model = Qwen2VLForConditionalGeneration.from_pretrained(
    "TakdanaiHF/Qwen2-VL-7B-Instruct-Thaifood", torch_dtype="auto", device_map="auto"
)

min_pixels = 256*28*28
max_pixels = 1280*28*28
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)

# System prompt to guide the model
system_prompt = "As a Thai food expert, only respond in JSON format with the following structure (don't add any more key json): {'label': 'The label for the dish (case-sensitive and may vary in spelling)', 'general_information': 'A brief description of the dish', 'main_ingredients': ['List of main ingredients'], 'guide': 'Answer to the specific question', 'precautions': ['List of precautions']}. Respond only in JSON format."
image_url = 'https://i.ytimg.com/vi/vhjcNoS9Y1o/mqdefault.jpg' # Som tam example 
response = requests.get(image_url)
img = Image.open(BytesIO(response.content))

system_message = {
    "role": "system",
    "content": system_prompt
}

# User input: Image and question
messages = [
    system_message,  # Add system prompt here
    {
        "role": "user",
        "content": [
            {
                "type": "image",
                "image": img,  # image path
            },
            {"type": "text", "text": "What is this dish"},  # User input text
        ],
    }
]

# Prepare for inference
text = processor.apply_chat_template(
    messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
    text=[text],
    images=image_inputs,
    videos=video_inputs,
    padding=True,
    return_tensors="pt",
)

inputs = inputs.to("cuda")

# Generate the output
generated_ids = model.generate(**inputs, max_new_tokens = 1000)
generated_ids_trimmed = [
    out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
    generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)

print(output_text)

# access key
# parsed_data = json.loads(output_text[0])
# parsed_data
Downloads last month
3
Safetensors
Model size
8.29B params
Tensor type
BF16
·
Inference API
Unable to determine this model's library. Check the docs .