Spaces:
Sleeping
Sleeping
File size: 1,801 Bytes
16e678c 69350e3 16e678c c72a8a2 16e678c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import transformers, accelerate
import requests
print(accelerate.__version__)
print(transformers.__version__)
# Image Captioning
from transformers import AutoProcessor
from transformers import AutoModelForCausalLM
import torch
import streamlit as st
device = "cuda" if torch.cuda.is_available() else "cpu" # Set device to GPU if its available
checkpoint = "microsoft/git-base"
processor = AutoProcessor.from_pretrained(checkpoint) # We would load a tokenizer for language. Here we load a processor to process images
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
# Text Search
st.title("Flower Type Demo")
st.subheader("Upload an image and See how Chinese qisper works")
upload_file = st.file_uploader('Upload an Image')
from PIL import Image
if upload_file:
test_sample = Image.open(upload_file)
inputs = processor(images=image, return_tensors="pt").to(device)
pixel_values = inputs.pixel_values.to(device)
generated_ids = model.generate(pixel_values=pixel_values, max_length=50)
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
for i in range(10):
st.write('New Caption is :')
st.write(generated_caption)
image = pipe(generated_caption).images[0]
display(image)
print("Model Loading + Inference time = " + str(time.time() - t1) + " seconds")
st.write("Showing the Image")
st.image (image, caption=name, width=None, use_column_width=None, clamp=False, channels='RGB', output_format='auto')
inputs = processor(images=image, return_tensors="pt").to(device)
pixel_values = inputs.pixel_values.to(device)
generated_ids = model.generate(pixel_values=pixel_values, max_length=50)
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] |