scott12355 commited on
Commit
237f784
·
1 Parent(s): bfb593e

Refactor GPU initialization and device management for Hugging Face Spaces compatibility

Browse files
Files changed (1) hide show
  1. main.py +24 -24
main.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from uuid import UUID
2
  from fastapi import FastAPI, HTTPException
3
  from transformers import pipeline
@@ -12,25 +13,35 @@ from VectorDB import *
12
  from pydantic import BaseModel
13
  import threading
14
  import uvicorn
15
- import spaces
16
-
17
- zero = torch.Tensor([0]).cuda()
18
- print(zero.device) # <-- 'cpu' 🤔
19
 
 
20
  @spaces.GPU
21
- def greet(n):
22
- print(zero.device) # <-- 'cuda:0' 🤗
23
- return f"Hello {zero + n} Tensor"
 
24
 
 
 
25
 
26
  # Pick the best available device - MPS (Mac), CUDA (NVIDIA), or CPU
27
- if torch.backends.mps.is_available():
28
- device = torch.device("mps")
29
- elif torch.cuda.is_available():
30
- device = torch.device("cuda")
31
- else:
 
 
 
 
 
 
 
 
 
 
 
32
  device = torch.device("cpu")
33
- #print(device)
34
 
35
  initRAG(device)
36
  supabase: Client = initSupabase()
@@ -341,17 +352,6 @@ async def status():
341
 
342
 
343
 
344
- # ...existing code...
345
-
346
- # Remove this section
347
- # def run_fastapi():
348
- # print("Starting FastAPI server on http://0.0.0.0:8000")
349
- # uvicorn.run(app, host="0.0.0.0", port=8000, log_level="info", reload=False)
350
- #
351
- # # Start FastAPI in a separate thread
352
- # fastapi_thread = threading.Thread(target=run_fastapi, daemon=True)
353
- # fastapi_thread.start()
354
-
355
  # Gradio Interface
356
  def chatbot_interface(user_input, history):
357
  return f"You said: {user_input}" # Replace with actual chatbot logic
 
1
+ import spaces
2
  from uuid import UUID
3
  from fastapi import FastAPI, HTTPException
4
  from transformers import pipeline
 
13
  from pydantic import BaseModel
14
  import threading
15
  import uvicorn
 
 
 
 
16
 
17
+ # Use spaces.GPU decorator for GPU operations
18
  @spaces.GPU
19
+ def initialize_gpu():
20
+ zero = torch.Tensor([0]).cuda()
21
+ print(f"GPU initialized: {zero.device}")
22
+ return zero.device
23
 
24
+ # Call this function after app startup
25
+ device_info = None
26
 
27
  # Pick the best available device - MPS (Mac), CUDA (NVIDIA), or CPU
28
+ try:
29
+ # For Hugging Face Spaces, let spaces handle GPU initialization
30
+ if "spaces" in sys.modules:
31
+ print("Running in Hugging Face Spaces, using spaces.GPU for device management")
32
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
33
+ else:
34
+ # Local development path
35
+ if torch.backends.mps.is_available():
36
+ device = torch.device("mps")
37
+ elif torch.cuda.is_available():
38
+ device = torch.device("cuda")
39
+ else:
40
+ device = torch.device("cpu")
41
+ print(f"Using device: {device}")
42
+ except Exception as e:
43
+ print(f"Error detecting device, falling back to CPU: {str(e)}")
44
  device = torch.device("cpu")
 
45
 
46
  initRAG(device)
47
  supabase: Client = initSupabase()
 
352
 
353
 
354
 
 
 
 
 
 
 
 
 
 
 
 
355
  # Gradio Interface
356
  def chatbot_interface(user_input, history):
357
  return f"You said: {user_input}" # Replace with actual chatbot logic