Joash commited on
Commit
724ad93
·
1 Parent(s): 395e49f

Fix model name and storage paths for metrics/history

Browse files
Files changed (1) hide show
  1. app.py +18 -22
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
- import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
 
4
  from huggingface_hub import login
5
  import os
6
  import logging
@@ -19,22 +19,16 @@ logger = logging.getLogger(__name__)
19
 
20
  # Environment variables
21
  HF_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
22
- MODEL_NAME = os.getenv("MODEL_NAME", "google/gemma-2-2b-it")
23
-
24
- # Login to Hugging Face with git credential
25
- if HF_TOKEN:
26
- try:
27
- login(token=HF_TOKEN, add_to_git_credential=True)
28
- logger.info("Successfully logged in to Hugging Face")
29
- except Exception as e:
30
- logger.error(f"Error logging in to Hugging Face: {e}")
31
 
32
  # Create data directory for persistence
33
  DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
34
  os.makedirs(DATA_DIR, exist_ok=True)
35
 
36
- # History file
 
37
  HISTORY_FILE = os.path.join(DATA_DIR, "review_history.json")
 
38
 
39
  class Review:
40
  def __init__(self, code: str, language: str, suggestions: str):
@@ -85,13 +79,6 @@ class CodeReviewer:
85
  logger.info(f"Loaded {len(self.review_history)} reviews from history")
86
  except Exception as e:
87
  logger.error(f"Error loading history: {e}")
88
- # Initialize empty history if file doesn't exist or is corrupted
89
- self.review_history = []
90
- self.metrics = {
91
- 'total_reviews': 0,
92
- 'avg_response_time': 0.0,
93
- 'reviews_today': 0
94
- }
95
 
96
  def save_history(self):
97
  """Save review history to file."""
@@ -100,8 +87,6 @@ class CodeReviewer:
100
  'history': [r.to_dict() for r in self.review_history],
101
  'metrics': self.metrics
102
  }
103
- # Ensure the directory exists
104
- os.makedirs(os.path.dirname(HISTORY_FILE), exist_ok=True)
105
  with open(HISTORY_FILE, 'w') as f:
106
  json.dump(data, f)
107
  logger.info("Saved review history")
@@ -118,11 +103,15 @@ class CodeReviewer:
118
  def initialize_model(self):
119
  """Initialize the model and tokenizer."""
120
  try:
 
 
 
121
  logger.info("Loading tokenizer...")
122
  self.tokenizer = AutoTokenizer.from_pretrained(
123
  MODEL_NAME,
124
  token=HF_TOKEN,
125
- trust_remote_code=True
 
126
  )
127
  special_tokens = {
128
  'pad_token': '[PAD]',
@@ -140,6 +129,7 @@ class CodeReviewer:
140
  torch_dtype=torch.float16,
141
  trust_remote_code=True,
142
  low_cpu_mem_usage=True,
 
143
  token=HF_TOKEN
144
  )
145
  if num_added > 0:
@@ -418,5 +408,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
418
  inputs=[code_input, language_input]
419
  )
420
 
 
421
  if __name__ == "__main__":
422
- iface.launch()
 
 
 
 
 
 
1
  import gradio as gr
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
  from huggingface_hub import login
5
  import os
6
  import logging
 
19
 
20
  # Environment variables
21
  HF_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
22
+ MODEL_NAME = os.getenv("MODEL_NAME", "google/gemma-2-2b-it") # Fixed model name
 
 
 
 
 
 
 
 
23
 
24
  # Create data directory for persistence
25
  DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data")
26
  os.makedirs(DATA_DIR, exist_ok=True)
27
 
28
+ # Cache and history files
29
+ CACHE_DIR = os.path.join(DATA_DIR, "cache")
30
  HISTORY_FILE = os.path.join(DATA_DIR, "review_history.json")
31
+ os.makedirs(CACHE_DIR, exist_ok=True)
32
 
33
  class Review:
34
  def __init__(self, code: str, language: str, suggestions: str):
 
79
  logger.info(f"Loaded {len(self.review_history)} reviews from history")
80
  except Exception as e:
81
  logger.error(f"Error loading history: {e}")
 
 
 
 
 
 
 
82
 
83
  def save_history(self):
84
  """Save review history to file."""
 
87
  'history': [r.to_dict() for r in self.review_history],
88
  'metrics': self.metrics
89
  }
 
 
90
  with open(HISTORY_FILE, 'w') as f:
91
  json.dump(data, f)
92
  logger.info("Saved review history")
 
103
  def initialize_model(self):
104
  """Initialize the model and tokenizer."""
105
  try:
106
+ if HF_TOKEN:
107
+ login(token=HF_TOKEN, add_to_git_credential=False)
108
+
109
  logger.info("Loading tokenizer...")
110
  self.tokenizer = AutoTokenizer.from_pretrained(
111
  MODEL_NAME,
112
  token=HF_TOKEN,
113
+ trust_remote_code=True,
114
+ cache_dir=CACHE_DIR
115
  )
116
  special_tokens = {
117
  'pad_token': '[PAD]',
 
129
  torch_dtype=torch.float16,
130
  trust_remote_code=True,
131
  low_cpu_mem_usage=True,
132
+ cache_dir=CACHE_DIR,
133
  token=HF_TOKEN
134
  )
135
  if num_added > 0:
 
408
  inputs=[code_input, language_input]
409
  )
410
 
411
+ # Launch the app
412
  if __name__ == "__main__":
413
+ iface.launch(
414
+ server_name="0.0.0.0",
415
+ server_port=7860,
416
+ show_error=True,
417
+ quiet=False
418
+ )