QuantumChecker 0.2.8__tar.gz → 0.2.9__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. quantumchecker-0.2.9/PKG-INFO +53 -0
  2. quantumchecker-0.2.9/QuantumCheck/main.py +222 -0
  3. {quantumchecker-0.2.8 → quantumchecker-0.2.9}/QuantumCheck/powerbi_evaluator.py +7 -7
  4. {quantumchecker-0.2.8 → quantumchecker-0.2.9}/QuantumCheck/python_evaluator.py +57 -34
  5. {quantumchecker-0.2.8 → quantumchecker-0.2.9}/QuantumCheck/sql_evaluator.py +57 -34
  6. {quantumchecker-0.2.8 → quantumchecker-0.2.9}/QuantumCheck/ssis_evaluator.py +131 -20
  7. quantumchecker-0.2.9/QuantumChecker.egg-info/PKG-INFO +53 -0
  8. {quantumchecker-0.2.8 → quantumchecker-0.2.9}/QuantumChecker.egg-info/SOURCES.txt +2 -1
  9. quantumchecker-0.2.9/README.md +27 -0
  10. {quantumchecker-0.2.8 → quantumchecker-0.2.9}/setup.py +1 -1
  11. quantumchecker-0.2.9/tests/test.py +135 -0
  12. quantumchecker-0.2.9/tests/test2.py +30 -0
  13. quantumchecker-0.2.8/PKG-INFO +0 -138
  14. quantumchecker-0.2.8/QuantumCheck/main.py +0 -188
  15. quantumchecker-0.2.8/QuantumChecker.egg-info/PKG-INFO +0 -138
  16. quantumchecker-0.2.8/README.md +0 -112
  17. quantumchecker-0.2.8/tests/test.py +0 -388
  18. {quantumchecker-0.2.8 → quantumchecker-0.2.9}/QuantumCheck/__init__.py +0 -0
  19. {quantumchecker-0.2.8 → quantumchecker-0.2.9}/QuantumCheck/prompts.py +0 -0
  20. {quantumchecker-0.2.8 → quantumchecker-0.2.9}/QuantumChecker.egg-info/dependency_links.txt +0 -0
  21. {quantumchecker-0.2.8 → quantumchecker-0.2.9}/QuantumChecker.egg-info/requires.txt +0 -0
  22. {quantumchecker-0.2.8 → quantumchecker-0.2.9}/QuantumChecker.egg-info/top_level.txt +0 -0
  23. {quantumchecker-0.2.8 → quantumchecker-0.2.9}/setup.cfg +0 -0
