kundan621 commited on
Commit
c6f4684
·
1 Parent(s): 474e992

Add HuggingFace authentication and better error handling for fine-tuned model

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +43 -5
src/streamlit_app.py CHANGED
@@ -45,11 +45,21 @@ load_dotenv()
45
  def load_fine_tuned_model():
46
  """Load the fine-tuned model from Hugging Face Hub"""
47
  try:
 
 
 
 
 
 
48
  # Replace with your actual repository name
49
  model_name = "kundan621/tinyllama-makemytrip-financial-qa"
50
 
51
- # Load tokenizer
52
- tokenizer = AutoTokenizer.from_pretrained(model_name)
 
 
 
 
53
 
54
  # Load base model
55
  base_model = AutoModelForCausalLM.from_pretrained(
@@ -59,12 +69,17 @@ def load_fine_tuned_model():
59
  trust_remote_code=True,
60
  )
61
 
62
- # Load the fine-tuned PEFT model
63
- model = PeftModel.from_pretrained(base_model, model_name)
 
 
 
 
64
 
65
  return model, tokenizer
66
  except Exception as e:
67
  st.error(f"Error loading fine-tuned model: {e}")
 
68
  return None, None
69
 
70
  def generate_fine_tuned_response(model, tokenizer, question):
@@ -120,15 +135,38 @@ def generate_fine_tuned_response(model, tokenizer, question):
120
  st.set_page_config(page_title="Finance QA Assistant", layout="centered")
121
  st.title("Finance QA Assistant")
122
 
 
 
 
 
 
 
 
 
 
 
123
  # Load fine-tuned model if Fine-Tuned mode is available
124
  fine_tuned_model, fine_tuned_tokenizer = None, None
125
 
126
- mode = st.radio("Choose Answering Mode:", ["RAG", "Fine-Tuned"], horizontal=True)
 
 
 
 
 
 
 
 
127
 
128
  if mode == "Fine-Tuned":
129
  if fine_tuned_model is None or fine_tuned_tokenizer is None:
130
  with st.spinner("Loading fine-tuned model..."):
131
  fine_tuned_model, fine_tuned_tokenizer = load_fine_tuned_model()
 
 
 
 
 
132
 
133
  query = st.text_input("Enter your question:")
134
 
 
45
  def load_fine_tuned_model():
46
  """Load the fine-tuned model from Hugging Face Hub"""
47
  try:
48
+ # Get HuggingFace token from environment
49
+ hf_token = os.getenv("HF_API_KEY")
50
+ if not hf_token:
51
+ st.error("HuggingFace API token not found. Please set HF_API_KEY in your environment.")
52
+ return None, None
53
+
54
  # Replace with your actual repository name
55
  model_name = "kundan621/tinyllama-makemytrip-financial-qa"
56
 
57
+ # Load tokenizer with authentication
58
+ tokenizer = AutoTokenizer.from_pretrained(
59
+ model_name,
60
+ token=hf_token,
61
+ trust_remote_code=True
62
+ )
63
 
64
  # Load base model
65
  base_model = AutoModelForCausalLM.from_pretrained(
 
69
  trust_remote_code=True,
70
  )
71
 
72
+ # Load the fine-tuned PEFT model with authentication
73
+ model = PeftModel.from_pretrained(
74
+ base_model,
75
+ model_name,
76
+ token=hf_token
77
+ )
78
 
79
  return model, tokenizer
80
  except Exception as e:
81
  st.error(f"Error loading fine-tuned model: {e}")
82
+ st.info("Make sure your model repository is public or you have the correct access permissions.")
83
  return None, None
84
 
85
  def generate_fine_tuned_response(model, tokenizer, question):
 
135
  st.set_page_config(page_title="Finance QA Assistant", layout="centered")
136
  st.title("Finance QA Assistant")
137
 
138
+ # Add information about the modes
139
+ with st.expander("ℹ️ About the Modes"):
140
+ st.markdown("""
141
+ **RAG Mode**: Uses Retrieval-Augmented Generation with a vector database and external LLM API.
142
+
143
+ **Fine-Tuned Mode**: Uses a custom fine-tuned TinyLlama model (requires authentication).
144
+
145
+ *Note: Fine-tuned mode requires a HuggingFace API token and access to the private model repository.*
146
+ """)
147
+
148
  # Load fine-tuned model if Fine-Tuned mode is available
149
  fine_tuned_model, fine_tuned_tokenizer = None, None
150
 
151
+ # Check if HuggingFace token is available
152
+ hf_token = os.getenv("HF_API_KEY")
153
+ if hf_token:
154
+ available_modes = ["RAG", "Fine-Tuned"]
155
+ else:
156
+ available_modes = ["RAG"]
157
+ st.warning("⚠️ Fine-Tuned mode is not available. HuggingFace API token is required for accessing private models.")
158
+
159
+ mode = st.radio("Choose Answering Mode:", available_modes, horizontal=True)
160
 
161
  if mode == "Fine-Tuned":
162
  if fine_tuned_model is None or fine_tuned_tokenizer is None:
163
  with st.spinner("Loading fine-tuned model..."):
164
  fine_tuned_model, fine_tuned_tokenizer = load_fine_tuned_model()
165
+
166
+ # If model loading failed, fall back to RAG
167
+ if fine_tuned_model is None or fine_tuned_tokenizer is None:
168
+ st.error("Failed to load fine-tuned model. Falling back to RAG mode.")
169
+ mode = "RAG"
170
 
171
  query = st.text_input("Enter your question:")
172