ig_test / ig.py
far555na's picture
Update ig.py
ce2a0c5 verified
from transformers import AutoProcessor, AutoModelForCausalLM
from peft import PeftModel, PeftConfig
import gradio as gr
import torch
# Load the processor
processor = AutoProcessor.from_pretrained("microsoft/git-base")
# Load the base model (the pre-trained model you're adapting with LoRA)
base_model = AutoModelForCausalLM.from_pretrained("microsoft/git-base")
# Load the adapter configuration
adapter_config_path = "./" # Path to your adapter_config.json
adapter_model_path = "./" # Path to your adapter_model.safetensors
# Load the LoRA adapter using Peft
peft_config = PeftConfig.from_pretrained(adapter_config_path)
model = PeftModel.from_pretrained(base_model, adapter_model_path, config=peft_config)
def predict(image):
try:
# Prepare the image using the processor
inputs = processor(images=image, return_tensors="pt")
# Move inputs to the appropriate device
device = "cuda" if torch.cuda.is_available() else "cpu"
inputs = {key: value.to(device) for key, value in inputs.items()}
model.to(device)
# Generate the caption
outputs = model.generate(**inputs)
# Decode the generated caption
caption = processor.batch_decode(outputs, skip_special_tokens=True)[0]
return caption
except Exception as e:
print("Error during prediction:", str(e))
return "Error: " + str(e)
# Gradio Interface
with gr.Blocks() as demo:
image = gr.Image(type="pil")
predict_btn = gr.Button("Predict", variant="primary")
output = gr.Label(label="Generated Caption")
inputs = [image]
outputs = [output]
predict_btn.click(predict, inputs=inputs, outputs=outputs)
if __name__ == "__main__":
demo.launch() # Local machine only
# demo.launch(server_name="0.0.0.0") # LAN access to local machine
# demo.launch(share=True) # Public access to local machine