Wizzas commited on
Commit
cd9f54c
·
verified ·
1 Parent(s): d09a296

DefectRAG phase 3: incremental real-model application migration

Browse files
deployment/space/app.py CHANGED
@@ -87,7 +87,8 @@ from backend.rag import KnowledgeIndex, EvidenceWorkflow
87
  if os.getenv('DEFECTRAG_LIVE_RAG')=='1':
88
  from deployment.space.generation import CpuBGE, generate_text
89
  INDEX=KnowledgeIndex(ROOT,RUNTIME/'index',embeddings_factory=CpuBGE)
90
- INDEX.ensure()
 
91
  WORKFLOW=EvidenceWorkflow(Settings(),INDEX,generator=generate_text if ZERO else None)
92
  else:
93
  WORKFLOW=None
 
87
  if os.getenv('DEFECTRAG_LIVE_RAG')=='1':
88
  from deployment.space.generation import CpuBGE, generate_text
89
  INDEX=KnowledgeIndex(ROOT,RUNTIME/'index',embeddings_factory=CpuBGE)
90
+ # Retrieval initializes on its first CPU request, not during the ZeroGPU
91
+ # fork snapshot. Eager CPU attention avoids CUDA/SDPA capability probing.
92
  WORKFLOW=EvidenceWorkflow(Settings(),INDEX,generator=generate_text if ZERO else None)
93
  else:
94
  WORKFLOW=None
deployment/space/generation.py CHANGED
@@ -17,7 +17,7 @@ class CpuBGE(Embeddings):
17
  pooling=json.loads((source/'1_Pooling/config.json').read_text())
18
  assert pooling['pooling_mode_cls_token'] and not pooling['pooling_mode_mean_tokens']
19
  self.tokenizer=AutoTokenizer.from_pretrained(str(source),local_files_only=True)
20
- self.model=AutoModel.from_pretrained(str(source),local_files_only=True).eval().to('cpu')
21
  @torch.inference_mode()
22
  def embed_documents(self,texts):
23
  batches=[]
 
17
  pooling=json.loads((source/'1_Pooling/config.json').read_text())
18
  assert pooling['pooling_mode_cls_token'] and not pooling['pooling_mode_mean_tokens']
19
  self.tokenizer=AutoTokenizer.from_pretrained(str(source),local_files_only=True)
20
+ self.model=AutoModel.from_pretrained(str(source),local_files_only=True,attn_implementation='eager').eval().to('cpu')
21
  @torch.inference_mode()
22
  def embed_documents(self,texts):
23
  batches=[]
deployment/space/sessions.py CHANGED
@@ -92,10 +92,14 @@ class SessionRepository:
92
  return StaffRepository(self.cloud_config,STAFF_TOKEN.get(),self.artifacts)
93
  return SQLiteRepository(self.directory / owner / 'inspection.sqlite3')
94
 
 
 
 
95
  def decorate(self, record):
96
  if record is None:
97
  return None
98
  result = dict(record)
 
99
  for kind in ('image', 'heatmap', 'report'):
100
  if kind!='report' and result.get('storage')=='supabase' and result.get(kind+'_url','').startswith('https://'):
101
  continue
@@ -106,7 +110,25 @@ class SessionRepository:
106
  return self.decorate(self.current().save(record))
107
 
108
  def get(self, identifier):
109
- return self.decorate(self.current().get(identifier))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  def list(self, *args, **kwargs):
112
  return [self.decorate(row) for row in self.current().list(*args, **kwargs)]
 
92
  return StaffRepository(self.cloud_config,STAFF_TOKEN.get(),self.artifacts)
93
  return SQLiteRepository(self.directory / owner / 'inspection.sqlite3')
94
 
95
+ def local(self):
96
+ return SQLiteRepository(self.directory / NAMESPACE.get() / 'inspection.sqlite3')
97
+
98
  def decorate(self, record):
99
  if record is None:
100
  return None
101
  result = dict(record)
102
+ result.setdefault('storage','browser_session')
103
  for kind in ('image', 'heatmap', 'report'):
104
  if kind!='report' and result.get('storage')=='supabase' and result.get(kind+'_url','').startswith('https://'):
105
  continue
 
110
  return self.decorate(self.current().save(record))
111
 
112
  def get(self, identifier):
113
+ current=self.current()
114
+ record=current.get(identifier)
115
+ # Signing in must not invalidate this browser's earlier anonymous upload
116
+ # before its explicit Share with staff operation has copied it durably.
117
+ if record is None and self.cloud_config and STAFF_TOKEN.get():
118
+ record=self.local().get(identifier)
119
+ return self.decorate(record)
120
+
121
+ def messages(self,identifier):
122
+ current=self.current()
123
+ if self.cloud_config and STAFF_TOKEN.get() and current.raw(identifier) is None:
124
+ return self.local().messages(identifier)
125
+ return current.messages(identifier)
126
+
127
+ def add_conversation(self,identifier,question,answer):
128
+ current=self.current()
129
+ if self.cloud_config and STAFF_TOKEN.get() and current.raw(identifier) is None:
130
+ return self.local().add_conversation(identifier,question,answer)
131
+ return current.add_conversation(identifier,question,answer)
132
 
133
  def list(self, *args, **kwargs):
134
  return [self.decorate(row) for row in self.current().list(*args, **kwargs)]