Arnic commited on
Commit
0e6a725
·
1 Parent(s): acbca58

fix: defer rag_pipeline import to inside @spaces.GPU worker — eliminates CUDA init error on zero-a10g

Browse files
Files changed (1) hide show
  1. app.py +49 -33
app.py CHANGED
@@ -2,32 +2,31 @@
2
 
3
  import os
4
  import sys
5
- from threading import Thread
6
 
7
  sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
8
 
9
  import gradio as gr
10
- import spaces
11
- from rag_pipeline import AethronPipeline
 
 
 
 
 
 
12
 
13
  print("=" * 55)
14
  print("AETHRON PORTFOLIO AGENT — CPU Mode")
15
  print("=" * 55)
16
 
17
- # Dummy GPU function required by HF Spaces runtime detection on zero-a10g.
18
- # Never called — all inference runs on CPU.
19
- @spaces.GPU(duration=120)
20
- def _gpu_placeholder() -> str:
21
- return "CPU-only mode"
22
-
23
-
24
- # Lazy-load pipeline on first query (model on CPU)
25
  _pipeline = None
26
 
27
 
28
  def get_pipeline():
29
  global _pipeline
30
  if _pipeline is None:
 
31
  print("Loading pipeline on CPU (first query only)...")
32
  _pipeline = AethronPipeline(
33
  build_index=not os.path.exists("data/index/faiss.index")
@@ -35,28 +34,45 @@ def get_pipeline():
35
  return _pipeline
36
 
37
 
38
- def respond(message, history):
39
- if not message or not message.strip():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  return history
41
-
42
- pipeline = get_pipeline()
43
- result = pipeline.query(message.strip())
44
- response = result["answer"]
45
- if result["sources"]:
46
- source_names = []
47
- for s in result["sources"][:3]:
48
- name = s["section"].replace("_", " ").title()
49
- if "Summary" in name:
50
- name = name.replace("Summary", "(Summary)")
51
- source_names.append(name)
52
- response += "\n\n**Sources:** " + " | ".join(source_names)
53
- history.append({"role": "user", "content": message})
54
- history.append({"role": "assistant", "content": response})
55
- return history
56
-
57
-
58
- def clear_chat():
59
- return []
60
 
61
 
62
  CUSTOM_CSS = """
@@ -117,7 +133,7 @@ with gr.Blocks(css=CUSTOM_CSS, title="Aethron | Chat with Arash's Portfolio") as
117
 
118
  msg_input.submit(respond, [msg_input, chatbot], [chatbot])
119
  send_btn.click(respond, [msg_input, chatbot], [chatbot])
120
- clear_btn.click(clear_chat, None, [chatbot], queue=False)
121
 
122
  gr.Markdown("""
123
  ---
 
2
 
3
  import os
4
  import sys
 
5
 
6
  sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
7
 
8
  import gradio as gr
9
+
10
+ try:
11
+ import spaces
12
+ import spaces.zero
13
+ from spaces.zero import torch as spaces_torch
14
+ spaces_torch.patch()
15
+ except ImportError:
16
+ spaces = None
17
 
18
  print("=" * 55)
19
  print("AETHRON PORTFOLIO AGENT — CPU Mode")
20
  print("=" * 55)
21
 
22
+ # Lazy-loaded inside @spaces.GPU context to avoid CUDA init in main process
 
 
 
 
 
 
 
23
  _pipeline = None
24
 
25
 
26
  def get_pipeline():
27
  global _pipeline
28
  if _pipeline is None:
29
+ from rag_pipeline import AethronPipeline
30
  print("Loading pipeline on CPU (first query only)...")
31
  _pipeline = AethronPipeline(
32
  build_index=not os.path.exists("data/index/faiss.index")
 
34
  return _pipeline
35
 
36
 
37
+ if spaces is not None:
38
+ @spaces.GPU(duration=120)
39
+ def respond(message, history):
40
+ if not message or not message.strip():
41
+ return history
42
+ pipeline = get_pipeline()
43
+ result = pipeline.query(message.strip())
44
+ response = result["answer"]
45
+ if result["sources"]:
46
+ source_names = []
47
+ for s in result["sources"][:3]:
48
+ name = s["section"].replace("_", " ").title()
49
+ if "Summary" in name:
50
+ name = name.replace("Summary", "(Summary)")
51
+ source_names.append(name)
52
+ response += "\n\n**Sources:** " + " | ".join(source_names)
53
+ history.append({"role": "user", "content": message})
54
+ history.append({"role": "assistant", "content": response})
55
+ return history
56
+ else:
57
+ from rag_pipeline import AethronPipeline
58
+
59
+ def respond(message, history):
60
+ if not message or not message.strip():
61
+ return history
62
+ pipeline = get_pipeline()
63
+ result = pipeline.query(message.strip())
64
+ response = result["answer"]
65
+ if result["sources"]:
66
+ source_names = []
67
+ for s in result["sources"][:3]:
68
+ name = s["section"].replace("_", " ").title()
69
+ if "Summary" in name:
70
+ name = name.replace("Summary", "(Summary)")
71
+ source_names.append(name)
72
+ response += "\n\n**Sources:** " + " | ".join(source_names)
73
+ history.append({"role": "user", "content": message})
74
+ history.append({"role": "assistant", "content": response})
75
  return history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
 
78
  CUSTOM_CSS = """
 
133
 
134
  msg_input.submit(respond, [msg_input, chatbot], [chatbot])
135
  send_btn.click(respond, [msg_input, chatbot], [chatbot])
136
+ clear_btn.click(lambda: [], None, [chatbot], queue=False)
137
 
138
  gr.Markdown("""
139
  ---