nitishhrms
commited on
Commit
•
1ca801f
1
Parent(s):
aa97573
new space
Browse files- app.py +41 -0
- model_folder/pytorch_model.bin +3 -0
- model_folder/special_tokens_map.json +7 -0
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
3 |
+
from PIL import Image
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Step 1: Load the processor from Hugging Face
|
7 |
+
processor = AutoProcessor.from_pretrained("microsoft/git-large-textcaps")
|
8 |
+
|
9 |
+
# Step 2: Load the model architecture from Hugging Face
|
10 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/git-large-textcaps") # Load model structure
|
11 |
+
|
12 |
+
# Step 3: Load your custom PyTorch weights
|
13 |
+
custom_weights_path = "model_folder/pytorch_model.bin" # Path to your custom weights
|
14 |
+
model.load_state_dict(torch.load(custom_weights_path, map_location=torch.device("cpu"))) # Load custom weights
|
15 |
+
model.eval() # Set the model to evaluation mode
|
16 |
+
|
17 |
+
# Step 4: Define the caption generation function
|
18 |
+
def generate_caption(image):
|
19 |
+
# Convert the input image to PIL format (if necessary)
|
20 |
+
image = Image.fromarray(image)
|
21 |
+
|
22 |
+
# Preprocess the image using the processor
|
23 |
+
inputs = processor(images=image, return_tensors="pt")
|
24 |
+
pixel_values = inputs.pixel_values
|
25 |
+
|
26 |
+
# Generate caption
|
27 |
+
generated_ids = model.generate(pixel_values=pixel_values, max_length=50)
|
28 |
+
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
29 |
+
|
30 |
+
return generated_caption
|
31 |
+
|
32 |
+
# Step 5: Define the Gradio interface
|
33 |
+
interface = gr.Interface(
|
34 |
+
fn=generate_caption, # Function to process input
|
35 |
+
inputs=gr.Image(), # Input as image
|
36 |
+
outputs=gr.Textbox(), # Output as text
|
37 |
+
live=True # Enable live prediction
|
38 |
+
)
|
39 |
+
|
40 |
+
# Step 6: Launch the Gradio app
|
41 |
+
interface.launch()
|
model_folder/pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:74f4b3b944f2a3e17c46bf4a028fb4a652b267c12618ac74b8aca20e14919992
|
3 |
+
size 989827505
|
model_folder/special_tokens_map.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cls_token": "[CLS]",
|
3 |
+
"mask_token": "[MASK]",
|
4 |
+
"pad_token": "[PAD]",
|
5 |
+
"sep_token": "[SEP]",
|
6 |
+
"unk_token": "[UNK]"
|
7 |
+
}
|