hruday96 commited on
Commit
9036c2a
1 Parent(s): 61e5dfd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -59
app.py CHANGED
@@ -1,59 +1,26 @@
1
- import streamlit as st # Don't forget to include `streamlit` in your `requirements.txt`
2
- from transformers import PaliGemmaProcessor, PaliGemmaForConditionalGeneration
3
-
4
- # Set up authentication
5
- if "hf_token" not in st.session_state:
6
- st.title("Authentication Required")
7
- st.write("Please authenticate with Hugging Face using the following token:")
8
- hf_token = st.text_input("Enter your token", type="password")
9
-
10
- if hf_token == "":
11
- st.stop()
12
- else:
13
- hf_token = st.session_state.hf_token
14
-
15
- # Load Token from Storage
16
- if "hf_token_local" not in st.session_state:
17
- st.title("Load Token from Storage")
18
- st.write("Please load your token from storage (e.g., environment variable, file)")
19
- hf_token_local = st.text_input("Enter your token", type="password")
20
-
21
- if hf_token_local == "":
22
- st.stop()
23
- else:
24
- hf_token_local = st.session_state.hf_token_local
25
-
26
- # Load Processor and Model
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