Updating the code for the Gemma 3n trial
Browse files- src/streamlit_app.py +37 -9
src/streamlit_app.py
CHANGED
|
@@ -3,11 +3,27 @@ from transformers import pipeline
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
# Set cache directory to avoid permission issues
|
| 8 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache"
|
| 9 |
os.environ["HF_HOME"] = "/tmp/hf_home"
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Set page config
|
| 12 |
st.set_page_config(
|
| 13 |
page_title="Gemma-3n E4B Vision-Language Model",
|
|
@@ -65,18 +81,30 @@ def main():
|
|
| 65 |
st.title("π€ Gemma-3n E4B Vision-Language Model")
|
| 66 |
st.markdown("Upload an image and ask questions about it!")
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# Check if user is authenticated
|
| 69 |
st.sidebar.markdown("### π Setup Instructions")
|
| 70 |
st.sidebar.markdown("""
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
3.
|
| 77 |
-
```bash
|
| 78 |
-
export HUGGINGFACE_HUB_TOKEN=your_token_here
|
| 79 |
-
```
|
| 80 |
""")
|
| 81 |
|
| 82 |
# Load model
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
import os
|
| 6 |
+
from huggingface_hub import login
|
| 7 |
|
| 8 |
# Set cache directory to avoid permission issues
|
| 9 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache"
|
| 10 |
os.environ["HF_HOME"] = "/tmp/hf_home"
|
| 11 |
|
| 12 |
+
# Authenticate with HuggingFace
|
| 13 |
+
def authenticate_hf():
|
| 14 |
+
"""Authenticate with HuggingFace using token from secrets"""
|
| 15 |
+
hf_token = os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 16 |
+
if hf_token:
|
| 17 |
+
try:
|
| 18 |
+
login(token=hf_token)
|
| 19 |
+
return True
|
| 20 |
+
except Exception as e:
|
| 21 |
+
st.error(f"Failed to authenticate with HuggingFace: {str(e)}")
|
| 22 |
+
return False
|
| 23 |
+
else:
|
| 24 |
+
st.error("HuggingFace token not found in environment variables")
|
| 25 |
+
return False
|
| 26 |
+
|
| 27 |
# Set page config
|
| 28 |
st.set_page_config(
|
| 29 |
page_title="Gemma-3n E4B Vision-Language Model",
|
|
|
|
| 81 |
st.title("π€ Gemma-3n E4B Vision-Language Model")
|
| 82 |
st.markdown("Upload an image and ask questions about it!")
|
| 83 |
|
| 84 |
+
# Check authentication first
|
| 85 |
+
if not authenticate_hf():
|
| 86 |
+
st.error("β Authentication failed. Please check your HuggingFace token in the Space secrets.")
|
| 87 |
+
st.markdown("""
|
| 88 |
+
**To fix this:**
|
| 89 |
+
1. Go to your Space settings (βοΈ icon)
|
| 90 |
+
2. Navigate to "Repository secrets"
|
| 91 |
+
3. Add a secret with name: `HF_TOKEN`
|
| 92 |
+
4. Value: Your HuggingFace token
|
| 93 |
+
5. Restart the Space
|
| 94 |
+
""")
|
| 95 |
+
return
|
| 96 |
+
|
| 97 |
+
st.success("β
Successfully authenticated with HuggingFace!")
|
| 98 |
+
|
| 99 |
# Check if user is authenticated
|
| 100 |
st.sidebar.markdown("### π Setup Instructions")
|
| 101 |
st.sidebar.markdown("""
|
| 102 |
+
β
**Authentication**: Configured via Space secrets
|
| 103 |
+
|
| 104 |
+
Make sure you have:
|
| 105 |
+
1. Access to the gated model
|
| 106 |
+
2. Added your HF token to Space secrets
|
| 107 |
+
3. Token has proper permissions
|
|
|
|
|
|
|
|
|
|
| 108 |
""")
|
| 109 |
|
| 110 |
# Load model
|