@@ -0,0 +1,53 @@
1
+ Metadata-Version: 2.4
2
+ Name: QuantumChecker
3
+ Version: 0.2.9
4
+ Summary: A package to evaluate homework submissions in Python, SQL, PowerBI, and SSIS.
5
+ Author: Qobiljon
6
+ Author-email: qobiljonkhayrullayev@gmail.com
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Requires-Python: >=3.6
11
+ Description-Content-Type: text/markdown
12
+ Requires-Dist: requests>=2.31.0
13
+ Requires-Dist: tenacity>=8.2.3
14
+ Requires-Dist: pdf2image>=1.16.3
15
+ Requires-Dist: python-dotenv>=1.0.0
16
+ Requires-Dist: Pillow>=10.0.0
17
+ Requires-Dist: PyPDF2>=3.0.1
18
+ Dynamic: author
19
+ Dynamic: author-email
20
+ Dynamic: classifier
21
+ Dynamic: description
22
+ Dynamic: description-content-type
23
+ Dynamic: requires-dist
24
+ Dynamic: requires-python
25
+ Dynamic: summary
26
+
27
+ Sample usage:
28
+ ```
29
+ import asyncio
30
+ from your_evaluator_module import HomeworkEvaluator
31
+
32
+ async def main():
33
+ evaluator = HomeworkEvaluator()
34
+ question_content = """
35
+ Q1: What is a Python list? Explain with an example.
36
+
37
+ Q2: Write an SQL query to select all records from a table named 'students'.
38
+ """
39
+ answer_path = "sample_submissions/student1_answer.py"
40
+ api_keys = ["your_api_key_1", "your_api_key_2"]
41
+ question_type = "python"
42
+
43
+ result = await evaluator.evaluate_from_content(
44
+ question_content=question_content,
45
+ answer_path=answer_path,
46
+ api_keys=api_keys,
47
+ question_type=question_type
48
+ )
49
+ print(result)
50
+
51
+ if __name__ == "__main__":
52
+ asyncio.run(main())
53
+ ```
@@ -0,0 +1,222 @@
1
+ import logging
2
+ import os
3
+ import zipfile
4
+ import random
5
+ from datetime import datetime
6
+ from typing import List, Dict, Optional
7
+ from .python_evaluator import PythonEvaluator
8
+ from .sql_evaluator import SQLEvaluator
9
+ from .powerbi_evaluator import PowerBIEvaluator
10
+ from .ssis_evaluator import SSISEvaluator
11
+ import asyncio
12
+
13
+ _logger_cache = {}
14
+
15
+ class HomeworkEvaluator:
16
+ EVALUATOR_REGISTRY = {
17
+ "python": PythonEvaluator,
18
+ "sql": SQLEvaluator,
19
+ "powerbi": PowerBIEvaluator,
20
+ "ssis": SSISEvaluator
21
+ }
22
+
23
+ EXTENSION_TO_TYPE = {
24
+ ".py": "python",
25
+ ".sql": "sql",
26
+ ".pbit": "powerbi",
27
+ ".pdf": "powerbi",
28
+ ".dtsx": "ssis",
29
+ ".DTSX": "ssis",
30
+ ".txt": "text",
31
+ ".md": "text"
32
+ }
33
+
34
+ API_NAME_MAPPING = {
35
+ "python": "Google Gemini API",
36
+ "sql": "Google Gemini API",
37
+ "powerbi": "Google Gemini API",
38
+ "ssis": "Google Gemini API",
39
+ "text": "Google Gemini API"
40
+ }
41
+
42
+ def __init__(self, log_level: int = logging.INFO):
43
+ self.log_level = log_level
44
+ self._successful_key_cache = {}
45
+ self._rate_limit_delay = {} # Track delay per key
46
+
47
+ def _get_logger(self, log_type: str) -> logging.Logger:
48
+ log_name = f"{log_type}_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}"
49
+ if log_name not in _logger_cache:
50
+ logger = logging.getLogger(log_name)
51
+ logger.setLevel(self.log_level)
52
+ if not logger.handlers:
53
+ handler = logging.StreamHandler()
54
+ handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
55
+ logger.addHandler(handler)
56
+ _logger_cache[log_name] = logger
57
+ return _logger_cache[log_name]
58
+
59
+ def parse_questions(self, content: str) -> List[str]:
60
+ logger = self._get_logger("QuantumCheck.main")
61
+ questions = [q.strip() for q in content.split("\n\n") if q.strip()]
62
+ logger.info(f"Parsed {len(questions)} questions from content")
63
+ if not questions:
64
+ raise ValueError("No valid questions found in content")
65
+ return questions
66
+
67
+ def _detect_zip_content_type(self, zip_path: str, logger: logging.Logger) -> str:
68
+ try:
69
+ with zipfile.ZipFile(zip_path, "r") as zip_ref:
70
+ extensions = {os.path.splitext(name)[1].lower() for name in zip_ref.namelist()}
71
+ file_types = [self.EXTENSION_TO_TYPE.get(ext, "text") for ext in extensions if ext]
72
+ logger.info(f"Detected extensions in ZIP {zip_path}: {extensions}, types: {file_types}")
73
+ if "python" in file_types:
74
+ logger.info(f"Selected file type: python from extension: .py in ZIP: {zip_path}")
75
+ return "python"
76
+ elif "sql" in file_types:
77
+ logger.info(f"Selected file type: sql from extension: .sql in ZIP: {zip_path}")
78
+ return "sql"
79
+ elif "powerbi" in file_types:
80
+ logger.info(f"Selected file type: powerbi from extension: .pbit or .pdf in ZIP: {zip_path}")
81
+ return "powerbi"
82
+ elif "ssis" in file_types:
83
+ logger.info(f"Selected file type: ssis from extension: .dtsx in ZIP: {zip_path}")
84
+ return "ssis"
85
+ else:
86
+ logger.info(f"Selected file type: text (default) in ZIP: {zip_path}")
87
+ return "text"
88
+ except zipfile.BadZipFile:
89
+ logger.error(f"Invalid ZIP file: {zip_path}")
90
+ raise ValueError(f"Invalid ZIP file: {zip_path}")
91
+
92
+ async def evaluate_from_content(
93
+ self,
94
+ question_content: str,
95
+ answer_path: str,
96
+ api_keys: List[str],
97
+ question_type: str
98
+ ) -> Dict[str, any]:
99
+ try:
100
+ questions = self.parse_questions(question_content)
101
+ except ValueError as e:
102
+ logger = self._get_logger("QuantumCheck.main")
103
+ logger.error("Failed to parse question content: %s", str(e))
104
+ return {
105
+ "score": 0,
106
+ "feedback": f"Error parsing question content: {str(e)}",
107
+ "issues": [str(e)],
108
+ "recommendations": [],
109
+ "used_api_key_index": None,
110
+ "used_api_name": None
111
+ }
112
+
113
+ answer_path = answer_path.strip()
114
+ _, ext = os.path.splitext(answer_path)
115
+ ext = ext.lower()
116
+
117
+ # Determine file type, prioritizing question_type for evaluator selection
118
+ if ext == ".zip":
119
+ logger = self._get_logger("zip")
120
+ file_type = self._detect_zip_content_type(answer_path, logger)
121
+ else:
122
+ file_type = self.EXTENSION_TO_TYPE.get(ext, "text")
123
+ logger = self._get_logger(file_type)
124
+
125
+ # Use question_type if provided, else fallback to file_type
126
+ eval_type = question_type if question_type in self.EVALUATOR_REGISTRY else file_type
127
+ logger.info(f"Processing answer_path: {answer_path} with detected file type: {file_type}, evaluation type: {eval_type}")
128
+
129
+ if not os.path.exists(answer_path):
130
+ logger.error(f"Answer file not found: {answer_path}")
131
+ return {
132
+ "score": 0,
133
+ "feedback": f"Answer file not found: {answer_path}",
134
+ "issues": [f"Answer file not found: {answer_path}"],
135
+ "recommendations": [],
136
+ "used_api_key_index": None,
137
+ "used_api_name": None
138
+ }
139
+
140
+ evaluator_class = self.EVALUATOR_REGISTRY.get(eval_type, PythonEvaluator)
141
+ last_error_messages = []
142
+
143
+ # Shuffle keys for load balancing
144
+ key_order = [(i + 1, key) for i, key in enumerate(api_keys)]
145
+ random.shuffle(key_order)
146
+
147
+ # Try cached key with 30% probability to encourage rotation
148
+ cached_key_idx = self._successful_key_cache.get(eval_type)
149
+ if cached_key_idx is not None and cached_key_idx < len(api_keys) and random.random() < 0.3:
150
+ key_order.insert(0, (cached_key_idx + 1, api_keys[cached_key_idx]))
151
+
152
+ for idx, key in key_order:
153
+ # Check rate limit delay
154
+ if key in self._rate_limit_delay:
155
+ delay_until = self._rate_limit_delay[key]
156
+ current_time = datetime.now()
157
+ delay_until_time = datetime.fromtimestamp(delay_until)
158
+ if current_time < delay_until_time:
159
+ logger.info(f"API key #{idx} is rate-limited until {delay_until_time}, skipping.")
160
+ continue
161
+ else:
162
+ del self._rate_limit_delay[key]
163
+
164
+ logger.info(f"Trying API key #{idx}")
165
+ evaluator = evaluator_class(key)
166
+ api_name = getattr(evaluator, 'get_api_name', lambda: self.API_NAME_MAPPING.get(eval_type, "Unknown API"))()
167
+ logger.info(f"Using API: {api_name} for evaluation type: {eval_type}")
168
+
169
+ try:
170
+ evaluation = evaluator.evaluate(questions, answer_path, temp_dir=f"temp_extract_{os.getpid()}_{idx}")
171
+
172
+ feedback = evaluation.get("feedback", "").lower()
173
+ issues = " ".join(evaluation.get("issues", [])).lower()
174
+
175
+ # Check for invalid API key
176
+ if any(phrase in feedback or phrase in issues for phrase in ["api key not valid", "api_key_invalid"]):
177
+ logger.warning(f"API key #{idx} invalid, trying next key.")
178
+ last_error_messages.append(f"API key #{idx} invalid.")
179
+ continue
180
+
181
+ # Check for rate limit errors
182
+ if any(phrase in feedback or phrase in issues for phrase in ["429", "too many requests", "rate limit"]):
183
+ logger.warning(f"API key #{idx} hit rate limit, applying delay.")
184
+ last_error_messages.append(f"API key #{idx} rate limited.")
185
+ self._rate_limit_delay[key] = datetime.now().timestamp() + 45 # 45s delay
186
+ continue
187
+
188
+ # Check for invalid evaluation
189
+ if evaluation.get("score", 0) == 0 and "evaluation not returned" in feedback:
190
+ logger.warning(f"API key #{idx} returned invalid evaluation, trying next key.")
191
+ last_error_messages.append(f"API key #{idx} returned invalid evaluation.")
192
+ continue
193
+
194
+ # Cache successful key
195
+ self._successful_key_cache[eval_type] = idx - 1
196
+ logger.info(f"Evaluation succeeded with API key #{idx}: Score = {evaluation.get('score')}")
197
+
198
+ return {
199
+ "score": evaluation.get("score", 0),
200
+ "feedback": evaluation.get("feedback", "No feedback provided"),
201
+ "issues": evaluation.get("issues", []),
202
+ "recommendations": evaluation.get("recommendations", []),
203
+ "used_api_key_index": idx,
204
+ "used_api_name": api_name
205
+ }
206
+
207
+ except Exception as e:
208
+ logger.error(f"Exception using API key #{idx}: {str(e)}")
209
+ last_error_messages.append(f"Exception with key #{idx}: {str(e)}")
210
+ if "429" in str(e) or "rate limit" in str(e).lower():
211
+ self._rate_limit_delay[key] = datetime.now().timestamp() + 45
212
+ continue
213
+
214
+ logger.error("Evaluation failed with all API keys.")
215
+ return {
216
+ "score": 0,
217
+ "feedback": "Evaluation failed with all API keys.",
218
+ "issues": last_error_messages if last_error_messages else ["All API keys failed to evaluate the submission."],
219
+ "recommendations": [],
220
+ "used_api_key_index": None,
221
+ "used_api_name": None
222
+ }
@@ -285,11 +285,11 @@ class PowerBIEvaluator:
285
285
  self.model = GeminiFlashModel(api_key)
