tstone87 commited on
Commit
93b0124
Β·
verified Β·
1 Parent(s): ef66697

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -38
app.py CHANGED
@@ -1,19 +1,25 @@
1
- import gradio as gr
2
  import faiss
3
  import numpy as np
4
- import os
5
  from sentence_transformers import SentenceTransformer
6
- from huggingface_hub import InferenceClient
7
 
8
- # Load embedding model
9
- model = SentenceTransformer("all-MiniLM-L6-v2")
 
10
 
11
- # File paths
12
- TEXT_FILE = "combined_text_documents.txt"
 
 
 
 
 
13
  EMBEDDINGS_FILE = "policy_embeddings.npy"
14
  INDEX_FILE = "faiss_index.bin"
 
15
 
16
- # Load policy text from file
17
  if os.path.exists(TEXT_FILE):
18
  with open(TEXT_FILE, "r", encoding="utf-8") as f:
19
  POLICY_TEXT = f.read()
@@ -22,35 +28,88 @@ else:
22
  print("❌ ERROR: combined_text_documents.txt not found! Ensure it's uploaded.")
23
  POLICY_TEXT = ""
24
 
25
- # Split text into chunks for FAISS indexing
 
 
 
26
  chunk_size = 500
27
  chunks = [POLICY_TEXT[i:i+chunk_size] for i in range(0, len(POLICY_TEXT), chunk_size)] if POLICY_TEXT else []
28
 
29
- # Check if precomputed embeddings and FAISS index exist
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  if os.path.exists(EMBEDDINGS_FILE) and os.path.exists(INDEX_FILE):
31
- print("βœ… Loading precomputed FAISS index and embeddings...")
32
  embeddings = np.load(EMBEDDINGS_FILE)
33
  index = faiss.read_index(INDEX_FILE)
34
  else:
35
- print("πŸš€ Generating embeddings and FAISS index (First-time setup)...")
36
- if chunks:
37
- embeddings = np.array([model.encode(chunk) for chunk in chunks])
38
- np.save(EMBEDDINGS_FILE, embeddings) # Save for future runs
39
-
40
- # Use FAISS optimized index for faster lookup
41
- d = embeddings.shape[1]
42
- nlist = 10 # Number of clusters
43
- index = faiss.IndexIVFFlat(faiss.IndexFlatL2(d), d, nlist)
44
- index.train(embeddings)
45
- index.add(embeddings)
46
- index.nprobe = 2 # Speed optimization
47
- faiss.write_index(index, INDEX_FILE) # Save FAISS index
48
- print("βœ… FAISS index created and saved.")
49
- else:
50
- print("❌ ERROR: No text to index. Check combined_text_documents.txt.")
51
- index = None
52
 
53
- # πŸ”Ή Function to search FAISS
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  def search_policy(query, top_k=3):
55
  if index is None:
56
  return "Error: FAISS index is not available."
@@ -61,16 +120,11 @@ def search_policy(query, top_k=3):
61
  return "\n\n".join([chunks[i] for i in indices[0] if i < len(chunks)])
62
 
63
  # πŸ”Ή Hugging Face LLM Client
 
64
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
65
 
66
- def respond(
67
- message,
68
- history: list[tuple[str, str]],
69
- system_message,
70
- max_tokens,
71
- temperature,
72
- top_p,
73
- ):
74
  messages = [{"role": "system", "content": system_message}]
75
 
76
  for val in history:
