Spaces:
Sleeping
Sleeping
Upload Streamlit.py
Browse files- Streamlit.py +107 -0
Streamlit.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# coding: utf-8
|
| 3 |
+
|
| 4 |
+
# In[5]:
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import torch
|
| 10 |
+
import requests
|
| 11 |
+
from transformers import BlipProcessor, BlipForQuestionAnswering,BlipImageProcessor, AutoProcessor
|
| 12 |
+
from transformers import BlipConfig
|
| 13 |
+
from datasets import load_dataset
|
| 14 |
+
from torch.utils.data import DataLoader
|
| 15 |
+
from tqdm.notebook import tqdm
|
| 16 |
+
|
| 17 |
+
import numpy as np
|
| 18 |
+
import matplotlib.pyplot as plt
|
| 19 |
+
from IPython.display import display
|
| 20 |
+
|
| 21 |
+
text_processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
| 22 |
+
image_processor = BlipImageProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
| 23 |
+
model = BlipForQuestionAnswering.from_pretrained(r"blip_model_v2_epo89" )
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def preprocess_image(image):
|
| 27 |
+
# Your image preprocessing logic here...
|
| 28 |
+
# Example: Resize image to 128x128 pixels
|
| 29 |
+
image = image.resize((128, 128))
|
| 30 |
+
image_encoding = image_processor(image,
|
| 31 |
+
do_resize=True,
|
| 32 |
+
size=(128, 128),
|
| 33 |
+
return_tensors="pt")
|
| 34 |
+
return image_encoding["pixel_values"][0]
|
| 35 |
+
|
| 36 |
+
def preprocess_text(text, max_length=32):
|
| 37 |
+
# Your text preprocessing logic here...
|
| 38 |
+
encoding = text_processor(
|
| 39 |
+
None,
|
| 40 |
+
text,
|
| 41 |
+
padding="max_length",
|
| 42 |
+
truncation=True,
|
| 43 |
+
max_length=max_length,
|
| 44 |
+
return_tensors="pt"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
for k, v in encoding.items():
|
| 48 |
+
encoding[k] = v.squeeze()
|
| 49 |
+
return encoding
|
| 50 |
+
|
| 51 |
+
def predict(image, question):
|
| 52 |
+
# Preprocess image
|
| 53 |
+
pixel_values = preprocess_image(image).unsqueeze(0)
|
| 54 |
+
|
| 55 |
+
# Preprocess text
|
| 56 |
+
encoding = preprocess_text(question)
|
| 57 |
+
|
| 58 |
+
# Print shapes for debugging
|
| 59 |
+
#print("Pixel Values Shape:", pixel_values.shape)
|
| 60 |
+
#print("Input IDs Shape:", encoding['input_ids'].unsqueeze(0).shape)
|
| 61 |
+
|
| 62 |
+
# Perform prediction using your model
|
| 63 |
+
# Example: Replace this with your actual prediction logic
|
| 64 |
+
model.eval()
|
| 65 |
+
outputs = model.generate(pixel_values=pixel_values, input_ids=encoding['input_ids'].unsqueeze(0))
|
| 66 |
+
|
| 67 |
+
prediction_result = text_processor.decode(outputs[0], skip_special_tokens=True)
|
| 68 |
+
|
| 69 |
+
return prediction_result
|
| 70 |
+
|
| 71 |
+
def main():
|
| 72 |
+
st.title("PathoAgent")
|
| 73 |
+
|
| 74 |
+
# Image upload
|
| 75 |
+
st.subheader("Upload Image")
|
| 76 |
+
uploaded_file = st.file_uploader("Choose a file", type=["jpg", "png", "jpeg"])
|
| 77 |
+
|
| 78 |
+
# Text input
|
| 79 |
+
st.subheader("Input Question")
|
| 80 |
+
text_input = st.text_area("Enter text here:")
|
| 81 |
+
|
| 82 |
+
# Display uploaded image
|
| 83 |
+
if uploaded_file is not None:
|
| 84 |
+
image = Image.open(uploaded_file).convert('RGB')
|
| 85 |
+
#resized_img = image.resize((10,10))
|
| 86 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# Predict button
|
| 91 |
+
if st.button("Predict"):
|
| 92 |
+
if uploaded_file is not None and text_input:
|
| 93 |
+
# Perform prediction
|
| 94 |
+
prediction_result = predict(image, text_input)
|
| 95 |
+
|
| 96 |
+
# Display input text
|
| 97 |
+
st.subheader("Input Question:")
|
| 98 |
+
st.write(text_input)
|
| 99 |
+
# Display prediction result
|
| 100 |
+
st.subheader("Prediction Result:")
|
| 101 |
+
st.write(prediction_result)
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
main()
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
# streamlit run Streamlit.py
|