286
286
  self.processor = PowerBIProcessor()
287
287
 
288
- def evaluate(self, questions: List[str], answer_path: str) -> Dict[str, any]:
288
+ def evaluate(self, questions: List[str], answer_path: str, temp_dir: str = "temp_extract") -> Dict[str, any]:
289
289
  try:
290
290
  _, ext = os.path.splitext(answer_path)
291
291
  ext = ext.lower()
292
- extract_path = os.path.join(os.path.dirname(answer_path), "temp_extract")
292
+ extract_path = temp_dir
293
293
  pbit_path = None
294
294
  pdf_path = None
295
295
  if ext == ".zip":
@@ -322,8 +322,8 @@ class PowerBIEvaluator:
322
322
  }
323
323
  if pdf_path:
324
324
  try:
325
- self.processor.process_pdf(pdf_path)
326
- visual_result = self.model.evaluate_visuals(questions[0], "outputimages")
325
+ image_paths = self.processor.process_pdf(pdf_path, output_dir=os.path.join(temp_dir, "outputimages"))
326
+ visual_result = self.model.evaluate_visuals(questions[0], os.path.join(temp_dir, "outputimages"))
327
327
  result["score"] = int(0.7 * dax_result["score"] + 0.3 * visual_result["score"])
328
328
  result["visual_score"] = visual_result["score"]
