Dheeraj-13 commited on
Commit
b11fd72
·
1 Parent(s): 683746a

Refactor ZeroGPU detection to top-level

Browse files
Files changed (1) hide show
  1. services/rag/generate.py +6 -27
services/rag/generate.py CHANGED
@@ -19,30 +19,12 @@ class GeneratorService:
19
  # Initialize OpenAI if key exists
20
  self.openai_client = None
21
  self.openai_model = "gpt-5-nano" # Default
22
- env_key = os.getenv("OPENAI_API_KEY")
23
- if env_key:
24
- self.openai_client = OpenAI(api_key=env_key)
25
-
26
- # Initialize Local Pipeline (Lazy load or load now?)
27
- # To avoid slow startup if not used, we can lazy load,
28
- # but user might want to switch instantly.
29
- # For now, let's load it ONLY if requested or if openai is missing?
30
- # Re-using strict logic: Load structure but pipeline is None
31
- self.local_pipeline = None
32
-
33
- def _ensure_local_loaded(self):
34
- if self.local_pipeline is None:
35
- # Mistral-7B for ZeroGPU (Powerful)
36
- # Note: This might be heavy for local machines without strong GPU/RAM.
37
- model_id = "mistralai/Mistral-7B-Instruct-v0.3"
38
- print(f"Loading local LLM ({model_id})...")
39
- from transformers import pipeline
40
- self.local_pipeline = pipeline(
41
- "text-generation",
42
- model=model_id,
43
- torch_dtype="auto",
44
- device_map="auto"
45
- )
46
 
47
  def _format_context(self, chunks: List[Dict]) -> str:
48
  context_str = ""
@@ -59,9 +41,6 @@ class GeneratorService:
59
  """
60
  context = self._format_context(context_chunks)
61
 
62
- # Determine actual backend
63
- use_openai = (backend == "openai") and (self.openai_client is not None)
64
-
65
  if backend == "openai" and self.openai_client is None:
66
  return "Error: OpenAI backend selected but OPENAI_API_KEY not found. Please switch to Local or set key."
67
 
 
19
  # Initialize OpenAI if key exists
20
  self.openai_client = None
21
  self.openai_model = "gpt-5-nano" # Default
22
+ # Initialize OpenAI if key exists
23
+ api_key = os.getenv("OPENAI_API_KEY")
24
+ if api_key:
25
+ self.openai_client = OpenAI(api_key=api_key)
26
+ else:
27
+ print("Warning: OPENAI_API_KEY not found. OpenAI backend will not work.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  def _format_context(self, chunks: List[Dict]) -> str:
30
  context_str = ""
 
41
  """
42
  context = self._format_context(context_chunks)
43
 
 
 
 
44
  if backend == "openai" and self.openai_client is None:
45
  return "Error: OpenAI backend selected but OPENAI_API_KEY not found. Please switch to Local or set key."
46