Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,19 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
| 3 |
-
import torch
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
-
# Load BLIP2 model and processor
|
| 7 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
| 9 |
-
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16
|
| 10 |
-
model.
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
inputs = processor(image, return_tensors="pt")
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
if __name__ == "__main__":
|
| 22 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
|
|
|
|
|
|
|
| 6 |
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
| 7 |
+
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16)
|
| 8 |
+
model.eval()
|
| 9 |
|
| 10 |
+
def predict(image):
|
| 11 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 12 |
+
with torch.no_grad():
|
| 13 |
+
generated_ids = model.generate(**inputs, max_new_tokens=50)
|
| 14 |
+
caption = processor.decode(generated_ids[0], skip_special_tokens=True)
|
| 15 |
+
return {"label": caption, "score": 0.8} # assumed high confidence
|
| 16 |
|
| 17 |
+
demo = gr.Interface(fn=predict, inputs="image", outputs="json")
|
| 18 |
+
demo.launch()
|
| 19 |
|
|
|
|
|
|