Add Gemini OCR and fix VRC log world handling
This commit is contained in:
@@ -26,18 +26,18 @@ def getVisionPipeline():
|
||||
|
||||
try:
|
||||
from transformers import AutoProcessor
|
||||
from transformers import AutoModelForImageTextToText
|
||||
from transformers import Gemma4ForConditionalGeneration
|
||||
import torch
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"vision dependencies are missing detail={e}")
|
||||
|
||||
log("INFO", f"Vision model initializing model={model_name}")
|
||||
processor = AutoProcessor.from_pretrained(model_name)
|
||||
model = AutoModelForImageTextToText.from_pretrained(
|
||||
model = Gemma4ForConditionalGeneration.from_pretrained(
|
||||
model_name,
|
||||
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
||||
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
||||
device_map="auto",
|
||||
)
|
||||
).eval()
|
||||
_vision_pipeline = (processor, model)
|
||||
return _vision_pipeline
|
||||
|
||||
@@ -55,6 +55,12 @@ def runVisionFromImage(image_path):
|
||||
|
||||
prompt = config["prompt"]
|
||||
messages = [
|
||||
{
|
||||
"role": "system",
|
||||
"content": [
|
||||
{"type": "text", "text": "You are a helpful vision-language assistant."},
|
||||
],
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
@@ -73,15 +79,23 @@ def runVisionFromImage(image_path):
|
||||
|
||||
try:
|
||||
image = Image.open(image_path).convert("RGB")
|
||||
inputs = processor(images=image, text=prompt, return_tensors="pt")
|
||||
inputs = {key: value.to(model.device) for key, value in inputs.items()}
|
||||
inputs = processor.apply_chat_template(
|
||||
messages,
|
||||
add_generation_prompt=True,
|
||||
tokenize=True,
|
||||
return_dict=True,
|
||||
return_tensors="pt",
|
||||
)
|
||||
inputs = inputs.to(model.device, dtype=model.dtype)
|
||||
with torch.no_grad():
|
||||
output = model.generate(
|
||||
**inputs,
|
||||
max_new_tokens=config["max_new_tokens"],
|
||||
temperature=config["temperature"],
|
||||
do_sample=config["temperature"] > 0,
|
||||
)
|
||||
text = processor.batch_decode(output, skip_special_tokens=True)[0].strip()
|
||||
input_len = inputs["input_ids"].shape[-1]
|
||||
generated = output[0][input_len:]
|
||||
text = processor.decode(generated, skip_special_tokens=True).strip()
|
||||
except Exception as e:
|
||||
log("ERROR", f"Vision inference failed detail={e}")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user