Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,26 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from transformers import PaliGemmaProcessor, PaliGemmaForConditionalGeneration
|
3 |
-
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
if
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
if hf_token or hf_token_local:
|
28 |
-
processor = PaliGemmaProcessor.from_pretrained(
|
29 |
-
"google/paligemma2",
|
30 |
-
token=hf_token,
|
31 |
-
local_file_dir="/tmp/",
|
32 |
-
)
|
33 |
-
model = PaliGemmaForConditionalGeneration.from_pretrained(
|
34 |
-
"google/paligemma2",
|
35 |
-
token=hf_token,
|
36 |
-
local_file_dir="/tmp/",
|
37 |
-
)
|
38 |
-
|
39 |
-
# Rest of your code
|
40 |
-
else:
|
41 |
-
st.title("No Token Found")
|
42 |
-
st.write("Please authenticate with Hugging Face or load token from storage")
|
43 |
-
|
44 |
-
# Use the model
|
45 |
-
def main():
|
46 |
-
if "output" not in st.session_state:
|
47 |
-
st.write("Model output")
|
48 |
-
else:
|
49 |
-
st.write(st.session_state.output)
|
50 |
-
|
51 |
-
# Add a button to generate text using the model
|
52 |
-
if st.button("Generate Text"):
|
53 |
-
input_text = st.text_input("Input text")
|
54 |
-
if input_text:
|
55 |
-
output = model.generate(input_text, max_length=50)
|
56 |
-
st.session_state.output = output
|
57 |
-
|
58 |
-
if __name__ == "__main__":
|
59 |
-
main()
|
|
|
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):
|
17 |
+
"""Extract text from image using PaliGemma2."""
|
18 |
+
# Preprocess the image
|
19 |
+
inputs = processor(images=image, return_tensors="pt")
|
20 |
+
|
21 |
+
# Generate predictions
|
22 |
+
with torch.no_grad():
|
23 |
+
generated_ids = model.generate(**inputs)
|
24 |
+
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
25 |
+
|
26 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|