Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,59 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def main():
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
st.
|
| 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()
|