Petzys commited on
Commit
687c737
·
1 Parent(s): a3a487b

refactor: functions for index generation and id extraction

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -46,15 +46,18 @@ def build_index():
46
 
47
  return index, meta
48
 
49
- if os.path.exists(INDEX_FILE) and os.path.exists(META_FILE):
50
- print("Loading cached index...")
51
- index = faiss.read_index(INDEX_FILE)
52
- with open(META_FILE, "rb") as f:
53
- meta = pickle.load(f)
54
- else:
55
- index, meta = build_index()
56
-
57
- embedder = SentenceTransformer("all-MiniLM-L6-v2")
 
 
 
58
 
59
  # --- Chat handler ---
60
  def respond(
@@ -113,9 +116,7 @@ EXPLANATION
113
 
114
  if out_text != "Sorry, I couldn't parse the model response.":
115
  try:
116
- id_start = out_text.index("[") +1
117
- id_end = out_text.index("]")
118
- id = out_text[id_start:id_end]
119
  print(f'Read ID: {id}')
120
 
121
  import urllib.request, json
@@ -151,4 +152,8 @@ with gr.Blocks(theme='gstaff/xkcd') as demo:
151
  )
152
 
153
  if __name__ == "__main__":
 
 
 
 
154
  demo.launch()
 
46
 
47
  return index, meta
48
 
49
+ def get_index():
50
+ if os.path.exists(INDEX_FILE) and os.path.exists(META_FILE):
51
+ print("Loading cached index...")
52
+ with open(META_FILE, "rb") as f:
53
+ return faiss.read_index(INDEX_FILE), pickle.load(f)
54
+ else:
55
+ return build_index()
56
+
57
+ def get_id_from_string(str:str) -> str:
58
+ id_start = str.index("[") +1
59
+ id_end = str.index("]")
60
+ return str[id_start:id_end]
61
 
62
  # --- Chat handler ---
63
  def respond(
 
116
 
117
  if out_text != "Sorry, I couldn't parse the model response.":
118
  try:
119
+ id = get_id_from_string(out_text)
 
 
120
  print(f'Read ID: {id}')
121
 
122
  import urllib.request, json
 
152
  )
153
 
154
  if __name__ == "__main__":
155
+ global index
156
+ global meta
157
+ index, meta = get_index()
158
+ embedder = SentenceTransformer("all-MiniLM-L6-v2")
159
  demo.launch()