329
329
  result["feedback"] += f"\n\nVisual Feedback:\n{visual_result['feedback']}"
@@ -344,10 +344,10 @@ class PowerBIEvaluator:
344
344
  logger.info("[Final] Score (70%% DAX, 30%% Visuals): %d/100", result["score"])
345
345
  return result
346
346
  finally:
347
- self.processor._cleanup(extract_path, "outputimages")
347
+ self.processor._cleanup(extract_path, os.path.join(temp_dir, "outputimages"))
348
348
  except Exception as e:
349
349
  logger.exception("Failed to evaluate Power BI file %s: %s", answer_path, str(e))
350
- self.processor._cleanup(extract_path, "outputimages")
350
+ self.processor._cleanup(extract_path, os.path.join(temp_dir, "outputimages"))
351
351
  return {
352
352
  "score": 0,
353
353
  "feedback": f"Error processing file: {str(e)}",
@@ -359,4 +359,4 @@ class PowerBIEvaluator:
359
359
 
360
360
 
361
361
  class ProcessingError(Exception):
362
- pass
362
+ pass
@@ -1,6 +1,7 @@
1
1
  import logging
2
2
  import os
3
3
  import zipfile
4
+ import shutil
4
5
  from pprint import pprint
5
6
  from typing import List, Dict
6
7
 
@@ -107,50 +108,60 @@ class PythonAnswerParser:
107
108
  return answers
108
109
 
109
110
  @staticmethod
110
- def parse_zip_file(zip_path: str) -> List[str]:
111
+ def parse_zip_file(zip_path: str, temp_dir: str) -> List[str]:
112
+ """
113
+ Parse Python files from a ZIP file, extracting to the specified temp_dir.
114
+
115
+ Args:
116
+ zip_path: Path to the ZIP file
117
+ temp_dir: Directory to extract ZIP contents
118
+
119
+ Returns:
120
+ List of answer strings extracted from Python files
121
+ """
111
122
  combined_content = []
112
123
 
113
124
  try:
125
+ # Create temporary extraction directory
126
+ os.makedirs(temp_dir, exist_ok=True)
114
127
  with zipfile.ZipFile(zip_path, "r") as zip_ref:
115
- temp_dir = "temp_python_extract"
116
- os.makedirs(temp_dir, exist_ok=True)
117
128
  zip_ref.extractall(temp_dir)
118
129
 
119
- python_files = sorted(
120
- [f for f in os.listdir(temp_dir) if f.endswith(".py")]
121
- )
130
+ python_files = sorted(
131
+ [f for f in os.listdir(temp_dir) if f.endswith(".py")]
132
+ )
122
133
 
123
- if not python_files:
124
- logger.warning("No Python files found in ZIP")
125
- return []
134
+ if not python_files:
135
+ logger.warning(f"No Python files found in ZIP: {zip_path}")
136
+ return []
126
137
 
127
- for python_file in python_files:
128
- with open(
138
+ for python_file in python_files:
139
+ with open(
129
140
  os.path.join(temp_dir, python_file),
130
141
  "r",
131
142
  encoding="utf-8",
132
- ) as f:
133
- content = f.read().strip()
134
- if content:
135
- combined_content.append(content)
136
-
137
- for f in os.listdir(temp_dir):
138
- os.remove(os.path.join(temp_dir, f))
143
+ ) as f:
144
+ content = f.read().strip()
145
+ if content:
146
+ combined_content.append(content)
139
147
 
140
- os.rmdir(temp_dir)
148
+ if not combined_content:
149
+ logger.warning(f"No valid content found in Python files in ZIP: {zip_path}")
150
+ return []
141
151
 
142
- if not combined_content:
143
- logger.warning("No valid content found in Python files")
144
- return []
145
-
146
- combined_text = "\n\n".join(combined_content)
147
- return [a.strip() for a in combined_text.split("\n\n") if a.strip()]
152
+ combined_text = "\n\n".join(combined_content)
153
+ return [a.strip() for a in combined_text.split("\n\n") if a.strip()]
148
154
  except zipfile.BadZipFile:
149
- logger.error("Invalid ZIP file: %s", zip_path)
155
+ logger.error(f"Invalid ZIP file: {zip_path}")
150
156
  return []
151
157
  except Exception as e:
152
- logger.error("Error processing ZIP file %s: %s", zip_path, str(e))
158
+ logger.error(f"Error processing ZIP file {zip_path}: {str(e)}")
153
159
  return []
160
+ finally:
161
+ # Clean up temporary directory
162
+ if os.path.exists(temp_dir):
163
+ shutil.rmtree(temp_dir, ignore_errors=True)
164
+ logger.info(f"Cleaned up temporary directory: {temp_dir}")
154
165
 
155
166
 
156
167
  class PythonEvaluator:
@@ -158,19 +169,30 @@ class PythonEvaluator:
158
169
  self.api_key = api_key
159
170
  self.model = GeminiFlashModel(api_key)
160
171
 
161
- def evaluate(self, questions: List[str], answer_path: str) -> Dict[str, any]:
172
+ def evaluate(self, questions: List[str], answer_path: str, temp_dir: str = None) -> Dict[str, any]:
173
+ """
174
+ Evaluate a Python submission.
175
+
176
+ Args:
177
+ questions: List of questions to evaluate against
178
+ answer_path: Path to the answer file (ZIP or single file)
179
+ temp_dir: Optional directory for temporary ZIP extraction
180
+
181
+ Returns:
182
+ Dictionary containing score, feedback, issues, and recommendations
183
+ """
162
184
  try:
163
185
  if answer_path.endswith(".zip"):
164
- answers = PythonAnswerParser.parse_zip_file(answer_path)
186
+ # Use provided temp_dir or generate a default one
187
+ temp_dir = temp_dir or f"temp_python_extract_{os.getpid()}"
188
+ answers = PythonAnswerParser.parse_zip_file(answer_path, temp_dir)
165
189
  else:
166
190
  with open(answer_path, "r", encoding="utf-8") as file:
167
191
  content = file.read()
168
192
  answers = PythonAnswerParser.parse_single_file(content)
169
193
 
170
194
  logger.info(
171
- "Processing %d questions and %d answers",
172
- len(questions),
173
- len(answers),
195
+ f"Processing {len(questions)} questions and {len(answers)} answers"
174
196
  )
175
197
  pprint(f"Processing {len(questions)} questions and {len(answers)} answers")
176
198
 
@@ -186,9 +208,10 @@ class PythonEvaluator:
186
208
 
187
209
  return self.model.evaluate(combined_raw_content)
188
210
  except Exception as e:
189
- logger.error("Failed to process answers from %s: %s", answer_path, str(e))
211
+ logger.error(f"Failed to process answers from {answer_path}: {str(e)}")
190
212
  return {
191
213
  "score": 0,
192
214
  "feedback": f"Error processing answers: {str(e)}",
193
215
  "issues": [str(e)],
194
- }
216
+ "recommendations": []
217
+ }
@@ -1,6 +1,7 @@
1
1
  import logging
