WebashalarForML commited on
Commit
a24987a
·
verified ·
1 Parent(s): 510a8cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -8
app.py CHANGED
@@ -187,21 +187,67 @@ def chat():
187
  #prompt = prompt_template.format(context=context_text_document,table=context_text_table, question=query_text)
188
  print("results------------------->",prompt)
189
 
190
- #Model Defining and its use
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
192
- HFT = os.environ["HF_TOKEN"]
 
 
 
 
193
  llm = HuggingFaceEndpoint(
194
  repo_id=repo_id,
195
- #max_tokens=3000,
196
  max_new_tokens=2000,
197
- task = "text-generation",
198
  temperature=0.8,
199
  huggingfacehub_api_token=HFT,
200
  )
201
- data= llm.invoke(prompt)
202
- #data= llm(prompt)
203
- #data = response.choices[0].message.content
204
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  # filtering the uneccessary context.
206
  if re.search(r'\bmention\b|\bnot mention\b|\bnot mentioned\b|\bnot contain\b|\bnot include\b|\bnot provide\b|\bdoes not\b|\bnot explicitly\b|\bnot explicitly mentioned\b', data, re.IGNORECASE):
207
  data = "We do not have information related to your query on our end."
 
187
  #prompt = prompt_template.format(context=context_text_document,table=context_text_table, question=query_text)
188
  print("results------------------->",prompt)
189
 
190
+ # #Model Defining and its use
191
+ # repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
192
+ # HFT = os.environ["HF_TOKEN"]
193
+ # llm = HuggingFaceEndpoint(
194
+ # repo_id=repo_id,
195
+ # #max_tokens=3000,
196
+ # max_new_tokens=2000,
197
+ # task = "text-generation",
198
+ # temperature=0.8,
199
+ # huggingfacehub_api_token=HFT,
200
+ # )
201
+ # data= llm.invoke(prompt)
202
+ # #data= llm(prompt)
203
+ # #data = response.choices[0].message.content
204
+ # ---------------------- LLM CALL (FIXED) ----------------------
205
  repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
206
+ HFT = os.environ.get("HF_TOKEN")
207
+
208
+ if not HFT:
209
+ raise RuntimeError("HF_TOKEN not found in environment")
210
+
211
  llm = HuggingFaceEndpoint(
212
  repo_id=repo_id,
213
+ task="text-generation",
214
  max_new_tokens=2000,
 
215
  temperature=0.8,
216
  huggingfacehub_api_token=HFT,
217
  )
218
+
219
+ try:
220
+ # ALWAYS USE generate() — invoke() returns raw HF dict
221
+ raw_resp = llm.generate([prompt])
222
+
223
+ print("\n=== RAW LLM RESPONSE ===")
224
+ print(repr(raw_resp))
225
+ print("========================\n")
226
+
227
+ # --- Robust extraction ---
228
+ if hasattr(raw_resp, "generations"):
229
+ # LangChain LLMResult: list[list[Generation]]
230
+ data = raw_resp.generations[0][0].text
231
+ elif isinstance(raw_resp, dict) and "generated_text" in raw_resp:
232
+ data = raw_resp["generated_text"]
233
+ else:
234
+ data = str(raw_resp)
235
+
236
+ # --- Clean unwanted instruction tokens ---
237
+ data = re.sub(r'<\/?s>', '', data)
238
+ data = re.sub(r'\[\/?INST\]', '', data, flags=re.IGNORECASE)
239
+ data = re.sub(r'<\|im_start\|assistant>|<\|im_end\|>', '', data)
240
+ data = data.strip()
241
+
242
+ print("\n=== CLEANED LLM TEXT ===")
243
+ print(repr(data))
244
+ print("========================\n")
245
+
246
+ except Exception as e:
247
+ print("LLM ERROR:", repr(e))
248
+ flash(f"LLM Error: {e}", "error")
249
+ return redirect(url_for('list_dbs'))
250
+ # --------------------------------------------------------------
251
  # filtering the uneccessary context.
252
  if re.search(r'\bmention\b|\bnot mention\b|\bnot mentioned\b|\bnot contain\b|\bnot include\b|\bnot provide\b|\bdoes not\b|\bnot explicitly\b|\bnot explicitly mentioned\b', data, re.IGNORECASE):
253
  data = "We do not have information related to your query on our end."