|
import streamlit as st |
|
from transformers import PaliGemmaProcessor, PaliGemmaForConditionalGeneration |
|
|
|
|
|
if "hf_token" not in st.session_state: |
|
st.title("Authentication Required") |
|
st.write("Please authenticate with Hugging Face using the following token:") |
|
hf_token = st.text_input("Enter your token", type="password") |
|
|
|
if hf_token == "": |
|
st.stop() |
|
else: |
|
hf_token = st.session_state.hf_token |
|
|
|
|
|
if "hf_token_local" not in st.session_state: |
|
st.title("Load Token from Storage") |
|
st.write("Please load your token from storage (e.g., environment variable, file)") |
|
hf_token_local = st.text_input("Enter your token", type="password") |
|
|
|
if hf_token_local == "": |
|
st.stop() |
|
else: |
|
hf_token_local = st.session_state.hf_token_local |
|
|
|
|
|
if hf_token or hf_token_local: |
|
processor = PaliGemmaProcessor.from_pretrained( |
|
"google/paligemma2", |
|
token=hf_token, |
|
local_file_dir="/tmp/", |
|
) |
|
model = PaliGemmaForConditionalGeneration.from_pretrained( |
|
"google/paligemma2", |
|
token=hf_token, |
|
local_file_dir="/tmp/", |
|
) |
|
|
|
|
|
else: |
|
st.title("No Token Found") |
|
st.write("Please authenticate with Hugging Face or load token from storage") |
|
|
|
|
|
def main(): |
|
if "output" not in st.session_state: |
|
st.write("Model output") |
|
else: |
|
st.write(st.session_state.output) |
|
|
|
|
|
if st.button("Generate Text"): |
|
input_text = st.text_input("Input text") |
|
if input_text: |
|
output = model.generate(input_text, max_length=50) |
|
st.session_state.output = output |
|
|
|
if __name__ == "__main__": |
|
main() |