pangxiang commited on
Commit
2c49f59
·
verified ·
1 Parent(s): 75e16a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -96
app.py CHANGED
@@ -1,108 +1,52 @@
1
  #!/usr/bin/env python3
2
  # -*- coding: utf-8 -*-
3
  """
4
- CodeGPS Pro — DeepSeek AI 测试安全
5
- 不在磁盘保存任何凭证;关闭或点击“清除凭证”即忘记。
6
  """
7
- import gradio as gr, json, re, difflib
8
-
9
- # ------------------------------------------------------------
10
- # 极简代码引擎
11
- # ------------------------------------------------------------
12
- class Engine:
13
- def __init__(self): self.lines=[]; self.history=[]
14
- def load(self,code):
15
- self.lines=code.split('\n');return f"✅ 载入 {len(self.lines)} 行"
16
- def replace_range(self,s,e,new):
17
- self.history.append(self.lines.copy())
18
- old=self.lines[s-1:e]; self.lines[s-1:e]=new.split('\n')
19
- return old
20
- def undo(self):
21
- if not self.history:return"无历史"
22
- self.lines=self.history.pop();return"✅ 撤销"
23
- def get_code(self): return '\n'.join(self.lines)
24
-
25
- eng=Engine()
26
-
27
- # ------------------------------------------------------------
28
- # 虚拟 AI 调用:你这替换为真实 DeepSeek API 请求
29
- # ------------------------------------------------------------
30
- def call_ai_patch(api_key, model, code, task):
31
- """这里应调用真实接口;返回 JSON patch 字符串"""
32
- # mock:演示只改前两行
33
- fake=[{"start":1,"end":2,"new_code":"print('AI已修复')"}]
34
- return json.dumps(fake,ensure_ascii=False)
35
-
36
- # ------------------------------------------------------------
37
- # diff 生成
38
- # ------------------------------------------------------------
39
- def diff_html(old,new,s,e):
40
- out=['<pre style="background:#fff;border:1px solid #ccc;'
41
- 'padding:8px;height:300px;overflow:auto;">']
42
- for l in difflib.unified_diff(old,new,fromfile=f"旧({s}-{e})",
43
- tofile="新",lineterm=""):
44
- c='red'if l.startswith('-')else'green'if l.startswith('+')else'#000'
45
- out.append(f'<span style="color:{c}">{l}</span>')
46
- out.append('</pre>')
47
- return ''.join(out)
48
-
49
- # ------------------------------------------------------------
50
- # UI
51
- # ------------------------------------------------------------
52
- def make_ui():
53
- with gr.Blocks(title="CodeGPS Pro — 安全测试版") as app:
54
- gr.Markdown("## 👁 CodeGPS Pro — DeepSeek AI 视觉补丁安全版")
55
-
56
- # 仅内存保存的 Key / Model
57
- creds = gr.State({"api_key":"","model":""})
58
 
59
  with gr.Row():
60
- code=gr.Code(label="编辑器",language="python",lines=25)
61
- chat=gr.Chatbot(label="AI日志")
62
-
63
- with gr.Accordion("⚙️ 临时凭证设置",open=False):
64
- api_in=gr.Textbox(label="API Key",type="password")
65
- model_in=gr.Textbox(label="模型名",value="deepseek-chat")
66
- save_btn=gr.Button("保存临时凭证")
67
- clear_btn=gr.Button("🔐 清除凭证")
68
- out_info=gr.Textbox(label="状态",interactive=False)
69
-
70
- # 存入内存状态
71
- def save_creds(api,model):
72
- return {"api_key":api,"model":model},f"✅ 凭证加载于内存(退出即清除)"
73
- def clear_creds():
74
- return {"api_key":"","model":""},"🔐 已清除内存凭证"
75
- save_btn.click(save_creds,[api_in,model_in],[creds,out_info])
76
- clear_btn.click(clear_creds,[],[creds,out_info])
77
 
