Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,42 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import gradio as gr
|
| 5 |
from PIL import Image
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# Decode the output to text
|
| 44 |
-
generated_text = processor.decode(output[0], skip_special_tokens=True)
|
| 45 |
-
return generated_text
|
| 46 |
-
|
| 47 |
-
# Step 6: Create the Gradio interface
|
| 48 |
-
interface = gr.Interface(
|
| 49 |
-
fn=predict,
|
| 50 |
-
inputs=[
|
| 51 |
-
gr.Textbox(label="Image (Base64)", placeholder="Enter base64 encoded image here...", lines=10), # Base64 input for image
|
| 52 |
-
gr.Textbox(label="Prompt", placeholder="Enter your prompt here...") # Prompt input
|
| 53 |
-
],
|
| 54 |
-
outputs="text", # Text output
|
| 55 |
-
title="Image and Prompt to Text Model",
|
| 56 |
-
description="Enter a base64 encoded image and a prompt to generate a descriptive text."
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
# Step 7: Launch the Gradio app
|
| 60 |
-
interface.launch()
|
|
|
|
| 1 |
+
from huggingface_hub import login
|
| 2 |
import os
|
| 3 |
+
from peft import PeftModel, PeftConfig
|
| 4 |
+
from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
+
import requests
|
| 7 |
+
import torch
|
| 8 |
+
import io
|
| 9 |
+
import base64
|
| 10 |
+
import cv2
|
| 11 |
+
|
| 12 |
+
access_token = os.environ["HF_TOKEN"]
|
| 13 |
+
login(token=access_token)
|
| 14 |
+
|
| 15 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
+
dtype = torch.bfloat16
|
| 17 |
+
|
| 18 |
+
config = PeftConfig.from_pretrained("anushettypsl/paligemma_vqav2")
|
| 19 |
+
# base_model = AutoModelForCausalLM.from_pretrained("google/paligemma-3b-pt-448")
|
| 20 |
+
base_model = PaliGemmaForConditionalGeneration.from_pretrained("google/paligemma-3b-pt-448")
|
| 21 |
+
model = PeftModel.from_pretrained(base_model, "anushettypsl/paligemma_vqav2", device_map=device)
|
| 22 |
+
processor = AutoProcessor.from_pretrained("google/paligemma-3b-pt-448", device_map=device)
|
| 23 |
+
model.to(device)
|
| 24 |
+
|
| 25 |
+
image = cv2.imread('/content/15_BC_G2_6358_40x_2_jpg.rf.97595fa4965f66ad45be8fd055331933.jpg')
|
| 26 |
+
|
| 27 |
+
# Convert the image to base64 encoding
|
| 28 |
+
image_bytes = cv2.imencode('.jpg', image)[1]
|
| 29 |
+
base64_string = base64.b64encode(image_bytes).decode('utf-8')
|
| 30 |
+
|
| 31 |
+
input_image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
|
| 32 |
+
|
| 33 |
+
model_inputs = processor(
|
| 34 |
+
text=input_text, images=input_image, return_tensors="pt").to(device)
|
| 35 |
+
input_len = model_inputs["input_ids"].shape[-1]
|
| 36 |
+
model.to(device)
|
| 37 |
+
with torch.inference_mode():
|
| 38 |
+
generation = model.generate(
|
| 39 |
+
**model_inputs, max_new_tokens=100, do_sample=False)
|
| 40 |
+
generation = generation[0][input_len:]
|
| 41 |
+
decoded = processor.decode(generation, skip_special_tokens=True)
|
| 42 |
+
print(decoded)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|