Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load the Microsoft Phi-3.5-mini-instruct model
|
7 |
+
model_name = "microsoft/phi-3.5-mini-instruct"
|
8 |
+
processor = BlipProcessor.from_pretrained(model_name)
|
9 |
+
model = BlipForConditionalGeneration.from_pretrained(model_name)
|
10 |
+
|
11 |
+
# Define the image classification function
|
12 |
+
def classify_image(image):
|
13 |
+
# Preprocess the image
|
14 |
+
inputs = processor(images=image, return_tensors="pt")
|
15 |
+
pixel_values = inputs["pixel_values"]
|
16 |
+
|
17 |
+
# Generate the classification
|
18 |
+
with torch.no_grad():
|
19 |
+
generated_ids = model.generate(pixel_values=pixel_values)
|
20 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
21 |
+
|
22 |
+
return generated_text
|
23 |
+
|
24 |
+
# Create a Gradio interface
|
25 |
+
iface = gr.Interface(
|
26 |
+
fn=classify_image,
|
27 |
+
inputs=gr.Image(type="pil"),
|
28 |
+
outputs=gr.Textbox(label="Image Classification"),
|
29 |
+
title="Image Context Classification",
|
30 |
+
description="Upload an image to classify its context using Microsoft's Phi-3.5-mini-instruct model."
|
31 |
+
)
|
32 |
+
|
33 |
+
# Launch the interface
|
34 |
+
iface.launch()
|