Joash commited on
Commit
3e82f96
·
1 Parent(s): 80d4148

Add ZeroGPU support and update dependencies

Browse files
Files changed (2) hide show
  1. app.py +33 -9
  2. requirements.txt +5 -0
app.py CHANGED
@@ -7,6 +7,9 @@ import logging
7
  from datetime import datetime
8
  import json
9
  from typing import List, Dict
 
 
 
10
 
11
  # Configure logging
12
  logging.basicConfig(level=logging.INFO)
@@ -16,6 +19,16 @@ logger = logging.getLogger(__name__)
16
  HF_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
17
  MODEL_NAME = os.getenv("MODEL_NAME", "google/gemma-2b-it")
18
 
 
 
 
 
 
 
 
 
 
 
19
  class Review:
20
  def __init__(self, code: str, language: str, suggestions: str):
21
  self.code = code
@@ -28,8 +41,7 @@ class CodeReviewer:
28
  def __init__(self):
29
  self.model = None
30
  self.tokenizer = None
31
- # Let ZeroGPU handle GPU allocation
32
- self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
33
  self.review_history: List[Review] = []
34
  self.metrics = {
35
  'total_reviews': 0,
@@ -52,14 +64,17 @@ class CodeReviewer:
52
  )
53
 
54
  logger.info("Loading model...")
55
- # Let ZeroGPU handle device mapping
56
  self.model = AutoModelForCausalLM.from_pretrained(
57
  MODEL_NAME,
58
  token=HF_TOKEN,
59
  device_map="auto",
60
- torch_dtype=torch.float16, # Use fp16 for GPU
61
- trust_remote_code=True
 
 
62
  )
 
63
  logger.info(f"Model loaded successfully on {self.device}")
64
  except Exception as e:
65
  logger.error(f"Error initializing model: {e}")
@@ -164,6 +179,13 @@ Code:
164
  'Device': str(self.device)
165
  }
166
 
 
 
 
 
 
 
 
167
  # Create Gradio interface
168
  with gr.Blocks(theme=gr.themes.Soft()) as iface:
169
  gr.Markdown("# Code Review Assistant")
@@ -203,9 +225,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
203
  label="Performance Metrics"
204
  )
205
 
206
- # Initialize reviewer
207
- reviewer = CodeReviewer()
208
-
209
  # Set up event handlers
210
  def review_code_interface(code: str, language: str) -> str:
211
  if not code.strip():
@@ -264,4 +283,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
264
 
265
  # Launch the app
266
  if __name__ == "__main__":
267
- iface.launch()
 
 
 
 
 
 
7
  from datetime import datetime
8
  import json
9
  from typing import List, Dict
10
+ from huggingface_hub import HfApi
11
+ from huggingface_hub.spaces import SpaceHardware, SpaceStage
12
+ from huggingface_hub.spaces.space_sdk import SpaceRuntime
13
 
14
  # Configure logging
15
  logging.basicConfig(level=logging.INFO)
 
19
  HF_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
20
  MODEL_NAME = os.getenv("MODEL_NAME", "google/gemma-2b-it")
21
 
22
+ # Initialize Hugging Face API
23
+ api = HfApi()
24
+
25
+ # Space hardware configuration
26
+ space_config = {
27
+ "hardware": SpaceHardware.A10G_SMALL,
28
+ "stage": SpaceStage.RUNTIME,
29
+ "runtime": SpaceRuntime.ZEROGPU
30
+ }
31
+
32
  class Review:
33
  def __init__(self, code: str, language: str, suggestions: str):
34
  self.code = code
 
41
  def __init__(self):
42
  self.model = None
43
  self.tokenizer = None
44
+ self.device = None
 
45
  self.review_history: List[Review] = []
46
  self.metrics = {
47
  'total_reviews': 0,
 
64
  )
65
 
66
  logger.info("Loading model...")
67
+ # Initialize with ZeroGPU configuration
68
  self.model = AutoModelForCausalLM.from_pretrained(
69
  MODEL_NAME,
70
  token=HF_TOKEN,
71
  device_map="auto",
72
+ torch_dtype=torch.float16,
73
+ trust_remote_code=True,
74
+ low_cpu_mem_usage=True,
75
+ use_zerogpu=True # Enable ZeroGPU
76
  )
77
+ self.device = next(self.model.parameters()).device
78
  logger.info(f"Model loaded successfully on {self.device}")
79
  except Exception as e:
80
  logger.error(f"Error initializing model: {e}")
 
179
  'Device': str(self.device)
180
  }
181
 
182
+ # Initialize reviewer with ZeroGPU
183
+ @space_config
184
+ def create_reviewer():
185
+ return CodeReviewer()
186
+
187
+ reviewer = create_reviewer()
188
+
189
  # Create Gradio interface
190
  with gr.Blocks(theme=gr.themes.Soft()) as iface:
191
  gr.Markdown("# Code Review Assistant")
 
225
  label="Performance Metrics"
226
  )
227
 
 
 
 
228
  # Set up event handlers
229
  def review_code_interface(code: str, language: str) -> str:
230
  if not code.strip():
 
283
 
284
  # Launch the app
285
  if __name__ == "__main__":
286
+ iface.launch(
287
+ share=False,
288
+ server_name="0.0.0.0",
289
+ server_port=7860,
290
+ enable_queue=True
291
+ )
requirements.txt CHANGED
@@ -6,6 +6,11 @@ accelerate>=0.27.2
6
  safetensors>=0.4.2
7
  sentencepiece>=0.1.99
8
 
 
 
 
 
 
9
  # Utilities
10
  python-dotenv>=1.0.0
11
  pydantic>=2.4.2
 
6
  safetensors>=0.4.2
7
  sentencepiece>=0.1.99
8
 
9
+ # Hugging Face
10
+ huggingface-hub>=0.20.3
11
+ huggingface_hub[cli]>=0.20.3
12
+ huggingface_hub[space]>=0.20.3
13
+
14
  # Utilities
15
  python-dotenv>=1.0.0
16
  pydantic>=2.4.2