Spaces:
Sleeping
Sleeping
Sgridda
commited on
Commit
·
4eca884
1
Parent(s):
798c1c2
updated model config and added logs
Browse files
main.py
CHANGED
|
@@ -85,20 +85,31 @@ def run_ai_inference(diff: str) -> str:
|
|
| 85 |
if not model or not tokenizer:
|
| 86 |
raise RuntimeError("Model is not loaded.")
|
| 87 |
|
|
|
|
| 88 |
messages = [
|
| 89 |
{
|
| 90 |
"role": "system",
|
| 91 |
-
"content": "
|
| 92 |
},
|
| 93 |
{
|
| 94 |
"role": "user",
|
| 95 |
-
"content": f"
|
| 96 |
}
|
| 97 |
]
|
| 98 |
|
| 99 |
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
|
| 100 |
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
response_text = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
|
| 104 |
return response_text.strip()
|
|
@@ -109,10 +120,15 @@ def parse_ai_response(response_text: str) -> list[ReviewComment]:
|
|
| 109 |
"""
|
| 110 |
print(f"Raw AI Response:\n---\n{response_text}\n---")
|
| 111 |
|
| 112 |
-
json_match = re.search(r'\[
|
| 113 |
if not json_match:
|
| 114 |
print("Warning: Could not find a JSON array in the AI response.")
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
json_string = json_match.group(0)
|
| 118 |
|
|
@@ -123,7 +139,12 @@ def parse_ai_response(response_text: str) -> list[ReviewComment]:
|
|
| 123 |
except (json.JSONDecodeError, TypeError, KeyError) as e:
|
| 124 |
print(f"Error parsing JSON from AI response: {e}")
|
| 125 |
print(f"Invalid JSON string: {json_string}")
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
# ----------------------------
|
| 129 |
# 6. The API Endpoint
|
|
@@ -134,13 +155,23 @@ async def get_code_review(request: ReviewRequest):
|
|
| 134 |
if not request.diff:
|
| 135 |
raise HTTPException(status_code=400, detail="Diff content cannot be empty.")
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
try:
|
|
|
|
| 138 |
ai_response_text = run_ai_inference(request.diff)
|
|
|
|
|
|
|
|
|
|
| 139 |
parsed_comments = parse_ai_response(ai_response_text)
|
|
|
|
|
|
|
| 140 |
return ReviewResponse(comments=parsed_comments)
|
| 141 |
|
| 142 |
except Exception as e:
|
| 143 |
-
print(f"An unexpected error occurred: {e}")
|
| 144 |
raise HTTPException(status_code=500, detail="An internal error occurred while processing the review.")
|
| 145 |
|
| 146 |
# ----------------------------
|
|
|
|
| 85 |
if not model or not tokenizer:
|
| 86 |
raise RuntimeError("Model is not loaded.")
|
| 87 |
|
| 88 |
+
# Simplified, shorter prompt for faster inference
|
| 89 |
messages = [
|
| 90 |
{
|
| 91 |
"role": "system",
|
| 92 |
+
"content": "You are a code reviewer. Analyze the diff and return a JSON array of comments with file_path, line_number, and comment_text fields. Be concise."
|
| 93 |
},
|
| 94 |
{
|
| 95 |
"role": "user",
|
| 96 |
+
"content": f"Review this diff:\n{diff[:1000]}" # Limit diff size for faster processing
|
| 97 |
}
|
| 98 |
]
|
| 99 |
|
| 100 |
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
|
| 101 |
|
| 102 |
+
# Optimized generation parameters for speed
|
| 103 |
+
outputs = model.generate(
|
| 104 |
+
inputs,
|
| 105 |
+
max_new_tokens=256, # Reduced from 1024
|
| 106 |
+
do_sample=False,
|
| 107 |
+
temperature=0.1, # Lower temperature for more focused output
|
| 108 |
+
num_return_sequences=1,
|
| 109 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 110 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 111 |
+
use_cache=True # Enable KV cache for faster generation
|
| 112 |
+
)
|
| 113 |
|
| 114 |
response_text = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
|
| 115 |
return response_text.strip()
|
|
|
|
| 120 |
"""
|
| 121 |
print(f"Raw AI Response:\n---\n{response_text}\n---")
|
| 122 |
|
| 123 |
+
json_match = re.search(r'\[.*?\]', response_text, re.DOTALL)
|
| 124 |
if not json_match:
|
| 125 |
print("Warning: Could not find a JSON array in the AI response.")
|
| 126 |
+
# Return a simple fallback comment
|
| 127 |
+
return [ReviewComment(
|
| 128 |
+
file_path="unknown",
|
| 129 |
+
line_number=1,
|
| 130 |
+
comment_text="AI review completed - no specific issues found."
|
| 131 |
+
)]
|
| 132 |
|
| 133 |
json_string = json_match.group(0)
|
| 134 |
|
|
|
|
| 139 |
except (json.JSONDecodeError, TypeError, KeyError) as e:
|
| 140 |
print(f"Error parsing JSON from AI response: {e}")
|
| 141 |
print(f"Invalid JSON string: {json_string}")
|
| 142 |
+
# Return a simple fallback comment
|
| 143 |
+
return [ReviewComment(
|
| 144 |
+
file_path="unknown",
|
| 145 |
+
line_number=1,
|
| 146 |
+
comment_text="AI review completed - response format issue."
|
| 147 |
+
)]
|
| 148 |
|
| 149 |
# ----------------------------
|
| 150 |
# 6. The API Endpoint
|
|
|
|
| 155 |
if not request.diff:
|
| 156 |
raise HTTPException(status_code=400, detail="Diff content cannot be empty.")
|
| 157 |
|
| 158 |
+
import time
|
| 159 |
+
start_time = time.time()
|
| 160 |
+
print(f"Starting review request at {start_time}")
|
| 161 |
+
|
| 162 |
try:
|
| 163 |
+
print("Running AI inference...")
|
| 164 |
ai_response_text = run_ai_inference(request.diff)
|
| 165 |
+
print(f"AI inference completed in {time.time() - start_time:.2f} seconds")
|
| 166 |
+
|
| 167 |
+
print("Parsing AI response...")
|
| 168 |
parsed_comments = parse_ai_response(ai_response_text)
|
| 169 |
+
print(f"Total processing time: {time.time() - start_time:.2f} seconds")
|
| 170 |
+
|
| 171 |
return ReviewResponse(comments=parsed_comments)
|
| 172 |
|
| 173 |
except Exception as e:
|
| 174 |
+
print(f"An unexpected error occurred after {time.time() - start_time:.2f} seconds: {e}")
|
| 175 |
raise HTTPException(status_code=500, detail="An internal error occurred while processing the review.")
|
| 176 |
|
| 177 |
# ----------------------------
|