2
2
  import os
3
3
  import zipfile
4
+ import shutil
4
5
  from pprint import pprint
5
6
  from typing import List, Dict
6
7
 
@@ -107,50 +108,60 @@ class SQLAnswerParser:
107
108
  return answers
108
109
 
109
110
  @staticmethod
110
- def parse_zip_file(zip_path: str) -> List[str]:
111
+ def parse_zip_file(zip_path: str, temp_dir: str) -> List[str]:
112
+ """
113
+ Parse SQL files from a ZIP file, extracting to the specified temp_dir.
114
+
115
+ Args:
116
+ zip_path: Path to the ZIP file
117
+ temp_dir: Directory to extract ZIP contents
118
+
119
+ Returns:
120
+ List of answer strings extracted from SQL files
121
+ """
111
122
  combined_content = []
112
123
 
113
124
  try:
125
+ # Create temporary extraction directory
126
+ os.makedirs(temp_dir, exist_ok=True)
114
127
  with zipfile.ZipFile(zip_path, "r") as zip_ref:
115
- temp_dir = "temp_sql_extract"
116
- os.makedirs(temp_dir, exist_ok=True)
117
128
  zip_ref.extractall(temp_dir)
118
129
 
119
- sql_files = sorted(
120
- [f for f in os.listdir(temp_dir) if f.endswith(".sql")]
121
- )
130
+ sql_files = sorted(
131
+ [f for f in os.listdir(temp_dir) if f.endswith(".sql")]
132
+ )
122
133
 
