Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoProcessor
|
| 5 |
+
import torch
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# Load the model and processor
|
| 9 |
+
model_name = "arjunanand13/Florence-enphase"
|
| 10 |
+
|
| 11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
print(device)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True).to(device)
|
| 14 |
+
|
| 15 |
+
processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
|
| 16 |
+
torch.cuda.empty_cache()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def predict(image, question):
|
| 20 |
+
|
| 21 |
+
encoding = processor(image, question, return_tensors="pt")
|
| 22 |
+
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model.generate(**encoding, max_length=256)
|
| 25 |
+
answer = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 26 |
+
return answer
|
| 27 |
+
|
| 28 |
+
def gradio_interface(image, question):
|
| 29 |
+
|
| 30 |
+
if image.mode != "RGB":
|
| 31 |
+
image = image.convert("RGB")
|
| 32 |
+
|
| 33 |
+
answer = predict(image, question)
|
| 34 |
+
return answer
|
| 35 |
+
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=gradio_interface,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 40 |
+
gr.Textbox(label="Enter your question")
|
| 41 |
+
],
|
| 42 |
+
outputs=gr.Textbox(label="Answer"),
|
| 43 |
+
title="Florence-enphase Leg lift classifier",
|
| 44 |
+
description="Upload an image and ask a question about it."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
iface.launch()
|