Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import os
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 6 |
|
| 7 |
-
class
|
| 8 |
def __init__(self):
|
| 9 |
self.feedback_file = "user_feedback.json"
|
| 10 |
self.model = None
|
|
@@ -15,20 +16,24 @@ class SmartCodeFixer:
|
|
| 15 |
def load_model(self):
|
| 16 |
"""加载预训练模型"""
|
| 17 |
try:
|
| 18 |
-
# 使用
|
| 19 |
-
model_name = "microsoft/DialoGPT-medium"
|
| 20 |
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 21 |
self.model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 22 |
-
print("模型加载成功!")
|
| 23 |
except Exception as e:
|
| 24 |
-
print(f"模型加载失败: {e}")
|
| 25 |
self.model = None
|
| 26 |
|
| 27 |
def load_feedback_data(self):
|
| 28 |
"""加载用户反馈数据用于学习"""
|
| 29 |
if os.path.exists(self.feedback_file):
|
| 30 |
-
|
| 31 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
else:
|
| 33 |
self.feedback_data = []
|
| 34 |
|
|
@@ -49,21 +54,25 @@ class SmartCodeFixer:
|
|
| 49 |
with open(self.feedback_file, 'w', encoding='utf-8') as f:
|
| 50 |
json.dump(self.feedback_data, f, ensure_ascii=False, indent=2)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
if len(self.feedback_data) %
|
| 54 |
self.retrain_from_feedback()
|
| 55 |
|
| 56 |
def detect_language(self, code):
|
| 57 |
"""智能检测编程语言"""
|
| 58 |
-
code_lower = code.lower()
|
| 59 |
|
| 60 |
language_indicators = {
|
| 61 |
-
'html': ['<!doctype', '<html', '<div', '<span', 'class="', 'id="'],
|
| 62 |
-
'python': ['def ', 'import ', 'print(', 'if __name__', 'lambda '],
|
| 63 |
-
'javascript': ['function ', 'console.log', 'document.', 'addEventListener'],
|
| 64 |
-
'java': ['public class', 'public static', '
|
| 65 |
-
'cpp': ['#include', 'using namespace', 'cout <<', 'std::'],
|
| 66 |
-
'css': ['{', '}', ':', ';', 'font-size', 'color:']
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
}
|
| 68 |
|
| 69 |
scores = {lang: 0 for lang in language_indicators}
|
|
@@ -73,18 +82,34 @@ class SmartCodeFixer:
|
|
| 73 |
if indicator in code_lower:
|
| 74 |
scores[lang] += 1
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
def ai_fix_code(self, code, language):
|
| 79 |
"""使用AI模型修复代码"""
|
| 80 |
if self.model is None:
|
| 81 |
-
return self.
|
| 82 |
|
| 83 |
try:
|
| 84 |
-
# 构建修复提示
|
| 85 |
-
prompt = f"""修复以下{language}代码的错误:
|
| 86 |
|
| 87 |
-
|
| 88 |
```{language}
|
| 89 |
{code}
|
| 90 |
"""
|
|
@@ -92,228 +117,604 @@ class SmartCodeFixer:
|
|
| 92 |
inputs = self.tokenizer.encode(prompt, return_tensors="pt")
|
| 93 |
outputs = self.model.generate(
|
| 94 |
inputs,
|
| 95 |
-
max_length=len(inputs[0]) +
|
| 96 |
num_return_sequences=1,
|
| 97 |
-
temperature=0.
|
| 98 |
-
do_sample=True
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 102 |
# 提取修复后的代码
|
| 103 |
if "```" in response:
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
else:
|
| 106 |
fixed_code = response.replace(prompt, "").strip()
|
| 107 |
|
| 108 |
-
return fixed_code
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
-
print(f"AI修复失败: {e}")
|
| 112 |
-
return self.
|
| 113 |
|
| 114 |
-
def
|
| 115 |
-
"""基于规则的代码修复"""
|
| 116 |
-
fixes =
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
}
|
| 124 |
|
| 125 |
-
fix_function =
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
def
|
| 129 |
-
"""修复
|
| 130 |
fixes = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
|
|
|
| 136 |
|
| 137 |
-
#
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
# 添加基本的HTML结构
|
| 145 |
-
if '<!DOCTYPE html>' not in
|
| 146 |
-
|
| 147 |
-
<html>
|
|
|
|
| 148 |
<head>
|
| 149 |
<meta charset="UTF-8">
|
|
|
|
| 150 |
<title>Document</title>
|
| 151 |
</head>
|
| 152 |
<body>
|
| 153 |
-
{
|
| 154 |
</body>
|
| 155 |
</html>"""
|
| 156 |
-
|
| 157 |
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
-
def
|
| 161 |
-
"""修复
|
| 162 |
fixes = []
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
-
|
| 175 |
-
code += "'"
|
| 176 |
-
fixes.append("修复了不匹配的单引号")
|
| 177 |
|
| 178 |
-
# 修复
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
lines[i] = line.rstrip() + ':'
|
| 183 |
-
fixes.append("在条件/函数声明后添加了冒号")
|
| 184 |
|
| 185 |
-
return
|
| 186 |
|
| 187 |
-
def
|
| 188 |
-
"""修复
|
| 189 |
fixes = []
|
|
|
|
|
|
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
-
|
| 197 |
-
if code.count('{') > code.count('}'):
|
| 198 |
-
code += '}' * (code.count('{') - code.count('}'))
|
| 199 |
-
fixes.append("修复了不匹配的花括号")
|
| 200 |
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
def
|
| 204 |
-
"""修复
|
| 205 |
fixes = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
# 修复选择器
|
| 208 |
-
if ':' in
|
| 209 |
-
|
| 210 |
-
fixes.append("
|
| 211 |
|
| 212 |
-
return
|
| 213 |
|
| 214 |
-
def
|
| 215 |
-
"""修复
|
| 216 |
fixes = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
# 添加基本的类结构
|
| 219 |
-
if 'public class' in
|
| 220 |
-
|
| 221 |
-
fixes.append("添加了
|
| 222 |
|
| 223 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
-
def
|
| 226 |
-
"""修复
|
| 227 |
fixes = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
fixes.append("添加了main函数")
|
| 232 |
|
| 233 |
-
return
|
| 234 |
|
| 235 |
-
def
|
| 236 |
-
"""通用修复"""
|
| 237 |
-
fixes = ["进行了通用语法检查"]
|
| 238 |
return code, fixes
|
| 239 |
|
| 240 |
def retrain_from_feedback(self):
|
| 241 |
-
"""根据用户反馈重新训练模型
|
| 242 |
-
print("正在从
|
| 243 |
-
# 这里可以添加增量学习
|
| 244 |
-
# 目前先记录
|
| 245 |
|
| 246 |
# 创建修复器实例
|
| 247 |
-
fixer =
|
| 248 |
|
| 249 |
def process_code(input_code, use_ai=True):
|
| 250 |
"""处理代码修复"""
|
| 251 |
-
|
|
|
|
| 252 |
|
| 253 |
-
|
| 254 |
-
fixed_code = fixer.ai_fix_code(input_code, language)
|
| 255 |
-
fixes = ["使用AI模型修复"]
|
| 256 |
-
else:
|
| 257 |
-
fixed_code, fixes = fixer.rule_based_fix(input_code, language)
|
| 258 |
-
|
| 259 |
-
# 生成修复报告
|
| 260 |
-
report = f"""🔧 修复报告
|
| 261 |
-
📝 检测语言: {language}
|
| 262 |
-
✅ 修复内容: {', '.join(fixes) if fixes else '代码看起来没问题'}
|
| 263 |
-
|
| 264 |
-
修复后的代码:"""
|
| 265 |
|
| 266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
|
| 268 |
def handle_feedback(original_code, fixed_code, user_feedback, is_correct):
|
| 269 |
"""处理用户反馈"""
|
|
|
|
|
|
|
|
|
|
| 270 |
fixer.save_feedback(original_code, fixed_code, user_feedback, is_correct)
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
# 创建Gradio界面
|
| 274 |
-
with gr.Blocks(
|
| 275 |
-
gr.
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
-
with gr.Tab("代码修复"):
|
| 279 |
with gr.Row():
|
| 280 |
-
with gr.Column():
|
|
|
|
| 281 |
input_code = gr.Textbox(
|
| 282 |
-
label="
|
| 283 |
-
placeholder="粘贴你的代码到这里...",
|
| 284 |
-
lines=
|
|
|
|
| 285 |
)
|
| 286 |
-
|
| 287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
-
with gr.Column():
|
|
|
|
| 290 |
output_code = gr.Textbox(
|
| 291 |
-
label="
|
| 292 |
-
lines=
|
| 293 |
show_copy_button=True
|
| 294 |
)
|
| 295 |
report = gr.Textbox(
|
| 296 |
-
label="📊 修复报告",
|
| 297 |
-
lines=
|
|
|
|
| 298 |
)
|
| 299 |
|
| 300 |
-
with gr.Tab("反馈学习"):
|
| 301 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
with gr.Row():
|
| 303 |
with gr.Column():
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
)
|
| 315 |
-
|
| 316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
|
| 318 |
# 事件处理
|
| 319 |
fix_btn.click(
|
|
@@ -328,16 +729,25 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 328 |
outputs=[feedback_result]
|
| 329 |
)
|
| 330 |
|
| 331 |
-
# 示例
|
| 332 |
-
gr.Markdown("## 🎯 试试这些例子:")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
gr.Examples(
|
| 334 |
-
examples=
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
["function test() {", True], # JavaScript
|
| 338 |
-
["public class MyClass", True], # Java
|
| 339 |
-
],
|
| 340 |
-
inputs=[input_code, use_ai]
|
| 341 |
)
|
| 342 |
|
| 343 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
+
import re
|
| 5 |
from datetime import datetime
|
| 6 |
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 7 |
|
| 8 |
+
class AdvancedCodeFixer:
|
| 9 |
def __init__(self):
|
| 10 |
self.feedback_file = "user_feedback.json"
|
| 11 |
self.model = None
|
|
|
|
| 16 |
def load_model(self):
|
| 17 |
"""加载预训练模型"""
|
| 18 |
try:
|
| 19 |
+
# 使用轻量级但效果不错的模型
|
| 20 |
+
model_name = "microsoft/DialoGPT-medium"
|
| 21 |
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 22 |
self.model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 23 |
+
print("🤖 AI模型加载成功!")
|
| 24 |
except Exception as e:
|
| 25 |
+
print(f"❌ 模型加载失败: {e}")
|
| 26 |
self.model = None
|
| 27 |
|
| 28 |
def load_feedback_data(self):
|
| 29 |
"""加载用户反馈数据用于学习"""
|
| 30 |
if os.path.exists(self.feedback_file):
|
| 31 |
+
try:
|
| 32 |
+
with open(self.feedback_file, 'r', encoding='utf-8') as f:
|
| 33 |
+
self.feedback_data = json.load(f)
|
| 34 |
+
print(f"📚 已加载 {len(self.feedback_data)} 条反馈数据")
|
| 35 |
+
except:
|
| 36 |
+
self.feedback_data = []
|
| 37 |
else:
|
| 38 |
self.feedback_data = []
|
| 39 |
|
|
|
|
| 54 |
with open(self.feedback_file, 'w', encoding='utf-8') as f:
|
| 55 |
json.dump(self.feedback_data, f, ensure_ascii=False, indent=2)
|
| 56 |
|
| 57 |
+
# 每5个反馈重新学习一次
|
| 58 |
+
if len(self.feedback_data) % 5 == 0:
|
| 59 |
self.retrain_from_feedback()
|
| 60 |
|
| 61 |
def detect_language(self, code):
|
| 62 |
"""智能检测编程语言"""
|
| 63 |
+
code_lower = code.lower().strip()
|
| 64 |
|
| 65 |
language_indicators = {
|
| 66 |
+
'html': ['<!doctype', '<html', '<div', '<span', '<body', '<head', '<title', 'class="', 'id="'],
|
| 67 |
+
'python': ['def ', 'import ', 'print(', 'if __name__', 'lambda ', 'range(', 'len('],
|
| 68 |
+
'javascript': ['function ', 'console.log', 'document.', 'addEventListener', 'const ', 'let ', '=>'],
|
| 69 |
+
'java': ['public class', 'public static', 'system.out.println', 'void main'],
|
| 70 |
+
'cpp': ['#include', 'using namespace', 'cout <<', 'std::', 'int main()'],
|
| 71 |
+
'css': ['{', '}', ':', ';', 'font-size', 'color:', 'background:'],
|
| 72 |
+
'php': ['<?php', '$_get', '$_post', 'echo '],
|
| 73 |
+
'sql': ['select ', 'from ', 'where ', 'insert into', 'update '],
|
| 74 |
+
'ruby': ['def ', 'end', 'puts ', 'class '],
|
| 75 |
+
'go': ['package ', 'import "', 'func main()', 'fmt.println']
|
| 76 |
}
|
| 77 |
|
| 78 |
scores = {lang: 0 for lang in language_indicators}
|
|
|
|
| 82 |
if indicator in code_lower:
|
| 83 |
scores[lang] += 1
|
| 84 |
|
| 85 |
+
# 如果分数相同,优先选择更具体的语言
|
| 86 |
+
detected_lang = max(scores.items(), key=lambda x: x[1])[0]
|
| 87 |
+
|
| 88 |
+
# 如果所有分数都为0,根据内容特征猜测
|
| 89 |
+
if scores[detected_lang] == 0:
|
| 90 |
+
if '<' in code and '>' in code:
|
| 91 |
+
return 'html'
|
| 92 |
+
elif 'def ' in code or 'import ' in code:
|
| 93 |
+
return 'python'
|
| 94 |
+
elif 'function ' in code or 'console.log' in code:
|
| 95 |
+
return 'javascript'
|
| 96 |
+
elif 'public class' in code:
|
| 97 |
+
return 'java'
|
| 98 |
+
else:
|
| 99 |
+
return 'text'
|
| 100 |
+
|
| 101 |
+
return detected_lang
|
| 102 |
|
| 103 |
def ai_fix_code(self, code, language):
|
| 104 |
"""使用AI模型修复代码"""
|
| 105 |
if self.model is None:
|
| 106 |
+
return self.advanced_rule_based_fix(code, language)
|
| 107 |
|
| 108 |
try:
|
| 109 |
+
# 构建更详细的修复提示
|
| 110 |
+
prompt = f"""请修复以下{language}代码��的语法错误和常见问题:
|
| 111 |
|
| 112 |
+
有问题的代码:
|
| 113 |
```{language}
|
| 114 |
{code}
|
| 115 |
"""
|
|
|
|
| 117 |
inputs = self.tokenizer.encode(prompt, return_tensors="pt")
|
| 118 |
outputs = self.model.generate(
|
| 119 |
inputs,
|
| 120 |
+
max_length=min(len(inputs[0]) + 200, 1024),
|
| 121 |
num_return_sequences=1,
|
| 122 |
+
temperature=0.3,
|
| 123 |
+
do_sample=True,
|
| 124 |
+
pad_token_id=self.tokenizer.eos_token_id
|
| 125 |
)
|
| 126 |
|
| 127 |
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 128 |
+
|
| 129 |
# 提取修复后的代码
|
| 130 |
if "```" in response:
|
| 131 |
+
parts = response.split("```")
|
| 132 |
+
if len(parts) >= 3:
|
| 133 |
+
fixed_code = parts[2].strip()
|
| 134 |
+
else:
|
| 135 |
+
fixed_code = parts[-1].strip()
|
| 136 |
else:
|
| 137 |
fixed_code = response.replace(prompt, "").strip()
|
| 138 |
|
| 139 |
+
return fixed_code, ["使用AI智能修复"]
|
| 140 |
|
| 141 |
except Exception as e:
|
| 142 |
+
print(f"🤖 AI修复失败: {e}")
|
| 143 |
+
return self.advanced_rule_based_fix(code, language)
|
| 144 |
|
| 145 |
+
def advanced_rule_based_fix(self, code, language):
|
| 146 |
+
"""基于规则的智能代码修复"""
|
| 147 |
+
fixes = []
|
| 148 |
+
fixed_code = code
|
| 149 |
+
|
| 150 |
+
# 通用修复(适用于所有语言)
|
| 151 |
+
fixed_code, general_fixes = self.apply_general_fixes(fixed_code)
|
| 152 |
+
fixes.extend(general_fixes)
|
| 153 |
+
|
| 154 |
+
# 语言特定修复
|
| 155 |
+
language_fix_functions = {
|
| 156 |
+
'html': self.fix_html_advanced,
|
| 157 |
+
'python': self.fix_python_advanced,
|
| 158 |
+
'javascript': self.fix_javascript_advanced,
|
| 159 |
+
'java': self.fix_java_advanced,
|
| 160 |
+
'cpp': self.fix_cpp_advanced,
|
| 161 |
+
'css': self.fix_css_advanced,
|
| 162 |
+
'php': self.fix_php_advanced,
|
| 163 |
+
'sql': self.fix_sql_advanced,
|
| 164 |
+
'ruby': self.fix_ruby_advanced,
|
| 165 |
+
'go': self.fix_go_advanced
|
| 166 |
}
|
| 167 |
|
| 168 |
+
fix_function = language_fix_functions.get(language, self.fix_generic_advanced)
|
| 169 |
+
fixed_code, language_fixes = fix_function(fixed_code)
|
| 170 |
+
fixes.extend(language_fixes)
|
| 171 |
+
|
| 172 |
+
return fixed_code, fixes
|
| 173 |
|
| 174 |
+
def apply_general_fixes(self, code):
|
| 175 |
+
"""应用通用修复规则"""
|
| 176 |
fixes = []
|
| 177 |
+
fixed_code = code
|
| 178 |
+
|
| 179 |
+
# 修复不匹配的括号
|
| 180 |
+
paren_diff = fixed_code.count('(') - fixed_code.count(')')
|
| 181 |
+
if paren_diff > 0:
|
| 182 |
+
fixed_code += ')' * paren_diff
|
| 183 |
+
fixes.append(f"添加了 {paren_diff} 个缺失的右括号")
|
| 184 |
+
|
| 185 |
+
# 修复不匹配的方括号
|
| 186 |
+
bracket_diff = fixed_code.count('[') - fixed_code.count(']')
|
| 187 |
+
if bracket_diff > 0:
|
| 188 |
+
fixed_code += ']' * bracket_diff
|
| 189 |
+
fixes.append(f"添加了 {bracket_diff} 个缺失的右方括号")
|
| 190 |
|
| 191 |
+
# 修复不匹配的花括号
|
| 192 |
+
brace_diff = fixed_code.count('{') - fixed_code.count('}')
|
| 193 |
+
if brace_diff > 0:
|
| 194 |
+
fixed_code += '}' * brace_diff
|
| 195 |
+
fixes.append(f"添加了 {brace_diff} 个缺失的右花括号")
|
| 196 |
|
| 197 |
+
# 修复不匹配的引号
|
| 198 |
+
single_quote_pairs = fixed_code.count("'") % 2
|
| 199 |
+
double_quote_pairs = fixed_code.count('"') % 2
|
| 200 |
+
|
| 201 |
+
if single_quote_pairs != 0:
|
| 202 |
+
fixed_code += "'"
|
| 203 |
+
fixes.append("修复了不匹配的单引号")
|
| 204 |
+
|
| 205 |
+
if double_quote_pairs != 0:
|
| 206 |
+
fixed_code += '"'
|
| 207 |
+
fixes.append("修复了不匹配的双引号")
|
| 208 |
+
|
| 209 |
+
return fixed_code, fixes
|
| 210 |
+
|
| 211 |
+
def fix_html_advanced(self, code):
|
| 212 |
+
"""高级HTML修复"""
|
| 213 |
+
fixes = []
|
| 214 |
+
fixed_code = code
|
| 215 |
+
|
| 216 |
+
# 修复属性引号
|
| 217 |
+
attr_pattern = r'(\w+)=([^"\'][^\s>]*)'
|
| 218 |
+
fixed_code = re.sub(attr_pattern, r'\1="\2"', fixed_code)
|
| 219 |
+
if fixed_code != code:
|
| 220 |
+
fixes.append("修复了HTML属性引号")
|
| 221 |
+
|
| 222 |
+
# 修复常见的标签闭合问题
|
| 223 |
+
unclosed_tags = {
|
| 224 |
+
'<div>': '</div>', '<p>': '</p>', '<span>': '</span>',
|
| 225 |
+
'<h1>': '</h1>', '<h2>': '</h2>', '<h3>': '</h3>',
|
| 226 |
+
'<ul>': '</ul>', '<ol>': '</ol>', '<li>': '</li>',
|
| 227 |
+
'<table>': '</table>', '<tr>': '</tr>', '<td>': '</td>'
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
for start_tag, end_tag in unclosed_tags.items():
|
| 231 |
+
if start_tag in fixed_code and end_tag not in fixed_code:
|
| 232 |
+
# 简单地在文档末尾添加结束标签(实际应该更智能)
|
| 233 |
+
if fixed_code.count(start_tag) > fixed_code.count(end_tag):
|
| 234 |
+
fixed_code += f"\n{end_tag}"
|
| 235 |
+
fixes.append(f"添加了缺失的 {end_tag}")
|
| 236 |
|
| 237 |
# 添加基本的HTML结构
|
| 238 |
+
if '<!DOCTYPE html>' not in fixed_code and '<html>' not in fixed_code:
|
| 239 |
+
if not fixed_code.strip().startswith('<'):
|
| 240 |
+
fixed_code = f"""<!DOCTYPE html>
|
| 241 |
+
<html lang="zh-CN">
|
| 242 |
<head>
|
| 243 |
<meta charset="UTF-8">
|
| 244 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 245 |
<title>Document</title>
|
| 246 |
</head>
|
| 247 |
<body>
|
| 248 |
+
{fixed_code}
|
| 249 |
</body>
|
| 250 |
</html>"""
|
| 251 |
+
fixes.append("添加了完整的HTML文档结构")
|
| 252 |
|
| 253 |
+
# 修复自闭合标签
|
| 254 |
+
self_closing_tags = ['img', 'br', 'hr', 'input', 'meta', 'link']
|
| 255 |
+
for tag in self_closing_tags:
|
| 256 |
+
pattern = fr'<{tag}([^>]*[^/])>'
|
| 257 |
+
replacement = fr'<{tag}\1 />'
|
| 258 |
+
if re.search(pattern, fixed_code):
|
| 259 |
+
fixed_code = re.sub(pattern, replacement, fixed_code)
|
| 260 |
+
fixes.append(f"修复了 {tag} 标签的自闭合语法")
|
| 261 |
+
|
| 262 |
+
return fixed_code, fixes
|
| 263 |
|
| 264 |
+
def fix_python_advanced(self, code):
|
| 265 |
+
"""高级Python修复"""
|
| 266 |
fixes = []
|
| 267 |
+
fixed_code = code
|
| 268 |
+
lines = fixed_code.split('\n')
|
| 269 |
|
| 270 |
+
for i, line in enumerate(lines):
|
| 271 |
+
stripped = line.strip()
|
| 272 |
+
|
| 273 |
+
# 修复函数定义缺少冒号
|
| 274 |
+
if (stripped.startswith('def ') or
|
| 275 |
+
stripped.startswith('class ') or
|
| 276 |
+
(stripped.startswith('if ') and ':' not in stripped) or
|
| 277 |
+
(stripped.startswith('for ') and ':' not in stripped) or
|
| 278 |
+
(stripped.startswith('while ') and ':' not in stripped) or
|
| 279 |
+
(stripped.startswith('elif ') and ':' not in stripped) or
|
| 280 |
+
(stripped.startswith('else:') is False and stripped.startswith('else') and ':' not in stripped)):
|
| 281 |
+
|
| 282 |
+
if ':' not in stripped and not stripped.endswith('\\'):
|
| 283 |
+
lines[i] = line.rstrip() + ':'
|
| 284 |
+
fixes.append("在语句末尾添加了冒号")
|
| 285 |
+
|
| 286 |
+
# 修复缩进(简单的缩进检查)
|
| 287 |
+
if stripped and not stripped.startswith('#') and not stripped.startswith('@'):
|
| 288 |
+
if ':' in lines[i] and i + 1 < len(lines):
|
| 289 |
+
next_line = lines[i + 1].strip()
|
| 290 |
+
if next_line and not next_line.startswith(' ') and not next_line.startswith('\t'):
|
| 291 |
+
lines[i + 1] = ' ' + lines[i + 1]
|
| 292 |
+
fixes.append("修复了缩进")
|
| 293 |
|
| 294 |
+
fixed_code = '\n'.join(lines)
|
|
|
|
|
|
|
| 295 |
|
| 296 |
+
# 修复常见的Python语法问题
|
| 297 |
+
if 'print ' in fixed_code and 'print(' not in fixed_code:
|
| 298 |
+
fixed_code = fixed_code.replace('print ', 'print(') + ')'
|
| 299 |
+
fixes.append("修复了print语句语法")
|
|
|
|
|
|
|
| 300 |
|
| 301 |
+
return fixed_code, fixes
|
| 302 |
|
| 303 |
+
def fix_javascript_advanced(self, code):
|
| 304 |
+
"""高级JavaScript修复"""
|
| 305 |
fixes = []
|
| 306 |
+
fixed_code = code
|
| 307 |
+
lines = fixed_code.split('\n')
|
| 308 |
|
| 309 |
+
for i, line in enumerate(lines):
|
| 310 |
+
stripped = line.strip()
|
| 311 |
+
|
| 312 |
+
# 添加分号
|
| 313 |
+
if (stripped and
|
| 314 |
+
not stripped.endswith(';') and
|
| 315 |
+
not stripped.endswith('{') and
|
| 316 |
+
not stripped.endswith('}') and
|
| 317 |
+
not stripped.startswith('//') and
|
| 318 |
+
not stripped.startswith('/*') and
|
| 319 |
+
not stripped.startswith('*') and
|
| 320 |
+
not stripped.endswith('*/') and
|
| 321 |
+
('=' in stripped or
|
| 322 |
+
stripped.startswith('const ') or
|
| 323 |
+
stripped.startswith('let ') or
|
| 324 |
+
stripped.startswith('var ') or
|
| 325 |
+
stripped.startswith('console.') or
|
| 326 |
+
'function' in stripped or
|
| 327 |
+
stripped.startswith('return ') or
|
| 328 |
+
stripped.startswith('throw '))):
|
| 329 |
+
|
| 330 |
+
lines[i] = line.rstrip() + ';'
|
| 331 |
+
fixes.append("添加了语句结束分号")
|
| 332 |
|
| 333 |
+
fixed_code = '\n'.join(lines)
|
|
|
|
|
|
|
|
|
|
| 334 |
|
| 335 |
+
# 修复箭头函数
|
| 336 |
+
if '=>' in fixed_code and '(' not in fixed_code.split('=>')[0]:
|
| 337 |
+
# 简单的箭头函数参数修复
|
| 338 |
+
parts = fixed_code.split('=>')
|
| 339 |
+
if len(parts) > 1:
|
| 340 |
+
param_part = parts[0].strip()
|
| 341 |
+
if ' ' in param_part and not param_part.startswith('('):
|
| 342 |
+
fixed_code = fixed_code.replace(param_part, f'({param_part})', 1)
|
| 343 |
+
fixes.append("修复了箭头函数参数括号")
|
| 344 |
+
|
| 345 |
+
return fixed_code, fixes
|
| 346 |
|
| 347 |
+
def fix_css_advanced(self, code):
|
| 348 |
+
"""高级CSS修复"""
|
| 349 |
fixes = []
|
| 350 |
+
fixed_code = code
|
| 351 |
+
lines = fixed_code.split('\n')
|
| 352 |
+
|
| 353 |
+
in_rule = False
|
| 354 |
+
for i, line in enumerate(lines):
|
| 355 |
+
stripped = line.strip()
|
| 356 |
+
|
| 357 |
+
# 检测CSS规则开始和结束
|
| 358 |
+
if stripped.endswith('{'):
|
| 359 |
+
in_rule = True
|
| 360 |
+
elif stripped.endswith('}'):
|
| 361 |
+
in_rule = False
|
| 362 |
+
|
| 363 |
+
# 在CSS规则内修复属性
|
| 364 |
+
if in_rule and ':' in stripped and not stripped.endswith(';') and not stripped.endswith('{'):
|
| 365 |
+
lines[i] = line.rstrip() + ';'
|
| 366 |
+
fixes.append("添加了CSS属性分号")
|
| 367 |
+
|
| 368 |
+
fixed_code = '\n'.join(lines)
|
| 369 |
|
| 370 |
# 修复选择器
|
| 371 |
+
if ': ' in fixed_code and ';' not in fixed_code:
|
| 372 |
+
fixed_code = fixed_code.replace(': ', ': ;')
|
| 373 |
+
fixes.append("修复了CSS选择器语法")
|
| 374 |
|
| 375 |
+
return fixed_code, fixes
|
| 376 |
|
| 377 |
+
def fix_java_advanced(self, code):
|
| 378 |
+
"""高级Java修复"""
|
| 379 |
fixes = []
|
| 380 |
+
fixed_code = code
|
| 381 |
+
lines = fixed_code.split('\n')
|
| 382 |
+
|
| 383 |
+
for i, line in enumerate(lines):
|
| 384 |
+
stripped = line.strip()
|
| 385 |
+
|
| 386 |
+
# 添加语句分号
|
| 387 |
+
if (stripped and
|
| 388 |
+
not stripped.endswith(';') and
|
| 389 |
+
not stripped.endswith('{') and
|
| 390 |
+
not stripped.endswith('}') and
|
| 391 |
+
not stripped.startswith('//') and
|
| 392 |
+
not stripped.startswith('/*') and
|
| 393 |
+
not stripped.startswith('*') and
|
| 394 |
+
not stripped.endswith('*/') and
|
| 395 |
+
('=' in stripped or
|
| 396 |
+
stripped.startswith('public ') or
|
| 397 |
+
stripped.startswith('private ') or
|
| 398 |
+
stripped.startswith('protected ') or
|
| 399 |
+
stripped.startswith('return ') or
|
| 400 |
+
'System.out' in stripped)):
|
| 401 |
+
|
| 402 |
+
lines[i] = line.rstrip() + ';'
|
| 403 |
+
fixes.append("添加了Java语句分号")
|
| 404 |
+
|
| 405 |
+
fixed_code = '\n'.join(lines)
|
| 406 |
|
| 407 |
# 添加基本的类结构
|
| 408 |
+
if 'public class' in fixed_code and '{' not in fixed_code:
|
| 409 |
+
fixed_code = fixed_code.replace('public class', 'public class Main {') + '\n public static void main(String[] args) {\n // 程序入口\n }\n}'
|
| 410 |
+
fixes.append("添加了完整的Java类结构")
|
| 411 |
|
| 412 |
+
return fixed_code, fixes
|
| 413 |
+
|
| 414 |
+
def fix_cpp_advanced(self, code):
|
| 415 |
+
"""高级C++修复"""
|
| 416 |
+
fixes = []
|
| 417 |
+
fixed_code = code
|
| 418 |
+
lines = fixed_code.split('\n')
|
| 419 |
+
|
| 420 |
+
for i, line in enumerate(lines):
|
| 421 |
+
stripped = line.strip()
|
| 422 |
+
|
| 423 |
+
# 添加语句分号
|
| 424 |
+
if (stripped and
|
| 425 |
+
not stripped.endswith(';') and
|
| 426 |
+
not stripped.endswith('{') and
|
| 427 |
+
not stripped.endswith('}') and
|
| 428 |
+
not stripped.startswith('//') and
|
| 429 |
+
not stripped.startswith('/*') and
|
| 430 |
+
not stripped.startswith('#') and
|
| 431 |
+
('=' in stripped or
|
| 432 |
+
stripped.startswith('return ') or
|
| 433 |
+
stripped.startswith('cout ') or
|
| 434 |
+
'cout <<' in stripped)):
|
| 435 |
+
|
| 436 |
+
lines[i] = line.rstrip() + ';'
|
| 437 |
+
fixes.append("添加了C++语句分号")
|
| 438 |
+
|
| 439 |
+
fixed_code = '\n'.join(lines)
|
| 440 |
+
|
| 441 |
+
# 添加main函数
|
| 442 |
+
if '#include' in fixed_code and 'int main' not in fixed_code:
|
| 443 |
+
fixed_code += '\n\nint main() {\n return 0;\n}'
|
| 444 |
+
fixes.append("添加了main函数")
|
| 445 |
+
|
| 446 |
+
return fixed_code, fixes
|
| 447 |
|
| 448 |
+
def fix_php_advanced(self, code):
|
| 449 |
+
"""高级PHP修复"""
|
| 450 |
fixes = []
|
| 451 |
+
fixed_code = code
|
| 452 |
+
|
| 453 |
+
# 添加PHP开始标签
|
| 454 |
+
if '<?php' not in fixed_code and not fixed_code.strip().startswith('<?'):
|
| 455 |
+
fixed_code = '<?php\n' + fixed_code
|
| 456 |
+
fixes.append("添加了PHP开始标签")
|
| 457 |
+
|
| 458 |
+
# 修复PHP语句分号
|
| 459 |
+
lines = fixed_code.split('\n')
|
| 460 |
+
for i, line in enumerate(lines):
|
| 461 |
+
stripped = line.strip()
|
| 462 |
+
if (stripped and
|
| 463 |
+
not stripped.endswith(';') and
|
| 464 |
+
not stripped.endswith('{') and
|
| 465 |
+
not stripped.endswith('}') and
|
| 466 |
+
not stripped.startswith('//') and
|
| 467 |
+
not stripped.startswith('/*') and
|
| 468 |
+
not stripped.startswith('*') and
|
| 469 |
+
not stripped.startswith('<?') and
|
| 470 |
+
not stripped.startswith('?>') and
|
| 471 |
+
('=' in stripped or
|
| 472 |
+
stripped.startswith('echo ') or
|
| 473 |
+
stripped.startswith('return ') or
|
| 474 |
+
stripped.startswith('$'))):
|
| 475 |
+
|
| 476 |
+
lines[i] = line.rstrip() + ';'
|
| 477 |
+
fixes.append("添加了PHP语句分号")
|
| 478 |
+
|
| 479 |
+
fixed_code = '\n'.join(lines)
|
| 480 |
+
return fixed_code, fixes
|
| 481 |
+
|
| 482 |
+
def fix_sql_advanced(self, code):
|
| 483 |
+
"""高级SQL修复"""
|
| 484 |
+
fixes = []
|
| 485 |
+
fixed_code = code.upper() # SQL通常使用大写
|
| 486 |
+
|
| 487 |
+
# 修复常见的SQL语法
|
| 488 |
+
sql_keywords = ['SELECT', 'FROM', 'WHERE', 'INSERT', 'UPDATE', 'DELETE']
|
| 489 |
+
for keyword in sql_keywords:
|
| 490 |
+
if keyword in fixed_code and f'{keyword} ' not in fixed_code:
|
| 491 |
+
fixed_code = fixed_code.replace(keyword, f'{keyword} ')
|
| 492 |
+
fixes.append(f"修复了{keyword}关键字格式")
|
| 493 |
|
| 494 |
+
return fixed_code, fixes
|
| 495 |
+
|
| 496 |
+
def fix_ruby_advanced(self, code):
|
| 497 |
+
"""高级Ruby修复"""
|
| 498 |
+
fixes = []
|
| 499 |
+
fixed_code = code
|
| 500 |
+
|
| 501 |
+
# 修复方法定义
|
| 502 |
+
if 'def ' in fixed_code and fixed_code.count('def ') > fixed_code.count('end'):
|
| 503 |
+
fixed_code += '\nend'
|
| 504 |
+
fixes.append("添加了方法结束的end")
|
| 505 |
+
|
| 506 |
+
# 修复块结构
|
| 507 |
+
if ('do ' in fixed_code or ' do\n' in fixed_code) and fixed_code.count('do') > fixed_code.count('end'):
|
| 508 |
+
fixed_code += '\nend'
|
| 509 |
+
fixes.append("添加了块结束的end")
|
| 510 |
+
|
| 511 |
+
return fixed_code, fixes
|
| 512 |
+
|
| 513 |
+
def fix_go_advanced(self, code):
|
| 514 |
+
"""高级Go修复"""
|
| 515 |
+
fixes = []
|
| 516 |
+
fixed_code = code
|
| 517 |
+
|
| 518 |
+
# 添加package声明
|
| 519 |
+
if 'package ' not in fixed_code:
|
| 520 |
+
fixed_code = 'package main\n\n' + fixed_code
|
| 521 |
+
fixes.append("添加了package声明")
|
| 522 |
+
|
| 523 |
+
# 添加import语句如果使用了标准库
|
| 524 |
+
if ('fmt.' in fixed_code or 'println' in fixed_code) and 'import "' not in fixed_code:
|
| 525 |
+
if 'package main' in fixed_code:
|
| 526 |
+
fixed_code = fixed_code.replace('package main', 'package main\n\nimport "fmt"')
|
| 527 |
+
fixes.append("添加了fmt包导入")
|
| 528 |
+
|
| 529 |
+
# 添加main函数
|
| 530 |
+
if 'func main' not in fixed_code and 'package main' in fixed_code:
|
| 531 |
+
fixed_code += '\n\nfunc main() {\n // 程序入口\n}'
|
| 532 |
fixes.append("添加了main函数")
|
| 533 |
|
| 534 |
+
return fixed_code, fixes
|
| 535 |
|
| 536 |
+
def fix_generic_advanced(self, code):
|
| 537 |
+
"""通用高级修复"""
|
| 538 |
+
fixes = ["进行了通用语法检查和修复"]
|
| 539 |
return code, fixes
|
| 540 |
|
| 541 |
def retrain_from_feedback(self):
|
| 542 |
+
"""根据用户反馈重新训练模型"""
|
| 543 |
+
print(f"🧠 正在从 {len(self.feedback_data)} 条反馈中学习...")
|
| 544 |
+
# 这里可以添加真正的增量学习逻辑
|
| 545 |
+
# 目前先记录,后续可以集成真正的模型微调
|
| 546 |
|
| 547 |
# 创建修复器实例
|
| 548 |
+
fixer = AdvancedCodeFixer()
|
| 549 |
|
| 550 |
def process_code(input_code, use_ai=True):
|
| 551 |
"""处理代码修复"""
|
| 552 |
+
if not input_code.strip():
|
| 553 |
+
return "请输入代码", "❌ 输入为空"
|
| 554 |
|
| 555 |
+
language = fixer.detect_language(input_code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
|
| 557 |
+
try:
|
| 558 |
+
if use_ai and fixer.model is not None:
|
| 559 |
+
fixed_code, fixes = fixer.ai_fix_code(input_code, language)
|
| 560 |
+
else:
|
| 561 |
+
fixed_code, fixes = fixer.advanced_rule_based_fix(input_code, language)
|
| 562 |
+
|
| 563 |
+
# 生成详细的修复报告
|
| 564 |
+
report = f"""🔧 智能修复报告
|
| 565 |
+
📝 检测语言: {language.upper()}
|
| 566 |
+
✅ 修复内容: {', '.join(fixes) if fixes else '代码看起来基本正确'}
|
| 567 |
+
🎯 修复类型: {'AI智能修复' if use_ai and fixer.model else '规则修复'}
|
| 568 |
+
💡 提示: {'' if fixes else '如需进一步优化,请使用反馈功能'}"""
|
| 569 |
+
|
| 570 |
+
return fixed_code, report
|
| 571 |
+
|
| 572 |
+
except Exception as e:
|
| 573 |
+
return f"修复过程中出现错误: {str(e)}", f"❌ 错误报告: {str(e)}"
|
| 574 |
|
| 575 |
def handle_feedback(original_code, fixed_code, user_feedback, is_correct):
|
| 576 |
"""处理用户反馈"""
|
| 577 |
+
if not original_code.strip():
|
| 578 |
+
return "❌ 请输入原始代码"
|
| 579 |
+
|
| 580 |
fixer.save_feedback(original_code, fixed_code, user_feedback, is_correct)
|
| 581 |
+
|
| 582 |
+
if is_correct:
|
| 583 |
+
return "✅ 感谢您的正面反馈!系统会继续保持"
|
| 584 |
+
else:
|
| 585 |
+
return "🔄 感谢您的改进建议!系统正在学习优化..."
|
| 586 |
|
| 587 |
# 创建Gradio界面
|
| 588 |
+
with gr.Blocks(
|
| 589 |
+
theme=gr.themes.Soft(
|
| 590 |
+
primary_hue="blue",
|
| 591 |
+
secondary_hue="green"
|
| 592 |
+
),
|
| 593 |
+
css="""
|
| 594 |
+
.gradio-container {
|
| 595 |
+
max-width: 1200px !important;
|
| 596 |
+
}
|
| 597 |
+
.feedback-box {
|
| 598 |
+
border: 2px solid #e0e0e0;
|
| 599 |
+
border-radius: 10px;
|
| 600 |
+
padding: 15px;
|
| 601 |
+
margin: 10px 0;
|
| 602 |
+
}
|
| 603 |
+
"""
|
| 604 |
+
) as demo:
|
| 605 |
+
gr.Markdown("""
|
| 606 |
+
# 🚀 Capricode 智能多语言代码修复助手
|
| 607 |
+
**支持 HTML, Python, JavaScript, Java, C++, CSS, PHP, SQL, Ruby, Go 等10+种编程语言!**
|
| 608 |
+
""")
|
| 609 |
|
| 610 |
+
with gr.Tab("🛠️ 代码修复"):
|
| 611 |
with gr.Row():
|
| 612 |
+
with gr.Column(scale=1):
|
| 613 |
+
gr.Markdown("### 📥 输入代码")
|
| 614 |
input_code = gr.Textbox(
|
| 615 |
+
label="",
|
| 616 |
+
placeholder="粘贴你的代码到这里...支持多种编程语言自动识别!",
|
| 617 |
+
lines=12,
|
| 618 |
+
show_copy_button=True
|
| 619 |
)
|
| 620 |
+
|
| 621 |
+
with gr.Row():
|
| 622 |
+
use_ai = gr.Checkbox(
|
| 623 |
+
label="使用AI智能修复(推荐)",
|
| 624 |
+
value=True,
|
| 625 |
+
info="启用AI模型进行更智能的修复"
|
| 626 |
+
)
|
| 627 |
+
fix_btn = gr.Button(
|
| 628 |
+
"🔧 开始修复",
|
| 629 |
+
variant="primary",
|
| 630 |
+
size="lg"
|
| 631 |
+
)
|
| 632 |
|
| 633 |
+
with gr.Column(scale=1):
|
| 634 |
+
gr.Markdown("### 📤 修复结果")
|
| 635 |
output_code = gr.Textbox(
|
| 636 |
+
label="修复后的代码",
|
| 637 |
+
lines=12,
|
| 638 |
show_copy_button=True
|
| 639 |
)
|
| 640 |
report = gr.Textbox(
|
| 641 |
+
label="📊 修复分析报告",
|
| 642 |
+
lines=4,
|
| 643 |
+
max_lines=6
|
| 644 |
)
|
| 645 |
|
| 646 |
+
with gr.Tab("💡 反馈学习"):
|
| 647 |
+
gr.Markdown("""
|
| 648 |
+
## 🎯 帮助系统变得更好
|
| 649 |
+
您的反馈会让AI模型越来越聪明!
|
| 650 |
+
""")
|
| 651 |
+
|
| 652 |
with gr.Row():
|
| 653 |
with gr.Column():
|
| 654 |
+
with gr.Group():
|
| 655 |
+
gr.Markdown("#### 原始输入")
|
| 656 |
+
feedback_original = gr.Textbox(
|
| 657 |
+
label="",
|
| 658 |
+
placeholder="原始有问题的代码...",
|
| 659 |
+
lines=4
|
| 660 |
+
)
|
| 661 |
+
|
| 662 |
+
with gr.Group():
|
| 663 |
+
gr.Markdown("#### 修复结果")
|
| 664 |
+
feedback_fixed = gr.Textbox(
|
| 665 |
+
label="",
|
| 666 |
+
placeholder="系统修复后的代码...",
|
| 667 |
+
lines=4
|
| 668 |
+
)
|
| 669 |
+
|
| 670 |
+
with gr.Column():
|
| 671 |
+
with gr.Group():
|
| 672 |
+
gr.Markdown("#### 您的评价")
|
| 673 |
+
user_feedback = gr.Textbox(
|
| 674 |
+
label="改进建议",
|
| 675 |
+
placeholder="这里可以如何改进?有什么建议?",
|
| 676 |
+
lines=3
|
| 677 |
+
)
|
| 678 |
+
|
| 679 |
+
is_correct = gr.Radio(
|
| 680 |
+
choices=[
|
| 681 |
+
("✅ 修复正确", True),
|
| 682 |
+
("🔄 需要改进", False)
|
| 683 |
+
],
|
| 684 |
+
label="修复效果评价",
|
| 685 |
+
value=True
|
| 686 |
+
)
|
| 687 |
+
|
| 688 |
+
feedback_btn = gr.Button(
|
| 689 |
+
"📮 提交反馈",
|
| 690 |
+
variant="secondary"
|
| 691 |
+
)
|
| 692 |
+
|
| 693 |
+
feedback_result = gr.Textbox(
|
| 694 |
+
label="反馈结果",
|
| 695 |
+
interactive=False,
|
| 696 |
+
lines=2
|
| 697 |
)
|
| 698 |
+
|
| 699 |
+
with gr.Tab("📚 语言支持"):
|
| 700 |
+
gr.Markdown("""
|
| 701 |
+
## 🌍 支持的编程语言
|
| 702 |
+
|
| 703 |
+
| 语言 | 支持特性 | 示例 |
|
| 704 |
+
|------|----------|------|
|
| 705 |
+
| **HTML** | 标签闭合、属性引号、文档结构 | `<div class=test>` → `<div class="test">` |
|
| 706 |
+
| **Python** | 括号匹配、缩进、冒号、引号 | `print('hello'` → `print('hello')` |
|
| 707 |
+
| **JavaScript** | 分号、括号、箭头函数 | `function test(` → `function test()` |
|
| 708 |
+
| **Java** | 分号、类结构、main方法 | `public class Test` → 完整类结构 |
|
| 709 |
+
| **C++** | 分号、头文件、main函数 | `cout << "hello"` → 添加分号 |
|
| 710 |
+
| **CSS** | 分号、选择器、属性 | `color: red` → `color: red;` |
|
| 711 |
+
| **PHP** | 开始标签、分号、语法 | `echo "hello"` → `echo "hello";` |
|
| 712 |
+
| **SQL** | 关键字格式、语法 | `select name` → `SELECT NAME` |
|
| 713 |
+
| **Ruby** | end关键字、块结构 | `def test` → 添加end |
|
| 714 |
+
| **Go** | package、import、main函数 | 自动添加基本结构 |
|
| 715 |
+
|
| 716 |
+
💡 **提示**: 系统会自动检测代码语言并应用相应的修复规则!
|
| 717 |
+
""")
|
| 718 |
|
| 719 |
# 事件处理
|
| 720 |
fix_btn.click(
|
|
|
|
| 729 |
outputs=[feedback_result]
|
| 730 |
)
|
| 731 |
|
| 732 |
+
# 更丰富的示例
|
| 733 |
+
gr.Markdown("## 🎯 试试这些例子(点击自动填充):")
|
| 734 |
+
|
| 735 |
+
examples_data = [
|
| 736 |
+
["<div class=test>\n <p>Hello World\n <img src=image.jpg alt=test>\n</div>", True], # HTML
|
| 737 |
+
["def greet(name\n print(f'Hello, {name}!')\n return 'Done'", True], # Python
|
| 738 |
+
["function calculate(a, b {\n const result = a + b\n console.log('结果:', result\n return result", True], # JavaScript
|
| 739 |
+
["public class Calculator {\n public int add(int a, int b) {\n return a + b\n }", True], # Java
|
| 740 |
+
[".container {\n width: 100%\n height: 200px\n background: blue\n}", True], # CSS
|
| 741 |
+
["<?php\n$name = 'John'\necho 'Hello, ' . $name", True], # PHP
|
| 742 |
+
["select name, age from users where age > 18", True], # SQL
|
| 743 |
+
["def greet(name)\n puts 'Hello, ' + name", True], # Ruby
|
| 744 |
+
["fmt.Println('Hello, World!')", True], # Go
|
| 745 |
+
]
|
| 746 |
+
|
| 747 |
gr.Examples(
|
| 748 |
+
examples=examples_data,
|
| 749 |
+
inputs=[input_code, use_ai],
|
| 750 |
+
label="点击示例代码自动测试"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 751 |
)
|
| 752 |
|
| 753 |
if __name__ == "__main__":
|