mbwanaf commited on
Commit
38bc9e9
·
verified ·
1 Parent(s): 3fa1adc

update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -18
app.py CHANGED
@@ -1,13 +1,11 @@
1
  import os
2
 
3
- # Use /tmp for all runtime-related folders
4
  os.environ["STREAMLIT_HOME"] = "/tmp"
5
  os.environ["STREAMLIT_RUNTIME_METRICS_ENABLED"] = "false"
6
- os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
7
- os.environ["HF_HOME"] = "/tmp/huggingface" # safer replacement for TRANSFORMERS_CACHE
8
  os.environ["STREAMLIT_WATCHED_MODULES"] = ""
9
-
10
-
11
 
12
  import streamlit as st
13
  import torch
@@ -17,10 +15,10 @@ import random
17
  from PIL import Image
18
  from transformers import AutoTokenizer, AutoModel, ViTModel, ViTImageProcessor
19
 
20
- # CPU device only
21
  device = torch.device("cpu")
22
 
23
- # Define Swahili VQA Model
24
  class SwahiliVQAModel(torch.nn.Module):
25
  def __init__(self, num_answers):
26
  super().__init__()
@@ -43,20 +41,19 @@ class SwahiliVQAModel(torch.nn.Module):
43
  fused = self.fusion(combined)
44
  return self.classifier(fused)
45
 
46
- # Load label encoder
47
  le = joblib.load("Vit_3895_label_encoder_best.pkl")
48
-
49
- # Load model weights normally — no override
50
  model = SwahiliVQAModel(num_answers=len(le.classes_)).to(device)
 
 
51
  state_dict = torch.load("Vit_3895_best_model_epoch25.pth", map_location=device)
52
  model.load_state_dict(state_dict)
53
  model.eval()
54
 
55
- # Load tokenizer and processor
56
  tokenizer = AutoTokenizer.from_pretrained("benjamin/roberta-base-wechsel-swahili")
57
  vit_processor = ViTImageProcessor.from_pretrained('google/vit-base-patch16-224-in21k')
58
 
59
- # Streamlit UI
60
  st.set_page_config(page_title="Swahili VQA", layout="wide")
61
  st.title("🦜 Swahili Visual Question Answering (VQA)")
62
 
@@ -69,13 +66,12 @@ col1, col2 = st.columns([1, 2], gap="large")
69
 
70
  with col1:
71
  if uploaded_image:
72
- st.image(uploaded_image, caption="Picha Iliyopakiwa", use_container_width=True)
73
- st.markdown("<div style='margin-bottom: 25px;'></div>", unsafe_allow_html=True)
74
 
75
  with col2:
76
  st.markdown("<div style='padding-top: 15px;'>", unsafe_allow_html=True)
77
- question = st.text_input("💬Andika swali lako hapa:", key="question_input")
78
- submit_button = st.button("📩Tuma")
79
  st.markdown("</div>", unsafe_allow_html=True)
80
 
81
  if submit_button and uploaded_image and question:
@@ -100,10 +96,9 @@ with col2:
100
  ]
101
 
102
  results = sorted(results, key=lambda x: x["confidence"], reverse=True)
 
103
 
104
- st.subheader("Majibu Yanayowezekana:")
105
  max_confidence = max(result["confidence"] for result in results)
106
-
107
  for i, pred in enumerate(results):
108
  bar_width = (pred["confidence"] / max_confidence) * 70
109
  color = generate_random_color()
 
1
  import os
2
 
3
+ # Use /tmp for all cache & runtime folders (Hugging Face safe)
4
  os.environ["STREAMLIT_HOME"] = "/tmp"
5
  os.environ["STREAMLIT_RUNTIME_METRICS_ENABLED"] = "false"
 
 
6
  os.environ["STREAMLIT_WATCHED_MODULES"] = ""
7
+ os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
8
+ os.environ["HF_HOME"] = "/tmp/huggingface"
9
 
10
  import streamlit as st
11
  import torch
 
15
  from PIL import Image
16
  from transformers import AutoTokenizer, AutoModel, ViTModel, ViTImageProcessor
17
 
18
+ # Use CPU only
19
  device = torch.device("cpu")
20
 
21
+ # === Define Swahili VQA Model ===
22
  class SwahiliVQAModel(torch.nn.Module):
23
  def __init__(self, num_answers):
24
  super().__init__()
 
41
  fused = self.fusion(combined)
42
  return self.classifier(fused)
43
 
44
+ # === Load model and encoders ===
45
  le = joblib.load("Vit_3895_label_encoder_best.pkl")
 
 
46
  model = SwahiliVQAModel(num_answers=len(le.classes_)).to(device)
47
+
48
+ # Load full state dict (already trained classifier)
49
  state_dict = torch.load("Vit_3895_best_model_epoch25.pth", map_location=device)
50
  model.load_state_dict(state_dict)
51
  model.eval()
52
 
 
53
  tokenizer = AutoTokenizer.from_pretrained("benjamin/roberta-base-wechsel-swahili")
54
  vit_processor = ViTImageProcessor.from_pretrained('google/vit-base-patch16-224-in21k')
55
 
56
+ # === Streamlit App ===
57
  st.set_page_config(page_title="Swahili VQA", layout="wide")
58
  st.title("🦜 Swahili Visual Question Answering (VQA)")
59
 
 
66
 
67
  with col1:
68
  if uploaded_image:
69
+ st.image(uploaded_image, caption="Picha Iliyopakiwa")
 
70
 
71
  with col2:
72
  st.markdown("<div style='padding-top: 15px;'>", unsafe_allow_html=True)
73
+ question = st.text_input("💬 Andika swali lako hapa:")
74
+ submit_button = st.button("📩 Tuma")
75
  st.markdown("</div>", unsafe_allow_html=True)
76
 
77
  if submit_button and uploaded_image and question:
 
96
  ]
97
 
98
  results = sorted(results, key=lambda x: x["confidence"], reverse=True)
99
+ st.subheader("🔎 Majibu Yanayowezekana:")
100
 
 
101
  max_confidence = max(result["confidence"] for result in results)
 
102
  for i, pred in enumerate(results):
103
  bar_width = (pred["confidence"] / max_confidence) * 70
104
  color = generate_random_color()