78
- task=gr.Textbox(label="任务描述",placeholder="讲清要修的部分",lines=2)
79
- load_btn=gr.Button("🚀 加载并自动修复")
80
- diffview=gr.HTML()
81
 
82
- def auto_fix(code_text,task_text,cred,chat_hist):
83
- status=eng.load(code_text)
84
- if not cred["api_key"]:
85
- chat_hist.append(("系统","⚠️ 未设置API Key"))
86
- return code_text,chat_hist,"未设置Key",""
87
- resp=call_ai_patch(cred["api_key"],cred["model"],code_text,task_text)
88
- try:
89
- patches=json.loads(resp)
90
- for p in sorted(patches,key=lambda x:x["start"],reverse=True):
91
- old=eng.replace_range(p["start"],p["end"],p["new_code"])
92
- html=diff_html(old,p["new_code"].split("\n"),p["start"],p["end"])
93
- chat_hist.append(("AI",f"🩹 已自动修复 {len(patches)} 处"))
94
- return eng.get_code(),chat_hist,f"{status}|AI 修复完成",html
95
- except Exception as e:
96
- chat_hist.append(("系统",f"⚠️ AI输出错误 {e}\n{resp}"))
97
- return code_text,chat_hist,"出错",""
98
- load_btn.click(auto_fix,
99
- inputs=[code,task,creds,chat],
100
- outputs=[code,chat,out_info,diffview])
101
 
102
- undo=gr.Button("↩️ 撤销")
103
- undo.click(lambda:[eng.undo(),eng.get_code()],[ ],[out_info,code])
104
  return app
105
 
106
- if __name__=="__main__":
107
- ui=make_ui()
108
  ui.launch(server_port=7860)
 
1
  #!/usr/bin/env python3
2
  # -*- coding: utf-8 -*-
3
  """
4
+ CodeGPS Pro —DeepSeek 两窗交互
5
+ 左:代码编辑器 右:AI 聊天(上下文连续)
6
  """
7
+ import gradio as gr, json
8
+
9
+ # 模拟本地修复逻辑;接通真实 API 时替换此函数
10
+ def call_deepseek(api_key, model, full_prompt):
11
+ # 在这里执行真实网络调用,返回生成内容
12
+ fake_reply = "我已看见你的全部代码,这里只是示例回答。\n" \
13
+ "若需要修改,请告诉我行号或描述问题。"
14
+ return fake_reply
15
+
16
+ # 聊天处理函数
17
+ def chat_round(message, history, code_text, api_key, model):
18
+ if not api_key.strip():
19
+ return history + [("系统", "⚠️ 请先输入 API Key")], ""
20
+ # 构造 prompt,把最新代码附加进去
21
+ prompt = f"用户消息:{message}\n\n当前完整代码:\n```{code_text}```"
22
+ ai_reply = call_deepseek(api_key, model, prompt)
23
+ history.append((message, ai_reply))
24
+ return history, ""
25
+
26
+ # Gradio UI
27
+ def create_ui():
28
+ with gr.Blocks(title="CodeGPS Pro DeepSeek 两窗版") as app:
29
+ gr.Markdown("## 👁 CodeGPS Pro — DeepSeek AI 聊天修复版")
30
+
31
+ with gr.Accordion("🔐 临时配置", open=False):
32
+ api_in = gr.Textbox(label="API KEY", type="password")
33
+ model_in = gr.Textbox(label="模型名", value="deepseek-chat")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  with gr.Row():
36
+ code = gr.Code(label="编辑器", language="python", lines=30)
37
+ chatbot = gr.Chatbot(label="DeepSeek对话")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
+ msg = gr.Textbox(label="输入问题或指令", placeholder="和 DeepSeek AI 对话…")
40
+ send = gr.Button("发送")
 
41
 
42
+ send.click(
43
+ chat_round,
44
+ inputs=[msg, chatbot, code, api_in, model_in],
45
+ outputs=[chatbot, msg]
46
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
 
 
48
  return app
49
 
50
+ if __name__ == "__main__":
51
+ ui = create_ui()
52
  ui.launch(server_port=7860)