123
- if not sql_files:
124
- logger.warning("No SQL files found in ZIP")
125
- return []
134
+ if not sql_files:
135
+ logger.warning(f"No SQL files found in ZIP: {zip_path}")
136
+ return []
126
137
 
127
- for sql_file in sql_files:
128
- with open(
138
+ for sql_file in sql_files:
139
+ with open(
129
140
  os.path.join(temp_dir, sql_file),
130
141
  "r",
131
142
  encoding="utf-8",
132
- ) as f:
133
- content = f.read().strip()
134
- if content:
135
- combined_content.append(content)
136
-
137
- for f in os.listdir(temp_dir):
138
- os.remove(os.path.join(temp_dir, f))
143
+ ) as f:
144
+ content = f.read().strip()
145
+ if content:
146
+ combined_content.append(content)
139
147
 
140
- os.rmdir(temp_dir)
148
+ if not combined_content:
149
+ logger.warning(f"No valid content found in SQL files in ZIP: {zip_path}")
150
+ return []
141
151
 
142
- if not combined_content:
143
- logger.warning("No valid content found in SQL files")
144
- return []
145
-
146
- combined_text = "\n\n".join(combined_content)
147
- return [a.strip() for a in combined_text.split("\n\n") if a.strip()]
152
+ combined_text = "\n\n".join(combined_content)
153
+ return [a.strip() for a in combined_text.split("\n\n") if a.strip()]
148
154
  except zipfile.BadZipFile:
