curry tang
commited on
Commit
·
4797738
1
Parent(s):
03b5791
update
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
from langchain_core.messages import HumanMessage, AIMessage
|
4 |
from llm import DeepSeekLLM, OpenRouterLLM, TongYiLLM
|
5 |
from config import settings
|
6 |
import base64
|
@@ -58,6 +57,58 @@ def update_chat(_provider: str, _chat, _model: str, _temperature: float, _max_to
|
|
58 |
return _chat
|
59 |
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
with gr.Blocks() as app:
|
62 |
with gr.Tab('聊天'):
|
63 |
chat_engine = gr.State(value=None)
|
@@ -71,7 +122,7 @@ with gr.Blocks() as app:
|
|
71 |
additional_inputs=[chat_engine]
|
72 |
)
|
73 |
with gr.Column(scale=1, min_width=300):
|
74 |
-
with gr.Accordion('
|
75 |
with gr.Column():
|
76 |
provider = gr.Dropdown(
|
77 |
label='模型厂商',
|
@@ -125,5 +176,28 @@ with gr.Blocks() as app:
|
|
125 |
outputs=[chat_engine],
|
126 |
)
|
127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
app.launch(debug=settings.debug, show_api=False)
|
|
|
1 |
import gradio as gr
|
2 |
+
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
|
|
|
3 |
from llm import DeepSeekLLM, OpenRouterLLM, TongYiLLM
|
4 |
from config import settings
|
5 |
import base64
|
|
|
57 |
return _chat
|
58 |
|
59 |
|
60 |
+
def explain_code(_code_type: str, _code: str, _chat):
|
61 |
+
if _chat is None:
|
62 |
+
_chat = init_chat()
|
63 |
+
chat_messages = [
|
64 |
+
SystemMessage(content=f'你的任务是获取提供的代码片段,并用简单易懂的语言解释它。分解代码的功能、目的和关键组件。使用类比、示例和通俗术语,使解释对编码知识很少的人来说易于理解。除非绝对必要,否则避免使用技术术语,并为使用的任何术语提供清晰的解释。目标是帮助读者在高层次上理解代码的作用和工作原理。'),
|
65 |
+
HumanMessage(content=_code),
|
66 |
+
]
|
67 |
+
response_message = ''
|
68 |
+
for chunk in _chat.stream(chat_messages):
|
69 |
+
response_message = response_message + chunk.content
|
70 |
+
yield response_message
|
71 |
+
|
72 |
+
|
73 |
+
def optimize_code(_code_type: str, _code: str, _chat):
|
74 |
+
if _chat is None:
|
75 |
+
_chat = init_chat()
|
76 |
+
chat_messages = [
|
77 |
+
SystemMessage(content=f'你的任务是分析提供的 {_code_type} 代码片段,并提出改进建议以优化其性能。确定可以使代码更高效、更快或更节省资源的地方。提供具体的优化建议,并解释这些更改如何提高代码的性能。优化后的代码应该保持与原始代码相同的功能,同时展示出更高的效率。'),
|
78 |
+
HumanMessage(content=_code),
|
79 |
+
]
|
80 |
+
response_message = ''
|
81 |
+
for chunk in _chat.stream(chat_messages):
|
82 |
+
response_message = response_message + chunk.content
|
83 |
+
yield response_message
|
84 |
+
|
85 |
+
|
86 |
+
def debug_code(_code_type: str, _code: str, _chat):
|
87 |
+
if _chat is None:
|
88 |
+
_chat = init_chat()
|
89 |
+
chat_messages = [
|
90 |
+
SystemMessage(content=f'你的任务是分析提供的 {_code_type} 代码片段,识别其中存在的任何错误,并提供一个修正后的代码版本来解决这些问题。解释你在原始代码中发现的问题以及你的修复如何解决它们。修正后的代码应该是功能性的、高效的,并遵循 {_code_type} 编程的最佳实践。'),
|
91 |
+
HumanMessage(content=_code),
|
92 |
+
]
|
93 |
+
response_message = ''
|
94 |
+
for chunk in _chat.stream(chat_messages):
|
95 |
+
response_message = response_message + chunk.content
|
96 |
+
yield response_message
|
97 |
+
|
98 |
+
|
99 |
+
def function_gen(_code_type: str, _code: str, _chat):
|
100 |
+
if _chat is None:
|
101 |
+
_chat = init_chat()
|
102 |
+
chat_messages = [
|
103 |
+
SystemMessage(content=f'你的任务是根据提供的自然语言请求创建 {_code_type} 函数。这些请求将描述函数的期望功能,包括输入参数和预期返回值。根据给定的规范实现这些函数,确保它们能够处理边缘情况,执行必要的验证,并遵循 {_code_type} 编程的最佳实践。请在代码中包含适当的注释,以解释逻辑并帮助其他开发人员理解实现。'),
|
104 |
+
HumanMessage(content=_code),
|
105 |
+
]
|
106 |
+
response_message = ''
|
107 |
+
for chunk in _chat.stream(chat_messages):
|
108 |
+
response_message = response_message + chunk.content
|
109 |
+
yield response_message
|
110 |
+
|
111 |
+
|
112 |
with gr.Blocks() as app:
|
113 |
with gr.Tab('聊天'):
|
114 |
chat_engine = gr.State(value=None)
|
|
|
122 |
additional_inputs=[chat_engine]
|
123 |
)
|
124 |
with gr.Column(scale=1, min_width=300):
|
125 |
+
with gr.Accordion('模型参数设置', open=True):
|
126 |
with gr.Column():
|
127 |
provider = gr.Dropdown(
|
128 |
label='模型厂商',
|
|
|
176 |
outputs=[chat_engine],
|
177 |
)
|
178 |
|
179 |
+
with gr.Tab('代码优化'):
|
180 |
+
chat_engine = gr.State(value=None)
|
181 |
+
with gr.Row():
|
182 |
+
with gr.Column(scale=1):
|
183 |
+
with gr.Row(variant="panel"):
|
184 |
+
code_result = gr.Markdown(label='解释结果', value=None)
|
185 |
+
with gr.Column(scale=1):
|
186 |
+
code_type = gr.Dropdown(
|
187 |
+
label='代码类型',
|
188 |
+
choices=['Javascript', 'Typescript', 'Python', 'C++', 'PHP', 'Java'],
|
189 |
+
value='Javascript',
|
190 |
+
)
|
191 |
+
code = gr.Textbox(label='代码', lines=10, value=None)
|
192 |
+
with gr.Row():
|
193 |
+
explain_code_btn = gr.Button('解释代码')
|
194 |
+
optimize_code_btn = gr.Button('优化代码')
|
195 |
+
debug_code_btn = gr.Button('错误修复')
|
196 |
+
function_gen_btn = gr.Button('函数生成')
|
197 |
+
explain_code_btn.click(fn=explain_code, inputs=[code_type, code, chat_engine], outputs=[code_result])
|
198 |
+
optimize_code_btn.click(fn=optimize_code, inputs=[code_type, code, chat_engine], outputs=[code_result])
|
199 |
+
debug_code_btn.click(fn=debug_code, inputs=[code_type, code, chat_engine], outputs=[code_result])
|
200 |
+
function_gen_btn.click(fn=function_gen, inputs=[code_type, code, chat_engine], outputs=[code_result])
|
201 |
+
|
202 |
|
203 |
app.launch(debug=settings.debug, show_api=False)
|