@@ -113,11 +167,13 @@ def respond(
113
  yield response
114
 
115
  # πŸ”Ή Gradio Chat Interface
 
 
116
  demo = gr.ChatInterface(
117
  respond,
118
  additional_inputs=[
119
  gr.Textbox(
120
- value="You are a knowledgeable and professional chatbot designed to assist Colorado case workers in determining eligibility for public assistance programs. Your primary role is to provide accurate, up-to-date, and policy-compliant information on Medicaid, SNAP, TANF, CHP+, and other state and federal assistance programs. Responses should be clear, concise, and structured based on eligibility criteria, income limits, deductions, federal poverty level guidelines, and program-specific requirements.",
121
  label="System message"
122
  ),
123
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
 
1
+ import os
2
  import faiss
3
  import numpy as np
 
4
  from sentence_transformers import SentenceTransformer
5
+ from huggingface_hub import HfApi, hf_hub_download, login, whoami
6
 
7
+ # πŸ”Ή Hugging Face Repository Details
8
+ HF_REPO_ID = "tstone87/repo" # Your repo
9
+ HF_TOKEN = os.getenv("HF_TOKEN") # Retrieve token securely from environment variable
10
 
11
+ if not HF_TOKEN:
12
+ raise ValueError("❌ ERROR: Hugging Face token not found. Add it as a secret in the Hugging Face Space settings.")
13
+
14
+ # πŸ”Ή Authenticate with Hugging Face
15
+ login(token=HF_TOKEN)
16
+
17
+ # πŸ”Ή File Paths
18
  EMBEDDINGS_FILE = "policy_embeddings.npy"
19
  INDEX_FILE = "faiss_index.bin"
20
+ TEXT_FILE = "combined_text_documents.txt"
21
 
22
+ # πŸ”Ή Load policy text from file
23
  if os.path.exists(TEXT_FILE):
24
  with open(TEXT_FILE, "r", encoding="utf-8") as f:
25
  POLICY_TEXT = f.read()
 
28
  print("❌ ERROR: combined_text_documents.txt not found! Ensure it's uploaded.")
29
  POLICY_TEXT = ""
30
 
31
+ # πŸ”Ή Sentence Embedding Model (Optimized for Speed)
32
+ model = SentenceTransformer("all-MiniLM-L6-v2")
33
+
34
+ # πŸ”Ή Split policy text into chunks for FAISS indexing
35
  chunk_size = 500
36
  chunks = [POLICY_TEXT[i:i+chunk_size] for i in range(0, len(POLICY_TEXT), chunk_size)] if POLICY_TEXT else []
37
 
38
+ # πŸ”Ή Function to Upload FAISS Files to Hugging Face Hub
39
+ def upload_faiss_to_hf():
40
+ api = HfApi()
41
+
42
+ if os.path.exists(EMBEDDINGS_FILE):
43
+ print("πŸ“€ Uploading FAISS embeddings to Hugging Face...")
44
+ api.upload_file(
45
+ path_or_fileobj=EMBEDDINGS_FILE,
46
+ path_in_repo=EMBEDDINGS_FILE,
47
+ repo_id=HF_REPO_ID,
48
+ repo_type="dataset",
49
+ token=HF_TOKEN,
50
+ )
51
+
52
+ if os.path.exists(INDEX_FILE):
53
+ print("πŸ“€ Uploading FAISS index to Hugging Face...")
54
+ api.upload_file(
55
+ path_or_fileobj=INDEX_FILE,
56
+ path_in_repo=INDEX_FILE,
57
+ repo_id=HF_REPO_ID,
58
+ repo_type="dataset",
59
+ token=HF_TOKEN,
60
+ )
61
+
62
+ print("βœ… FAISS files successfully uploaded to Hugging Face.")
63
+
64
+ # πŸ”Ή Function to Download FAISS Files from Hugging Face Hub if Missing
65
+ def download_faiss_from_hf():
66
+ if not os.path.exists(EMBEDDINGS_FILE):
67
+ print("πŸ“₯ Downloading FAISS embeddings from Hugging Face...")
68
+ hf_hub_download(repo_id=HF_REPO_ID, filename=EMBEDDINGS_FILE, local_dir=".", token=HF_TOKEN)
69
+
70
+ if not os.path.exists(INDEX_FILE):
71
+ print("πŸ“₯ Downloading FAISS index from Hugging Face...")
72
+ hf_hub_download(repo_id=HF_REPO_ID, filename=INDEX_FILE, local_dir=".", token=HF_TOKEN)
73
+
74
+ print("βœ… FAISS files downloaded from Hugging Face.")
75
+
76
+ # πŸ”Ή Check if FAISS Files Exist, Otherwise Download
77
  if os.path.exists(EMBEDDINGS_FILE) and os.path.exists(INDEX_FILE):
78
+ print("βœ… FAISS files found locally. Loading from disk...")
79
  embeddings = np.load(EMBEDDINGS_FILE)
80
  index = faiss.read_index(INDEX_FILE)
81
  else:
82
+ print("πŸš€ FAISS files not found! Downloading from Hugging Face...")
83
+ download_faiss_from_hf()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
+ if os.path.exists(EMBEDDINGS_FILE) and os.path.exists(INDEX_FILE):
86
+ embeddings = np.load(EMBEDDINGS_FILE)
87
+ index = faiss.read_index(INDEX_FILE)
88
+ else:
89
+ print("πŸš€ No FAISS files found. Recomputing...")
90
+ if chunks:
91
+ embeddings = np.array([model.encode(chunk) for chunk in chunks])
92
+
93
+ # Save embeddings for future use
94
+ np.save(EMBEDDINGS_FILE, embeddings)
95
+
96
+ # Use FAISS optimized index for faster lookup
97
+ d = embeddings.shape[1]
98
+ nlist = 10 # Number of clusters
99
+ index = faiss.IndexIVFFlat(faiss.IndexFlatL2(d), d, nlist)
100
+ index.train(embeddings)
101
+ index.add(embeddings)
102
+ index.nprobe = 2 # Speed optimization
103
+
104
+ # Save FAISS index
105
+ faiss.write_index(index, INDEX_FILE)
106
+ upload_faiss_to_hf() # Upload FAISS files to Hugging Face
107
+ print("βœ… FAISS index created and saved.")
108
+ else:
109
+ print("❌ ERROR: No text to index. Check combined_text_documents.txt.")
110
+ index = None
111
+
112
+ # πŸ”Ή Function to Search FAISS
113
  def search_policy(query, top_k=3):
114
  if index is None:
115
  return "Error: FAISS index is not available."
 
120
  return "\n\n".join([chunks[i] for i in indices[0] if i < len(chunks)])
121
 
122
  # πŸ”Ή Hugging Face LLM Client
123
+ from huggingface_hub import InferenceClient
124
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
125
 
126
+ # πŸ”Ή Function to Handle Chat Responses
127
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
 
 
 
 
 
 
128
  messages = [{"role": "system", "content": system_message}]
129
 
130
  for val in history:
 
167
  yield response
168
 
169
  # πŸ”Ή Gradio Chat Interface
170
+ import gradio as gr
171
+
172
  demo = gr.ChatInterface(
173
  respond,
174
  additional_inputs=[
175
  gr.Textbox(
176
+ value="You are a knowledgeable and professional chatbot designed to assist Colorado case workers in determining eligibility for public assistance programs.",
177
  label="System message"
178
  ),
179
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),