149
- logger.error("Invalid ZIP file: %s", zip_path)
155
+ logger.error(f"Invalid ZIP file: {zip_path}")
150
156
  return []
151
157
  except Exception as e:
152
- logger.error("Error processing ZIP file %s: %s", zip_path, str(e))
158
+ logger.error(f"Error processing ZIP file {zip_path}: {str(e)}")
153
159
  return []
160
+ finally:
161
+ # Clean up temporary directory
162
+ if os.path.exists(temp_dir):
163
+ shutil.rmtree(temp_dir, ignore_errors=True)
164
+ logger.info(f"Cleaned up temporary directory: {temp_dir}")
154
165
 
155
166
 
156
167
  class SQLEvaluator:
@@ -158,19 +169,30 @@ class SQLEvaluator:
158
169
  self.api_key = api_key
159
170
  self.model = GeminiFlashModel(api_key)
160
171
 
161
- def evaluate(self, questions: List[str], answer_path: str) -> Dict[str, any]:
172
+ def evaluate(self, questions: List[str], answer_path: str, temp_dir: str = None) -> Dict[str, any]:
173
+ """
174
+ Evaluate an SQL submission.
175
+
176
+ Args:
177
+ questions: List of questions to evaluate against
178
+ answer_path: Path to the answer file (ZIP or single file)
179
+ temp_dir: Optional directory for temporary ZIP extraction
180
+
181
+ Returns:
182
+ Dictionary containing score, feedback, issues, and recommendations
183
+ """
162
184
  try:
163
185
  if answer_path.endswith(".zip"):
164
- answers = SQLAnswerParser.parse_zip_file(answer_path)
186
+ # Use provided temp_dir or generate a default one
187
+ temp_dir = temp_dir or f"temp_sql_extract_{os.getpid()}"
188
+ answers = SQLAnswerParser.parse_zip_file(answer_path, temp_dir)
165
189
  else:
166
190
  with open(answer_path, "r", encoding="utf-8") as file:
167
191
  content = file.read()
168
192
  answers = SQLAnswerParser.parse_single_file(content)
169
193
 
170
194
  logger.info(
171
- "Processing %d questions and %d answers",
172
- len(questions),
173
- len(answers),
195
+ f"Processing {len(questions)} questions and {len(answers)} answers"
174
196
  )
175
197
  pprint(f"Processing {len(questions)} questions and {len(answers)} answers")
176
198
 
@@ -188,9 +210,10 @@ class SQLEvaluator:
188
210
 
189
211
  return self.model.evaluate(combined_raw_content)
190
212
  except Exception as e:
191
- logger.error("Failed to process answers from %s: %s", answer_path, str(e))
213
+ logger.error(f"Failed to process answers from {answer_path}: {str(e)}")
192
214
  return {
193
215
  "score": 0,
194
216
  "feedback": f"Error processing answers: {str(e)}",
195
217
  "issues": [str(e)],
196
- }
218
+ "recommendations": []
219
+ }