wylum commited on
Commit
c14bda5
·
verified ·
1 Parent(s): 5d8959c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -3
app.py CHANGED
@@ -197,9 +197,11 @@ def create_collection_name(filepath):
197
  print('Collection name: ', collection_name)
198
  return collection_name
199
 
200
- def build_qa_chain(collection_name, vector_db, file: str):
 
201
  print("in build_qa_chain="+file.name)
202
  documents, file_name = process_file2(file)
 
203
  # Load embeddings model
204
  #embeddings = OpenAIEmbeddings(openai_api_key=self.OPENAI_API_KEY)
205
 
@@ -275,6 +277,73 @@ def build_qa_chain(collection_name, vector_db, file: str):
275
  #vincent for new LLM
276
  #llm_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
277
  #llm_model = "meta-llama/Llama-2-7b-chat-hf"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278
  llm = HuggingFaceEndpoint(
279
  repo_id=llm_model,
280
  task="text-generation", # Explicitly specify task
@@ -283,7 +352,7 @@ def build_qa_chain(collection_name, vector_db, file: str):
283
  max_new_tokens = 250,
284
  top_k = 3,
285
  )
286
-
287
  chain = ConversationalRetrievalChain.from_llm(
288
  llm,
289
  retriever=vector_db.as_retriever(),
@@ -320,7 +389,8 @@ def get_response(collection_name, vector_db, qa_chain, history, query, file):
320
 
321
  #vincent added 20250211
322
  if app.count == 0:
323
- collection_name, vector_db, qa_chain = build_qa_chain(collection_name, vector_db, file)
 
324
  result = qa_chain.invoke(
325
  {"question": query, "chat_history": chat_history_tuples}, return_only_outputs=True
326
  #{"question": query, "chat_history": format_chat_history(query, history)}, return_only_outputs=True
 
197
  print('Collection name: ', collection_name)
198
  return collection_name
199
 
200
+ #collection_name, vector_db, btn, llm_btn, slider_temperature, slider_maxtokens, slider_topk]
201
+ def build_qa_chain(collection_name, vector_db, file: str, llm_option, llm_temperature, max_tokens, top_k, progress=gr.Progress()):
202
  print("in build_qa_chain="+file.name)
203
  documents, file_name = process_file2(file)
204
+
205
  # Load embeddings model
206
  #embeddings = OpenAIEmbeddings(openai_api_key=self.OPENAI_API_KEY)
207
 
 
277
  #vincent for new LLM
278
  #llm_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
279
  #llm_model = "meta-llama/Llama-2-7b-chat-hf"
280
+
281
+ llm_model = list_llm[llm_option]
282
+ task="text-generation" # Explicitly specify task
283
+
284
+ if llm_model == "mistralai/Mixtral-8x7B-Instruct-v0.1":
285
+ llm = HuggingFaceEndpoint(
286
+ repo_id=llm_model,
287
+ task=task, # Explicitly specify task
288
+ # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "load_in_8bit": True}
289
+ temperature = temperature,
290
+ max_new_tokens = max_tokens,
291
+ top_k = top_k,
292
+ load_in_8bit = True,
293
+ )
294
+ elif llm_model in ["HuggingFaceH4/zephyr-7b-gemma-v0.1","mosaicml/mpt-7b-instruct"]:
295
+ raise gr.Error("LLM model is too large to be loaded automatically on free inference endpoint")
296
+ llm = HuggingFaceEndpoint(
297
+ repo_id=llm_model,
298
+ task=task, # Explicitly specify task
299
+ temperature = temperature,
300
+ max_new_tokens = max_tokens,
301
+ top_k = top_k,
302
+ )
303
+ elif llm_model == "microsoft/phi-2":
304
+ # raise gr.Error("phi-2 model requires 'trust_remote_code=True', currently not supported by langchain HuggingFaceHub...")
305
+ llm = HuggingFaceEndpoint(
306
+ repo_id=llm_model,
307
+ task=task, # Explicitly specify task
308
+ # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "trust_remote_code": True, "torch_dtype": "auto"}
309
+ temperature = temperature,
310
+ max_new_tokens = max_tokens,
311
+ top_k = top_k,
312
+ trust_remote_code = True,
313
+ torch_dtype = "auto",
314
+ )
315
+ elif llm_model == "TinyLlama/TinyLlama-1.1B-Chat-v1.0":
316
+ llm = HuggingFaceEndpoint(
317
+ repo_id=llm_model,
318
+ task=task, # Explicitly specify task
319
+ # model_kwargs={"temperature": temperature, "max_new_tokens": 250, "top_k": top_k}
320
+ temperature = temperature,
321
+ max_new_tokens = 250,
322
+ top_k = top_k,
323
+ )
324
+ elif llm_model == "meta-llama/Llama-2-7b-chat-hf":
325
+ raise gr.Error("Llama-2-7b-chat-hf model requires a Pro subscription...")
326
+ llm = HuggingFaceEndpoint(
327
+ repo_id=llm_model,
328
+ task=task, # Explicitly specify task
329
+ # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k}
330
+ temperature = temperature,
331
+ max_new_tokens = max_tokens,
332
+ top_k = top_k,
333
+ )
334
+ else:
335
+ llm = HuggingFaceEndpoint(
336
+ repo_id=llm_model,
337
+ task=task, # Explicitly specify task
338
+ # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "trust_remote_code": True, "torch_dtype": "auto"}
339
+ # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k}
340
+ temperature = temperature,
341
+ max_new_tokens = max_tokens,
342
+ top_k = top_k,
343
+ )
344
+
345
+
346
+ """
347
  llm = HuggingFaceEndpoint(
348
  repo_id=llm_model,
349
  task="text-generation", # Explicitly specify task
 
352
  max_new_tokens = 250,
353
  top_k = 3,
354
  )
355
+ """
356
  chain = ConversationalRetrievalChain.from_llm(
357
  llm,
358
  retriever=vector_db.as_retriever(),
 
389
 
390
  #vincent added 20250211
391
  if app.count == 0:
392
+ #collection_name, vector_db, qa_chain = build_qa_chain(collection_name, vector_db, file)
393
+ raise gr.Error("Please initialize the Chain first!")
394
  result = qa_chain.invoke(
395
  {"question": query, "chat_history": chat_history_tuples}, return_only_outputs=True
396
  #{"question": query, "chat_history": format_chat_history(query, history)}, return_only_outputs=True