Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -74,15 +74,9 @@ import torch
|
|
| 74 |
from PIL import Image
|
| 75 |
|
| 76 |
# Load small LLaVA model
|
| 77 |
-
processor = AutoProcessor.from_pretrained("
|
| 78 |
-
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 79 |
-
import torch
|
| 80 |
-
from PIL import Image
|
| 81 |
-
|
| 82 |
-
# Load small LLaVA model
|
| 83 |
-
processor = AutoProcessor.from_pretrained("LLaVA/LLaVA-7B-llm-small")
|
| 84 |
model = AutoModelForCausalLM.from_pretrained(
|
| 85 |
-
"
|
| 86 |
torch_dtype=torch.float16,
|
| 87 |
device_map="auto" # Automatically use GPU if available
|
| 88 |
)
|
|
@@ -111,36 +105,6 @@ interface = gr.Interface(
|
|
| 111 |
)
|
| 112 |
|
| 113 |
interface.launch()
|
| 114 |
-
")
|
| 115 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 116 |
-
"LLaVA/LLaVA-7B-llm-small",
|
| 117 |
-
torch_dtype=torch.float16,
|
| 118 |
-
device_map="auto" # Automatically use GPU if available
|
| 119 |
-
)
|
| 120 |
|
| 121 |
-
def generate_caption(image):
|
| 122 |
-
# Convert to PIL if needed
|
| 123 |
-
if isinstance(image, str):
|
| 124 |
-
image = Image.open(image).convert("RGB")
|
| 125 |
-
|
| 126 |
-
# Prepare inputs
|
| 127 |
-
inputs = processor(images=image, return_tensors="pt").to(model.device)
|
| 128 |
-
|
| 129 |
-
# Generate output
|
| 130 |
-
outputs = model.generate(**inputs, max_new_tokens=50)
|
| 131 |
-
|
| 132 |
-
# Decode result
|
| 133 |
-
caption = processor.decode(outputs[0], skip_special_tokens=True)
|
| 134 |
-
return caption
|
| 135 |
-
|
| 136 |
-
# Gradio Interface
|
| 137 |
-
interface = gr.Interface(
|
| 138 |
-
fn=generate_caption,
|
| 139 |
-
inputs=gr.Image(type="pil"),
|
| 140 |
-
outputs=gr.Textbox(label="Generated Caption"),
|
| 141 |
-
title="LLaVA Image Captioning"
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
interface.launch()
|
| 145 |
|
| 146 |
|
|
|
|
| 74 |
from PIL import Image
|
| 75 |
|
| 76 |
# Load small LLaVA model
|
| 77 |
+
processor = AutoProcessor.from_pretrained("llava/LLaVA-7B-llm-small")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
model = AutoModelForCausalLM.from_pretrained(
|
| 79 |
+
"llava/LLaVA-7B-llm-small",
|
| 80 |
torch_dtype=torch.float16,
|
| 81 |
device_map="auto" # Automatically use GPU if available
|
| 82 |
)
|
|
|
|
| 105 |
)
|
| 106 |
|
| 107 |
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
|