Spaces:
Build error
Build error
Joash commited on
Commit ·
67f032c
1
Parent(s): 724ad93
Fix metrics and history with proper file permissions in Space environment
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import json
|
|
| 9 |
from typing import List, Dict
|
| 10 |
import warnings
|
| 11 |
import spaces
|
|
|
|
| 12 |
|
| 13 |
# Filter out warnings
|
| 14 |
warnings.filterwarnings('ignore')
|
|
@@ -19,16 +20,35 @@ 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 |
-
# Create data directory for persistence
|
| 25 |
-
DATA_DIR =
|
| 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,6 +99,13 @@ class CodeReviewer:
|
|
| 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,8 +114,14 @@ class CodeReviewer:
|
|
| 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")
|
| 93 |
except Exception as e:
|
| 94 |
logger.error(f"Error saving history: {e}")
|
|
@@ -104,7 +137,7 @@ class CodeReviewer:
|
|
| 104 |
"""Initialize the model and tokenizer."""
|
| 105 |
try:
|
| 106 |
if HF_TOKEN:
|
| 107 |
-
login(token=HF_TOKEN, add_to_git_credential=
|
| 108 |
|
| 109 |
logger.info("Loading tokenizer...")
|
| 110 |
self.tokenizer = AutoTokenizer.from_pretrained(
|
|
@@ -410,9 +443,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
| 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 |
-
)
|
|
|
|
| 9 |
from typing import List, Dict
|
| 10 |
import warnings
|
| 11 |
import spaces
|
| 12 |
+
import stat
|
| 13 |
|
| 14 |
# Filter out warnings
|
| 15 |
warnings.filterwarnings('ignore')
|
|
|
|
| 20 |
|
| 21 |
# Environment variables
|
| 22 |
HF_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
|
| 23 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "google/gemma-2-2b-it")
|
| 24 |
|
| 25 |
+
# Create data directory for persistence in Space environment
|
| 26 |
+
DATA_DIR = "/home/user/app/data"
|
| 27 |
os.makedirs(DATA_DIR, exist_ok=True)
|
| 28 |
+
# Ensure directory has write permissions
|
| 29 |
+
os.chmod(DATA_DIR, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
|
| 30 |
+
logger.info(f"Using data directory: {DATA_DIR}")
|
| 31 |
|
| 32 |
# Cache and history files
|
| 33 |
CACHE_DIR = os.path.join(DATA_DIR, "cache")
|
| 34 |
HISTORY_FILE = os.path.join(DATA_DIR, "review_history.json")
|
| 35 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
| 36 |
+
# Ensure cache directory has write permissions
|
| 37 |
+
os.chmod(CACHE_DIR, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
|
| 38 |
+
logger.info(f"Cache directory: {CACHE_DIR}")
|
| 39 |
+
logger.info(f"History file: {HISTORY_FILE}")
|
| 40 |
+
|
| 41 |
+
# Initialize empty history file if it doesn't exist
|
| 42 |
+
if not os.path.exists(HISTORY_FILE):
|
| 43 |
+
with open(HISTORY_FILE, 'w') as f:
|
| 44 |
+
json.dump({'history': [], 'metrics': {
|
| 45 |
+
'total_reviews': 0,
|
| 46 |
+
'avg_response_time': 0.0,
|
| 47 |
+
'reviews_today': 0
|
| 48 |
+
}}, f)
|
| 49 |
+
# Ensure history file has write permissions
|
| 50 |
+
os.chmod(HISTORY_FILE, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
|
| 51 |
+
logger.info("Created empty history file")
|
| 52 |
|
| 53 |
class Review:
|
| 54 |
def __init__(self, code: str, language: str, suggestions: str):
|
|
|
|
| 99 |
logger.info(f"Loaded {len(self.review_history)} reviews from history")
|
| 100 |
except Exception as e:
|
| 101 |
logger.error(f"Error loading history: {e}")
|
| 102 |
+
# Initialize empty history if file doesn't exist or is corrupted
|
| 103 |
+
self.review_history = []
|
| 104 |
+
self.metrics = {
|
| 105 |
+
'total_reviews': 0,
|
| 106 |
+
'avg_response_time': 0.0,
|
| 107 |
+
'reviews_today': 0
|
| 108 |
+
}
|
| 109 |
|
| 110 |
def save_history(self):
|
| 111 |
"""Save review history to file."""
|
|
|
|
| 114 |
'history': [r.to_dict() for r in self.review_history],
|
| 115 |
'metrics': self.metrics
|
| 116 |
}
|
| 117 |
+
# Ensure directory exists with proper permissions
|
| 118 |
+
os.makedirs(os.path.dirname(HISTORY_FILE), exist_ok=True)
|
| 119 |
+
os.chmod(os.path.dirname(HISTORY_FILE), stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
|
| 120 |
+
|
| 121 |
with open(HISTORY_FILE, 'w') as f:
|
| 122 |
json.dump(data, f)
|
| 123 |
+
# Ensure file has proper permissions
|
| 124 |
+
os.chmod(HISTORY_FILE, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
|
| 125 |
logger.info("Saved review history")
|
| 126 |
except Exception as e:
|
| 127 |
logger.error(f"Error saving history: {e}")
|
|
|
|
| 137 |
"""Initialize the model and tokenizer."""
|
| 138 |
try:
|
| 139 |
if HF_TOKEN:
|
| 140 |
+
login(token=HF_TOKEN, add_to_git_credential=True)
|
| 141 |
|
| 142 |
logger.info("Loading tokenizer...")
|
| 143 |
self.tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
| 443 |
|
| 444 |
# Launch the app
|
| 445 |
if __name__ == "__main__":
|
| 446 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|