zakerytclarke commited on
Commit
c361481
·
verified ·
1 Parent(s): 698d5c2

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +7 -175
src/streamlit_app.py CHANGED
@@ -1,4 +1,10 @@
1
- # streamlit_app.py
 
 
 
 
 
 
2
 
3
  import streamlit as st
4
  from datasets import load_dataset
@@ -153,177 +159,3 @@ def train_ffnn(tokens, context_size=3, epochs=3):
153
  return model
154
 
155
  def ffnn_predict(model, context, temperature=1.0):
156
- x = torch.tensor([token_to_idx.get(tok, 0) for tok in context[-2:]], device=device).unsqueeze(0)
157
- with torch.no_grad():
158
- logits = model(x).squeeze()
159
- probs = torch.softmax(logits / temperature, dim=0).cpu().numpy()
160
- return np.random.choice(vocab, p=probs)
161
-
162
- ###################################
163
- # Decision Tree
164
- ###################################
165
-
166
- def train_dt(tokens, context_size=3):
167
- X, y = [], []
168
- for i in range(len(tokens) - context_size):
169
- context = tokens[i:i+context_size-1]
170
- target = tokens[i+context_size-1]
171
- X.append([token_to_idx[tok] for tok in context])
172
- y.append(token_to_idx[target])
173
-
174
- with st.spinner("Training Decision Tree..."):
175
- model = DecisionTreeClassifier()
176
- model.fit(X, y)
177
- return model
178
-
179
- def dt_predict(model, context):
180
- x = [token_to_idx.get(tok, 0) for tok in context[-2:]]
181
- pred = model.predict([x])[0]
182
- return idx_to_token[pred]
183
-
184
- ###################################
185
- # Gradient Boosted Tree
186
- ###################################
187
-
188
- def train_gbt(tokens, context_size=3):
189
- X, y = [], []
190
- for i in range(len(tokens) - context_size):
191
- context = tokens[i:i+context_size-1]
192
- target = tokens[i+context_size-1]
193
- X.append([token_to_idx[tok] for tok in context])
194
- y.append(token_to_idx[target])
195
-
196
- with st.spinner("Training Gradient Boosted Tree..."):
197
- model = GradientBoostingClassifier()
198
- model.fit(X, y)
199
- return model
200
-
201
- def gbt_predict(model, context):
202
- x = [token_to_idx.get(tok, 0) for tok in context[-2:]]
203
- pred = model.predict([x])[0]
204
- return idx_to_token[pred]
205
-
206
- ###################################
207
- # RNN
208
- ###################################
209
-
210
- class RNNModel(nn.Module):
211
- def __init__(self, vocab_size, embed_size=64, hidden_size=128):
212
- super().__init__()
213
- self.embed = nn.Embedding(vocab_size, embed_size)
214
- self.rnn = nn.RNN(embed_size, hidden_size, batch_first=True)
215
- self.fc = nn.Linear(hidden_size, vocab_size)
216
-
217
- def forward(self, x, h=None):
218
- x = self.embed(x)
219
- out, h = self.rnn(x, h)
220
- out = self.fc(out[:, -1, :])
221
- return out, h
222
-
223
- def train_rnn(tokens, context_size=3, epochs=3):
224
- data = []
225
- for i in range(len(tokens) - context_size):
226
- context = tokens[i:i+context_size-1]
227
- target = tokens[i+context_size-1]
228
- data.append((
229
- torch.tensor([token_to_idx[tok] for tok in context], device=device),
230
- token_to_idx[target]
231
- ))
232
-
233
- model = RNNModel(len(vocab)).to(device)
234
- optimizer = optim.Adam(model.parameters(), lr=0.01)
235
- criterion = nn.CrossEntropyLoss()
236
-
237
- progress_bar = st.progress(0)
238
- total_steps = epochs * len(data)
239
- step = 0
240
-
241
- for epoch in range(epochs):
242
- total_loss = 0
243
- h = None
244
- for x, y in data:
245
- x = x.unsqueeze(0)
246
- y = torch.tensor([y], device=device)
247
- out, h = model(x, h)
248
- loss = criterion(out, y)
249
- optimizer.zero_grad()
250
- loss.backward()
251
- optimizer.step()
252
- total_loss += loss.item()
253
-
254
- step += 1
255
- progress_bar.progress(step / total_steps)
256
-
257
- st.write(f"Epoch {epoch+1}, Loss: {total_loss:.4f}")
258
-
259
- progress_bar.empty()
260
- return model
261
-
262
- def rnn_predict(model, context, temperature=1.0):
263
- x = torch.tensor([token_to_idx.get(tok, 0) for tok in context[-2:]], device=device).unsqueeze(0)
264
- with torch.no_grad():
265
- logits, _ = model(x)
266
- probs = torch.softmax(logits.squeeze() / temperature, dim=0).cpu().numpy()
267
- return np.random.choice(vocab, p=probs)
268
-
269
- ###################################
270
- # Train and evaluate
271
- ###################################
272
-
273
- if train_button:
274
- st.write(f"Training **{model_type}** model...")
275
-
276
- if model_type == "N-gram":
277
- with st.spinner("Training N-gram model..."):
278
- model = NGramModel(tokens, n=3)
279
- elif model_type == "Feed Forward NN":
280
- model = train_ffnn(tokens)
281
- elif model_type == "Decision Tree":
282
- model = train_dt(tokens)
283
- elif model_type == "Gradient Boosted Tree":
284
- model = train_gbt(tokens)
285
- elif model_type == "RNN":
286
- model = train_rnn(tokens)
287
-
288
- st.session_state["model"] = model
289
- st.session_state["model_type"] = model_type
290
- st.success(f"{model_type} model trained.")
291
-
292
- ###################################
293
- # Chat interface
294
- ###################################
295
-
296
- st.header("💬 Chat with the model")
297
-
298
- if "model" in st.session_state:
299
- user_input = st.text_input("Type a prompt:")
300
-
301
- if user_input:
302
- context = tokenize(user_input, tokenizer_type)
303
- generated = context.copy()
304
-
305
- for _ in range(20):
306
- if st.session_state["model_type"] == "N-gram":
307
- next_tok = st.session_state["model"].predict(generated, temperature)
308
- elif st.session_state["model_type"] == "Feed Forward NN":
309
- next_tok = ffnn_predict(st.session_state["model"], generated, temperature)
310
- elif st.session_state["model_type"] == "Decision Tree":
311
- next_tok = dt_predict(st.session_state["model"], generated)
312
- elif st.session_state["model_type"] == "Gradient Boosted Tree":
313
- next_tok = gbt_predict(st.session_state["model"], generated)
314
- elif st.session_state["model_type"] == "RNN":
315
- next_tok = rnn_predict(st.session_state["model"], generated, temperature)
316
-
317
- generated.append(next_tok)
318
- if next_tok == "<END>":
319
- break
320
-
321
- if tokenizer_type == "character":
322
- output = "".join(generated)
323
- else:
324
- output = " ".join(generated)
325
-
326
- st.write("**Model Output:**")
327
- st.write(output)
328
- else:
329
- st.info("Train a model to begin chatting.")
 
1
+ # app.py
2
+
3
+ import os
4
+
5
+ # ✅ Fix PermissionError on Hugging Face Spaces
6
+ os.environ["HF_HOME"] = "/tmp"
7
+ os.environ["HF_DATASETS_CACHE"] = "/tmp"
8
 
9
  import streamlit as st
10
  from datasets import load_dataset
 
159
  return model
160
 
161
  def ffnn_predict(model, context, temperature=1.0):