Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,16 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from transformers import PaliGemmaProcessor, PaliGemmaForConditionalGeneration
|
3 |
-
from PIL import Image
|
4 |
-
import torch
|
5 |
import os
|
6 |
|
7 |
def load_model():
|
8 |
-
"""Load PaliGemma2 model and processor."""
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
11 |
return processor, model
|
12 |
|
13 |
def process_image(image, processor, model):
|
@@ -29,8 +32,12 @@ def main():
|
|
29 |
|
30 |
# Load model and processor
|
31 |
with st.spinner("Loading PaliGemma2 model... This may take a few moments."):
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
|
35 |
# User input: upload image
|
36 |
uploaded_image = st.file_uploader("Upload an image containing text", type=["png", "jpg", "jpeg"])
|
|
|
1 |
+
import streamlit as st # Don't forget to include `streamlit` in your `requirements.txt` file to ensure the app runs properly on Hugging Face Spaces.
|
2 |
+
from transformers import PaliGemmaProcessor, PaliGemmaForConditionalGeneration # Make sure that the Hugging Face `transformers` library version supports the `PaliGemma2` model. You may need to specify the version in `requirements.txt`.
|
3 |
+
from PIL import Image # Ensure the `pillow` library is included in your `requirements.txt`.
|
4 |
+
import torch # Since PyTorch is required for this app, specify the appropriate version of `torch` in `requirements.txt` based on compatibility with the model.
|
5 |
import os
|
6 |
|
7 |
def load_model():
|
8 |
+
"""Load PaliGemma2 model and processor with Hugging Face token."""
|
9 |
+
token = os.getenv("HUGGINGFACEHUB_API_TOKEN") # Retrieve token from environment variable
|
10 |
+
if not token:
|
11 |
+
raise ValueError("Hugging Face API token not found. Please set it in the environment variables.")
|
12 |
+
processor = PaliGemmaProcessor.from_pretrained("google/paligemma2", token=token)
|
13 |
+
model = PaliGemmaForConditionalGeneration.from_pretrained("google/paligemma2", token=token)
|
14 |
return processor, model
|
15 |
|
16 |
def process_image(image, processor, model):
|
|
|
32 |
|
33 |
# Load model and processor
|
34 |
with st.spinner("Loading PaliGemma2 model... This may take a few moments."):
|
35 |
+
try:
|
36 |
+
processor, model = load_model()
|
37 |
+
st.success("Model loaded successfully!")
|
38 |
+
except ValueError as e:
|
39 |
+
st.error(str(e))
|
40 |
+
st.stop()
|
41 |
|
42 |
# User input: upload image
|
43 |
uploaded_image = st.file_uploader("Upload an image containing text", type=["png", "jpg", "jpeg"])
|