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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -60
app.py CHANGED
@@ -1,63 +1,59 @@
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
27
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  def main():
29
- # Set page configuration
30
- st.set_page_config(page_title="Text Reading with PaliGemma2", layout="centered")
31
- st.title("Text Reading from Images using PaliGemma2")
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"])
44
-
45
- if uploaded_image is not None:
46
- # Display uploaded image
47
- image = Image.open(uploaded_image)
48
- st.image(image, caption="Uploaded Image", use_column_width=True)
49
-
50
- # Extract text button
51
- if st.button("Extract Text"):
52
- with st.spinner("Processing image..."):
53
- extracted_text = process_image(image, processor, model)
54
- st.success("Text extraction complete!")
55
- st.subheader("Extracted Text")
56
- st.write(extracted_text)
57
-
58
- # Footer
59
- st.markdown("---")
60
- st.markdown("**Built with [PaliGemma2](https://huggingface.co/google/paligemma2) and Streamlit**")
61
 
62
  if __name__ == "__main__":
63
- main()
 
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()