manabb commited on
Commit
6f4e3ea
·
verified ·
1 Parent(s): b2feffe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -2
app.py CHANGED
@@ -88,7 +88,15 @@ def generate_qa_chain_with_citations(repo_id, llm):
88
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
89
 
90
  # Download files
91
- faiss_path = hf_hub_download(repo_id=repo_id, filename="index.faiss", repo_type="dataset")
 
 
 
 
 
 
 
 
92
  pkl_path = hf_hub_download(repo_id=repo_id, filename="index.pkl", repo_type="dataset")
93
  metadata_path = hf_hub_download(repo_id=repo_id, filename="metadata.json", repo_type="dataset")
94
 
@@ -154,6 +162,23 @@ def format_citations_with_links(sources, uploaded_files):
154
  citations_html.append(citation_html)
155
 
156
  return "".join(citations_html)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
  # ========================================
159
  # Creating the llm with model
@@ -177,7 +202,8 @@ def create_llm_pipeline():
177
  # ========================================
178
  def rag_query_with_citations(question, repo_id, history=[], uploaded_files=[]):
179
  try:
180
- llm = create_llm_pipeline()
 
181
  qa_chain, metadata_path = generate_qa_chain_with_citations(repo_id, llm)
182
 
183
  result = qa_chain.invoke({"query": question})
@@ -232,6 +258,7 @@ with gr.Blocks(title="NRL Chat for Commercial procurement", theme=gr.themes.Soft
232
  if f:
233
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
234
  tmp.write(f.read())
 
235
  file_dict[f.name] = tmp.name
236
  return file_dict
237
 
 
88
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
89
 
90
  # Download files
91
+ # In generate_qa_chain_with_citations(), replace:
92
+ faiss_path = hf_hub_download(
93
+ repo_id=repo_id,
94
+ filename="index.faiss",
95
+ repo_type="dataset",
96
+ cache_dir="/tmp/hf_cache" # Dedicated cache
97
+ )
98
+
99
+ #faiss_path = hf_hub_download(repo_id=repo_id, filename="index.faiss", repo_type="dataset")
100
  pkl_path = hf_hub_download(repo_id=repo_id, filename="index.pkl", repo_type="dataset")
101
  metadata_path = hf_hub_download(repo_id=repo_id, filename="metadata.json", repo_type="dataset")
102
 
 
162
  citations_html.append(citation_html)
163
 
164
  return "".join(citations_html)
165
+ #=========================================
166
+
167
+ from langchain_huggingface import HuggingFacePipeline
168
+ from transformers import pipeline
169
+
170
+ LLM_CACHE = None # Global cache
171
+
172
+ def get_cached_llm():
173
+ global LLM_CACHE
174
+ if LLM_CACHE is None:
175
+ LLM_CACHE = HuggingFacePipeline.from_model_id(
176
+ model_id="distilgpt2", # Smallest, fastest
177
+ task="text-generation",
178
+ device_map="cpu",
179
+ pipeline_kwargs={"max_new_tokens": 100}
180
+ )
181
+ return LLM_CACHE
182
 
183
  # ========================================
184
  # Creating the llm with model
 
202
  # ========================================
203
  def rag_query_with_citations(question, repo_id, history=[], uploaded_files=[]):
204
  try:
205
+ #llm = create_llm_pipeline()
206
+ llm = get_cached_llm() # Single creation
207
  qa_chain, metadata_path = generate_qa_chain_with_citations(repo_id, llm)
208
 
209
  result = qa_chain.invoke({"query": question})
 
258
  if f:
259
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
260
  tmp.write(f.read())
261
+ tmp.close() # Explicit close
262
  file_dict[f.name] = tmp.name
263
  return file_dict
264