You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
3.4 KiB
113 lines
3.4 KiB
import tkinter as tk
|
|
from tkinter import filedialog, messagebox, scrolledtext
|
|
import requests
|
|
import json
|
|
import time
|
|
|
|
# 替换为你的 Deepseek API 地址和密钥
|
|
API_URL = "https://api.deepseek.com/v1/chat/completions"
|
|
API_KEY = "your_deepseek_api_key_here"
|
|
|
|
HEADERS = {
|
|
"Authorization": f"Bearer {API_KEY}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
def split_code(code, max_tokens=3000):
|
|
"""将大段代码分割成适合 API 调用的块"""
|
|
lines = code.splitlines()
|
|
chunks = []
|
|
current_chunk = []
|
|
current_length = 0
|
|
|
|
for line in lines:
|
|
line_length = len(line.encode('utf-8'))
|
|
if current_length + line_length > max_tokens:
|
|
chunks.append("\n".join(current_chunk))
|
|
current_chunk = []
|
|
current_length = 0
|
|
current_chunk.append(line)
|
|
current_length += line_length
|
|
|
|
if current_chunk:
|
|
chunks.append("\n".join(current_chunk))
|
|
|
|
return chunks
|
|
|
|
def analyze_code(code_chunk):
|
|
"""调用 Deepseek API 分析代码"""
|
|
prompt = f"请检查以下代码中的潜在问题,包括语法错误、风格问题、逻辑漏洞等:\n\n{code_chunk}"
|
|
data = {
|
|
"model": "deepseek-chat",
|
|
"messages": [
|
|
{"role": "system", "content": "你是一个专业的代码审查助手。"},
|
|
{"role": "user", "content": prompt}
|
|
],
|
|
"temperature": 0.3
|
|
}
|
|
|
|
try:
|
|
response = requests.post(API_URL, headers=HEADERS, json=data)
|
|
response.raise_for_status()
|
|
result = response.json()
|
|
return result['choices'][0]['message']['content']
|
|
except Exception as e:
|
|
return f"API 调用失败: {str(e)}"
|
|
|
|
def load_file():
|
|
file_path = filedialog.askopenfilename(filetypes=[("Python 文件", "*.py"), ("所有文件", "*.*")])
|
|
if file_path:
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
code_text.delete(1.0, tk.END)
|
|
code_text.insert(tk.END, f.read())
|
|
|
|
def analyze():
|
|
code = code_text.get(1.0, tk.END)
|
|
if not code.strip():
|
|
messagebox.showwarning("警告", "请先加载代码")
|
|
return
|
|
|
|
result_text.delete(1.0, tk.END)
|
|
result_text.insert(tk.END, "正在分析代码...\n")
|
|
root.update()
|
|
|
|
chunks = split_code(code)
|
|
all_results = []
|
|
|
|
for i, chunk in enumerate(chunks):
|
|
result = analyze_code(chunk)
|
|
all_results.append(f"=== 第 {i+1} 段代码分析结果 ===\n{result}\n")
|
|
time.sleep(1) # 避免 API 请求过快
|
|
|
|
result_text.delete(1.0, tk.END)
|
|
result_text.insert(tk.END, "\n".join(all_results))
|
|
|
|
# GUI 构建
|
|
root = tk.Tk()
|
|
root.title("Deepseek 代码检查器")
|
|
|
|
frame_top = tk.Frame(root)
|
|
frame_top.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
|
|
|
|
code_label = tk.Label(frame_top, text="代码内容:")
|
|
code_label.pack(anchor='w')
|
|
|
|
code_text = scrolledtext.ScrolledText(frame_top, height=20, width=100)
|
|
code_text.pack(fill=tk.BOTH, expand=True)
|
|
|
|
frame_btn = tk.Frame(root)
|
|
frame_btn.pack(pady=5)
|
|
|
|
load_btn = tk.Button(frame_btn, text="加载代码文件", command=load_file)
|
|
load_btn.pack(side=tk.LEFT, padx=5)
|
|
|
|
analyze_btn = tk.Button(frame_btn, text="开始分析", command=analyze)
|
|
analyze_btn.pack(side=tk.LEFT, padx=5)
|
|
|
|
result_label = tk.Label(root, text="分析结果:")
|
|
result_label.pack(anchor='w', padx=10)
|
|
|
|
result_text = scrolledtext.ScrolledText(root, height=15, width=100)
|
|
result_text.pack(fill=tk.BOTH, expand=True, padx=10, pady=(0, 10))
|
|
|
|
root.mainloop() |