Ab00D/Arabic_elmostawsf_image_text
Viewer • Updated • 1.53k • 4 • 1
How to use Ab00D/Arabic_ElMostawsaf with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("image-text-to-text", model="Ab00D/Arabic_ElMostawsaf")
messages = [
{
"role": "user",
"content": [
{"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"},
{"type": "text", "text": "What animal is on the candy?"}
]
},
]
pipe(text=messages) # pip install -U transformers accelerate
# Load model directly
from transformers import AutoProcessor, AutoModelForMultimodalLM
processor = AutoProcessor.from_pretrained("Ab00D/Arabic_ElMostawsaf")
model = AutoModelForMultimodalLM.from_pretrained("Ab00D/Arabic_ElMostawsaf", device_map="auto")
messages = [
{
"role": "user",
"content": [
{"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"},
{"type": "text", "text": "What animal is on the candy?"}
]
},
]
inputs = processor.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
).to(model.device)
outputs = model.generate(**inputs, max_new_tokens=256)
print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:]))How to use Ab00D/Arabic_ElMostawsaf with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "Ab00D/Arabic_ElMostawsaf"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "Ab00D/Arabic_ElMostawsaf",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe this image in one sentence."
},
{
"type": "image_url",
"image_url": {
"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
}
}
]
}
]
}'docker model run hf.co/Ab00D/Arabic_ElMostawsaf
How to use Ab00D/Arabic_ElMostawsaf with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "Ab00D/Arabic_ElMostawsaf" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "Ab00D/Arabic_ElMostawsaf",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe this image in one sentence."
},
{
"type": "image_url",
"image_url": {
"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
}
}
]
}
]
}'docker run --gpus all \
--shm-size 32g \
-p 30000:30000 \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--env "HF_TOKEN=<secret>" \
--ipc=host \
lmsysorg/sglang:latest \
python3 -m sglang.launch_server \
--model-path "Ab00D/Arabic_ElMostawsaf" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "Ab00D/Arabic_ElMostawsaf",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe this image in one sentence."
},
{
"type": "image_url",
"image_url": {
"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
}
}
]
}
]
}'How to use Ab00D/Arabic_ElMostawsaf with Docker Model Runner:
docker model run hf.co/Ab00D/Arabic_ElMostawsaf
This model is a fine-tuned version of google/medgemma-4b-it. It has been trained using TRL.
import torch
from PIL import Image
import requests
from transformers import AutoModelForImageTextToText, AutoProcessor
import os
# Disable torch.compile to avoid the "Unsupported: generator" error
torch._dynamo.config.disable = True
# --- Configuration ---
# Use the model
MODEL_PATH = "Ab00D/Arabic_ElMostawsaf"
# Automatically set device and data type
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# Use bfloat16 if supported (on Ampere GPUs like A100), otherwise float16
DTYPE = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16
print(f"Using device: {DEVICE}")
print(f"Using dtype: {DTYPE}")
# --- Load Model & Processor ---
model = AutoModelForImageTextToText.from_pretrained(
MODEL_PATH,
torch_dtype=DTYPE,
device_map="auto", # Automatically handle model placement on devices
trust_remote_code=True # Add this if needed for custom model code
)
processor = AutoProcessor.from_pretrained(MODEL_PATH, trust_remote_code=True)
tokenizer = processor.tokenizer
# --- Prepare Image and Prompt ---
# Load your image
image = Image.open("Image Path").convert("RGB")
# The prompt for the model
user_prompt = "Analyze this medical image and provide step-by-step findings."
# --- Create Chat Template ---
chat = [
{
"role": "user",
"content": [
{"type": "image"},
{"type": "text", "text": user_prompt}
],
}
]
formatted_prompt = processor.apply_chat_template(chat, add_generation_prompt=True, tokenize=False)
# --- Run Inference ---
# Process the text and image together
inputs = processor(text=formatted_prompt, images=image, return_tensors="pt").to(DEVICE)
# Move inputs to correct dtype if needed
if hasattr(inputs, 'pixel_values') and inputs.pixel_values is not None:
inputs.pixel_values = inputs.pixel_values.to(dtype=DTYPE)
input_ids_len = inputs["input_ids"].shape[-1]
# Generate a response from the model with additional safeguards
with torch.inference_mode():
try:
output_ids = model.generate(
**inputs,
max_new_tokens=200,
use_cache=True,
do_sample=False, # Use greedy decoding for more stable results
pad_token_id=tokenizer.eos_token_id, # Explicitly set pad token
temperature=0.7, # Add temperature control
top_p=0.9, # Add nucleus sampling
)
except Exception as e:
print(f"Error during generation: {e}")
print("Trying with simplified generation parameters...")
output_ids = model.generate(
input_ids=inputs["input_ids"],
pixel_values=inputs.get("pixel_values"),
max_new_tokens=200,
pad_token_id=tokenizer.eos_token_id,
)
# Decode the generated tokens to text, skipping the prompt
response = processor.decode(output_ids[0, input_ids_len:], skip_special_tokens=True)
# --- Output ---
print("\n📌 Model Prediction:")
print(response)
This model was trained with SFT.
Cite TRL as:
@misc{vonwerra2022trl,
title = {{TRL: Transformer Reinforcement Learning}},
author = {Leandro von Werra and Younes Belkada and Lewis Tunstall and Edward Beeching and Tristan Thrush and Nathan Lambert and Shengyi Huang and Kashif Rasul and Quentin Gallou{\'e}dec},
year = 2020,
journal = {GitHub repository},
publisher = {GitHub},
howpublished = {\url{https://github.com/huggingface/trl}}
}