Hev832 commited on
Commit
1eff51b
·
verified ·
1 Parent(s): d11783a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -22
app.py CHANGED
@@ -1,33 +1,37 @@
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
 
1
  import gradio as gr
2
+ import tensorflow as tf
3
+ from tensorflow.keras.applications import ResNet50
4
+ from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
5
  from PIL import Image
6
+ import numpy as np
7
 
8
+ # Load pre-trained ResNet50 model + higher level layers
9
+ model = ResNet50(weights='imagenet')
 
 
10
 
11
+ def classify_image(img):
12
+ # Resize the image to 224x224 pixels (required input size for ResNet50)
13
+ img = img.resize((224, 224))
14
+ # Convert the image to array
15
+ img_array = np.array(img)
16
+ # Expand dimensions to match the shape required by the model
17
+ img_array = np.expand_dims(img_array, axis=0)
18
  # Preprocess the image
19
+ img_array = preprocess_input(img_array)
20
+ # Predict the classification
21
+ predictions = model.predict(img_array)
22
+ # Decode predictions into readable labels
23
+ decoded_predictions = decode_predictions(predictions, top=3)[0]
 
 
24
 
25
+ # Format the output
26
+ return {label: float(probability) for (_, label, probability) in decoded_predictions}
27
 
28
+ # Gradio interface
29
  iface = gr.Interface(
30
+ fn=classify_image, # Function to call for predictions
31
+ inputs=gr.Image(type="pil"), # Input is an image
32
+ outputs=gr.Label(num_top_classes=3), # Output is a label with top 3 predictions
33
+ title="Contextual Image Classification",
34
+ description="Upload an image, and the model will classify it based on the context."
35
  )
36
 
37
  # Launch the interface