NotRev commited on
Commit
cffdf8b
·
verified ·
1 Parent(s): e7e7f38

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +14 -13
src/streamlit_app.py CHANGED
@@ -1,35 +1,34 @@
1
  import json, re, ast, streamlit as st
2
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
  import torch
4
- import os # Necessary to read the HF_TOKEN from environment variables
5
 
6
- # Model ID for the small, structured Gemma model
7
- model_id = "google/gemma-2b-it"
8
 
9
- # 1. READ THE TOKEN: Get the Hugging Face Token from the Space Secrets
10
- HF_TOKEN = os.environ.get("HF_TOKEN")
 
11
 
12
- # 2. PASS THE TOKEN to the tokenizer loading
13
- tok = AutoTokenizer.from_pretrained(model_id, token=HF_TOKEN)
14
-
15
- # Simplified Model Loading (trying to avoid complex quantization)
16
  try:
17
- # Attempt to load using bfloat16 for efficiency
18
  model = AutoModelForCausalLM.from_pretrained(
19
  model_id,
20
  torch_dtype=torch.bfloat16,
21
  device_map="auto",
22
- token=HF_TOKEN # PASS THE TOKEN to the model loading
 
23
  )
24
  except Exception:
25
- # Fallback to float16 if bfloat16 is not supported
26
  model = AutoModelForCausalLM.from_pretrained(
27
  model_id,
28
  torch_dtype=torch.float16,
29
  device_map="auto",
30
- token=HF_TOKEN
 
31
  )
32
 
 
33
  gen = pipeline("text-generation", model=model, tokenizer=tok,
34
  max_new_tokens=256, do_sample=False, return_full_text=False)
35
 
@@ -58,6 +57,8 @@ def extract(text: str):
58
  continue
59
 
60
  if not isinstance(data, dict):
 
 
61
  return {
62
  "SKILL": ["(Error: Invalid/Corrupted Model Output)"],
63
  "KNOWLEDGE": [],
 
1
  import json, re, ast, streamlit as st
2
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
  import torch
4
+ import os
5
 
6
+ # NEW MODEL: Phi-2 - Does NOT use sentencepiece
7
+ model_id = "microsoft/phi-2"
8
 
9
+ # Token is NOT needed for Phi-2
10
+ # HF_TOKEN = os.environ.get("HF_TOKEN") # Removed
11
+ tok = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
12
 
13
+ # Model loading remains the same
 
 
 
14
  try:
 
15
  model = AutoModelForCausalLM.from_pretrained(
16
  model_id,
17
  torch_dtype=torch.bfloat16,
18
  device_map="auto",
19
+ trust_remote_code=True
20
+ # token=HF_TOKEN # Removed
21
  )
22
  except Exception:
 
23
  model = AutoModelForCausalLM.from_pretrained(
24
  model_id,
25
  torch_dtype=torch.float16,
26
  device_map="auto",
27
+ trust_remote_code=True
28
+ # token=HF_TOKEN # Removed
29
  )
30
 
31
+ # ... rest of the pipeline and extraction code is the same ...
32
  gen = pipeline("text-generation", model=model, tokenizer=tok,
33
  max_new_tokens=256, do_sample=False, return_full_text=False)
34
 
 
57
  continue
58
 
59
  if not isinstance(data, dict):
60
+ # NOTE: You are now hitting a KeyError: "SKILL" (image_36e619.png).
61
+ # This is because the model returned bad JSON. This is the code that handles it:
62
  return {
63
  "SKILL": ["(Error: Invalid/Corrupted Model Output)"],
64
  "KNOWLEDGE": [],