Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,108 +1,52 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
"""
|
| 4 |
-
CodeGPS Pro —
|
| 5 |
-
|
| 6 |
"""
|
| 7 |
-
import gradio as gr, json
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
return
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 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=
|
| 61 |
-
|
| 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 |
-
|
| 79 |
-
|
| 80 |
-
diffview=gr.HTML()
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 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=
|
| 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)
|