Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from transformers import AutoProcessor, AutoModelForPreTraining
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import cv2
|
| 7 |
+
import requests
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
import google.generativeai as genai
|
| 10 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 11 |
+
import os
|
| 12 |
+
import pandas as pd
|
| 13 |
+
from huggingface_hub import login
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
processor = AutoProcessor.from_pretrained("google/paligemma-3b-pt-224")
|
| 17 |
+
model = AutoModelForPreTraining.from_pretrained("google/paligemma-3b-pt-224")
|
| 18 |
+
|
| 19 |
+
st.title("Image segmentation and object analysis")
|
| 20 |
+
uploaded_file = st.file_uploader("Choose an image")
|
| 21 |
+
|
| 22 |
+
if uploaded_file is not None:
|
| 23 |
+
image_data = uploaded_file.read()
|
| 24 |
+
st.image(image_data)
|
| 25 |
+
st.write("file uploaded")
|
| 26 |
+
image = Image.open(uploaded_file)
|
| 27 |
+
# Specify the file path to save the image
|
| 28 |
+
filepath = "./uploaded_image.jpg"
|
| 29 |
+
# Save the image
|
| 30 |
+
image.save(filepath)
|
| 31 |
+
st.success(f"Image saved successfully at {filepath}")
|
| 32 |
+
prompt = "Describe the image content in detail."
|
| 33 |
+
|
| 34 |
+
# Preprocess the image and prompt using the processor
|
| 35 |
+
inputs = processor( text=prompt, images=image, return_tensors="pt")
|
| 36 |
+
|
| 37 |
+
# Pass the inputs to the model
|
| 38 |
+
outputs = model(**inputs)
|
| 39 |
+
# Assuming you have the output stored in a variable called `outputs`
|
| 40 |
+
generated_text = processor.decode(outputs.logits.argmax(dim=-1)[0], skip_special_tokens=True)
|
| 41 |
+
print(generated_text)
|
| 42 |
+
st.write(generated_text)
|