yizhangliu
commited on
Commit
•
9d752bb
1
Parent(s):
465b5fb
add “check code syntax”
Browse files- app.py +87 -40
- requirements.txt +3 -0
app.py
CHANGED
@@ -5,6 +5,7 @@ import json
|
|
5 |
from typing import Dict, List, Optional, Tuple
|
6 |
|
7 |
import gradio as gr
|
|
|
8 |
|
9 |
import sambanova_gradio
|
10 |
from sambanova_gradio import get_fn
|
@@ -19,6 +20,54 @@ YOUR_API_TOKEN = os.getenv('SAMBANOVA_API_KEY')
|
|
19 |
History = List[Tuple[str, str]]
|
20 |
Messages = List[Dict[str, str]]
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def history_to_messages(history: History, system: str) -> Messages:
|
23 |
messages = [{'role': 'system', 'content': system}]
|
24 |
for h in history:
|
@@ -33,8 +82,8 @@ def messages_to_history(messages: Messages) -> History:
|
|
33 |
history.append([q['content'], r['content']])
|
34 |
return history
|
35 |
|
36 |
-
def remove_code_block(text):
|
37 |
-
pattern = r'```
|
38 |
match = re.search(pattern, text, re.DOTALL)
|
39 |
if match:
|
40 |
return match.group(1).strip()
|
@@ -75,8 +124,7 @@ def get_item_list(locale="zh", programming_language="", category="", difficulty=
|
|
75 |
for sample in samples:
|
76 |
labels = sample['labels']
|
77 |
content = [sample['content']]
|
78 |
-
|
79 |
-
sample_dict = [title, content, labels['programming_language']=='html']
|
80 |
if (locale != "" and labels['locale'] == locale):
|
81 |
if programming_language == "":
|
82 |
item = labels['programming_language']
|
@@ -113,9 +161,21 @@ item_list_select, item_list_select_dropdown, item_samples = get_item_list(locale
|
|
113 |
def get_input_examples():
|
114 |
input_examples = []
|
115 |
for demo in DEMO_LIST:
|
116 |
-
input_examples.append([demo['title'], demo['description']
|
117 |
return input_examples
|
118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
with gr.Blocks(css_paths="app.css", title="Qwen2.5 Coder Artifacts (+SambaNova+FullStackBench)") as demo:
|
120 |
history_state = gr.State([])
|
121 |
setting = gr.State({
|
@@ -123,14 +183,14 @@ with gr.Blocks(css_paths="app.css", title="Qwen2.5 Coder Artifacts (+SambaNova+F
|
|
123 |
})
|
124 |
|
125 |
with gr.Row():
|
126 |
-
|
127 |
input_text_title= gr.TextArea(show_label=False, container=False, visible=False)
|
128 |
input_text_example = gr.TextArea(show_label=False, container=False, visible=False)
|
129 |
with gr.Tab("Examples_HTML"):
|
130 |
input_examples = get_input_examples()
|
131 |
gr.Examples(
|
132 |
examples=input_examples,
|
133 |
-
inputs=[input_text_title, input_text_example
|
134 |
outputs=None,
|
135 |
label='',
|
136 |
examples_per_page=10,
|
@@ -165,7 +225,7 @@ with gr.Blocks(css_paths="app.css", title="Qwen2.5 Coder Artifacts (+SambaNova+F
|
|
165 |
with gr.Row():
|
166 |
test_examples = gr.Examples(
|
167 |
examples=item_samples,
|
168 |
-
inputs=[input_text_title, input_text_example
|
169 |
outputs=None,
|
170 |
label='Examples:',
|
171 |
examples_per_page=5,
|
@@ -234,7 +294,7 @@ with gr.Blocks(css_paths="app.css", title="Qwen2.5 Coder Artifacts (+SambaNova+F
|
|
234 |
""")
|
235 |
|
236 |
input = antd.InputTextarea(size="middle", allow_clear=True, placeholder="Please enter what kind of application you want")
|
237 |
-
|
238 |
btn = antd.Button("send", type="primary", size="large")
|
239 |
|
240 |
with antd.Flex(gap="small", wrap=True):
|
@@ -271,16 +331,6 @@ with gr.Blocks(css_paths="app.css", title="Qwen2.5 Coder Artifacts (+SambaNova+F
|
|
271 |
history_drawer.close(lambda: gr.update(
|
272 |
open=False), inputs=[], outputs=[history_drawer])
|
273 |
|
274 |
-
# with antd.Col(span=24, md=16):
|
275 |
-
# with ms.Div(elem_classes="right_panel"):
|
276 |
-
# gr.HTML('<div class="render_header"><span class="header_btn"></span><span class="header_btn"></span><span class="header_btn"></span></div>')
|
277 |
-
# with antd.Tabs(active_key="empty", render_tab_bar="() => null") as state_tab:
|
278 |
-
# with antd.Tabs.Item(key="empty"):
|
279 |
-
# empty = antd.Empty(description="empty input", elem_classes="right_content")
|
280 |
-
# with antd.Tabs.Item(key="loading"):
|
281 |
-
# loading = antd.Spin(True, tip="coding...", size="large", elem_classes="right_content")
|
282 |
-
# with antd.Tabs.Item(key="render"):
|
283 |
-
# sandbox = gr.HTML(elem_classes="html_content")
|
284 |
with antd.Col(span=24, md=16):
|
285 |
with ms.Div(elem_classes="right_panel"):
|
286 |
gr.HTML('<div class="render_header"><span class="header_btn"></span><span class="header_btn"></span><span class="header_btn"></span></div>')
|
@@ -293,8 +343,10 @@ with gr.Blocks(css_paths="app.css", title="Qwen2.5 Coder Artifacts (+SambaNova+F
|
|
293 |
sandbox = gr.HTML(elem_classes="html_content")
|
294 |
with antd.Tabs.Item(key="render_other", label="Render_Code") as render_other:
|
295 |
sandbox_other = legacy.Markdown()
|
|
|
|
|
296 |
|
297 |
-
def generation_code(query: Optional[str],
|
298 |
if query is None:
|
299 |
query = ''
|
300 |
if _history is None:
|
@@ -302,7 +354,7 @@ with gr.Blocks(css_paths="app.css", title="Qwen2.5 Coder Artifacts (+SambaNova+F
|
|
302 |
|
303 |
# Prepare the preprocess and postprocess functions
|
304 |
def preprocess(message, history):
|
305 |
-
if
|
306 |
messages = [{'role': 'system', 'content': _setting['system']}]
|
307 |
else:
|
308 |
messages = [{'role': 'system', 'content': ""}]
|
@@ -339,7 +391,7 @@ with gr.Blocks(css_paths="app.css", title="Qwen2.5 Coder Artifacts (+SambaNova+F
|
|
339 |
assistant_response = response_text
|
340 |
local_history.append([query, assistant_response])
|
341 |
|
342 |
-
code = remove_code_block(assistant_response)
|
343 |
|
344 |
if "```html" in assistant_response:
|
345 |
sandbox_val = send_to_sandbox(code)
|
@@ -354,30 +406,25 @@ with gr.Blocks(css_paths="app.css", title="Qwen2.5 Coder Artifacts (+SambaNova+F
|
|
354 |
render_other_val = gr.update(visible=True)
|
355 |
sandbox_other_val = content
|
356 |
|
|
|
357 |
yield {
|
358 |
code_output: assistant_response,
|
359 |
history_state: local_history,
|
360 |
sandbox: sandbox_val,
|
361 |
-
state_tab:
|
362 |
-
code_drawer:
|
363 |
render_html: render_html_val,
|
364 |
render_other: render_other_val,
|
365 |
sandbox_other: sandbox_other_val,
|
|
|
366 |
}
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
def use_system_prompt_example_change(use_system_prompt):
|
376 |
-
return use_system_prompt
|
377 |
-
use_system_prompt_example.change(
|
378 |
-
fn=use_system_prompt_example_change,
|
379 |
-
inputs=[use_system_prompt_example],
|
380 |
-
outputs=[use_system_prompt],
|
381 |
)
|
382 |
def input_text_example_change(input_text_example):
|
383 |
return input_text_example
|
@@ -388,9 +435,9 @@ with gr.Blocks(css_paths="app.css", title="Qwen2.5 Coder Artifacts (+SambaNova+F
|
|
388 |
)
|
389 |
|
390 |
btn.click(generation_code,
|
391 |
-
inputs=[input,
|
392 |
outputs=[code_output, history_state, sandbox, state_tab, code_drawer,
|
393 |
-
render_html, render_other, sandbox_other])
|
394 |
|
395 |
clear_btn.click(clear_history, inputs=[], outputs=[history_state])
|
396 |
|
|
|
5 |
from typing import Dict, List, Optional, Tuple
|
6 |
|
7 |
import gradio as gr
|
8 |
+
from loguru import logger
|
9 |
|
10 |
import sambanova_gradio
|
11 |
from sambanova_gradio import get_fn
|
|
|
20 |
History = List[Tuple[str, str]]
|
21 |
Messages = List[Dict[str, str]]
|
22 |
|
23 |
+
from tree_sitter_languages import get_parser
|
24 |
+
def check_code_error(code, language='python'):
|
25 |
+
language_ext = {'go': 'go',
|
26 |
+
'html': 'html',
|
27 |
+
'python': 'python',
|
28 |
+
'cpp': 'cpp',
|
29 |
+
# 'sql': 'sql',
|
30 |
+
'php': 'php',
|
31 |
+
'javascript': 'javascript',
|
32 |
+
# 'R': 'r',
|
33 |
+
'typescript': 'typescript',
|
34 |
+
'ruby': 'ruby',
|
35 |
+
'bash': 'bash',
|
36 |
+
'rust': 'rust',
|
37 |
+
# 'D': 'd',
|
38 |
+
'scala': 'scala',
|
39 |
+
'csharp': 'c_sharp',
|
40 |
+
'java': 'java',
|
41 |
+
'json': 'json',
|
42 |
+
'julia': 'julia',
|
43 |
+
'css': 'css',
|
44 |
+
'jsdoc': 'jsdoc',
|
45 |
+
'swift': 'swift',
|
46 |
+
'cli': 'cli',
|
47 |
+
}
|
48 |
+
if language not in language_ext.keys():
|
49 |
+
return {'result': True, 'error': 'no language in tree_sitter_languages'}
|
50 |
+
try:
|
51 |
+
parser = get_parser(language_ext[language])
|
52 |
+
code_bytes = code.encode('utf-8')
|
53 |
+
tree = parser.parse(code_bytes)
|
54 |
+
root_node = tree.root_node
|
55 |
+
|
56 |
+
visit_list = [root_node]
|
57 |
+
ans = []
|
58 |
+
while visit_list:
|
59 |
+
node = visit_list.pop()
|
60 |
+
if node.type == 'ERROR':
|
61 |
+
return {'result': True, 'error': True, 'root_node': root_node}
|
62 |
+
is_inner = True if node.children else False
|
63 |
+
ans.append([node, is_inner])
|
64 |
+
for child in node.children:
|
65 |
+
visit_list.append(child)
|
66 |
+
|
67 |
+
return {'result': True, 'error': False, 'root_node': root_node}
|
68 |
+
except Exception as e:
|
69 |
+
return {'result': False, 'error': str(e)}
|
70 |
+
|
71 |
def history_to_messages(history: History, system: str) -> Messages:
|
72 |
messages = [{'role': 'system', 'content': system}]
|
73 |
for h in history:
|
|
|
82 |
history.append([q['content'], r['content']])
|
83 |
return history
|
84 |
|
85 |
+
def remove_code_block(text, language='html'):
|
86 |
+
pattern = r'```' + language.lower() + '\n(.+?)\n```'
|
87 |
match = re.search(pattern, text, re.DOTALL)
|
88 |
if match:
|
89 |
return match.group(1).strip()
|
|
|
124 |
for sample in samples:
|
125 |
labels = sample['labels']
|
126 |
content = [sample['content']]
|
127 |
+
sample_dict = [labels['programming_language'], f"/{labels['category']}/{labels['difficulty']}/", content]
|
|
|
128 |
if (locale != "" and labels['locale'] == locale):
|
129 |
if programming_language == "":
|
130 |
item = labels['programming_language']
|
|
|
161 |
def get_input_examples():
|
162 |
input_examples = []
|
163 |
for demo in DEMO_LIST:
|
164 |
+
input_examples.append(['html', demo['title'], demo['description']])
|
165 |
return input_examples
|
166 |
|
167 |
+
def code_syntax_check_fun(code, language='html'):
|
168 |
+
check_result = check_code_error(code, language=language.lower())
|
169 |
+
if check_result['result'] == True:
|
170 |
+
if check_result['error'] == True:
|
171 |
+
return 'No pass'
|
172 |
+
elif check_result['error'] == False:
|
173 |
+
return 'Pass'
|
174 |
+
else:
|
175 |
+
return 'No check [no language in check_languages]'
|
176 |
+
else:
|
177 |
+
return check_result['error']
|
178 |
+
|
179 |
with gr.Blocks(css_paths="app.css", title="Qwen2.5 Coder Artifacts (+SambaNova+FullStackBench)") as demo:
|
180 |
history_state = gr.State([])
|
181 |
setting = gr.State({
|
|
|
183 |
})
|
184 |
|
185 |
with gr.Row():
|
186 |
+
code_language_example = gr.Textbox(show_label=False, lines=1, container=False, visible=False)
|
187 |
input_text_title= gr.TextArea(show_label=False, container=False, visible=False)
|
188 |
input_text_example = gr.TextArea(show_label=False, container=False, visible=False)
|
189 |
with gr.Tab("Examples_HTML"):
|
190 |
input_examples = get_input_examples()
|
191 |
gr.Examples(
|
192 |
examples=input_examples,
|
193 |
+
inputs=[code_language_example, input_text_title, input_text_example],
|
194 |
outputs=None,
|
195 |
label='',
|
196 |
examples_per_page=10,
|
|
|
225 |
with gr.Row():
|
226 |
test_examples = gr.Examples(
|
227 |
examples=item_samples,
|
228 |
+
inputs=[code_language_example, input_text_title, input_text_example],
|
229 |
outputs=None,
|
230 |
label='Examples:',
|
231 |
examples_per_page=5,
|
|
|
294 |
""")
|
295 |
|
296 |
input = antd.InputTextarea(size="middle", allow_clear=True, placeholder="Please enter what kind of application you want")
|
297 |
+
code_language = gr.Textbox(label="code language", lines=1)
|
298 |
btn = antd.Button("send", type="primary", size="large")
|
299 |
|
300 |
with antd.Flex(gap="small", wrap=True):
|
|
|
331 |
history_drawer.close(lambda: gr.update(
|
332 |
open=False), inputs=[], outputs=[history_drawer])
|
333 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
334 |
with antd.Col(span=24, md=16):
|
335 |
with ms.Div(elem_classes="right_panel"):
|
336 |
gr.HTML('<div class="render_header"><span class="header_btn"></span><span class="header_btn"></span><span class="header_btn"></span></div>')
|
|
|
343 |
sandbox = gr.HTML(elem_classes="html_content")
|
344 |
with antd.Tabs.Item(key="render_other", label="Render_Code") as render_other:
|
345 |
sandbox_other = legacy.Markdown()
|
346 |
+
with antd.Row():
|
347 |
+
code_syntax_check = gr.Textbox(label="check code syntax: ", lines=1, interactive=False)
|
348 |
|
349 |
+
def generation_code(query: Optional[str], code_language, _setting: Dict[str, str], _history: Optional[History]):
|
350 |
if query is None:
|
351 |
query = ''
|
352 |
if _history is None:
|
|
|
354 |
|
355 |
# Prepare the preprocess and postprocess functions
|
356 |
def preprocess(message, history):
|
357 |
+
if code_language.lower() == 'html':
|
358 |
messages = [{'role': 'system', 'content': _setting['system']}]
|
359 |
else:
|
360 |
messages = [{'role': 'system', 'content': ""}]
|
|
|
391 |
assistant_response = response_text
|
392 |
local_history.append([query, assistant_response])
|
393 |
|
394 |
+
code = remove_code_block(assistant_response, language=code_language)
|
395 |
|
396 |
if "```html" in assistant_response:
|
397 |
sandbox_val = send_to_sandbox(code)
|
|
|
406 |
render_other_val = gr.update(visible=True)
|
407 |
sandbox_other_val = content
|
408 |
|
409 |
+
code_syntax_check_val = code_syntax_check_fun(code, language=code_language)
|
410 |
yield {
|
411 |
code_output: assistant_response,
|
412 |
history_state: local_history,
|
413 |
sandbox: sandbox_val,
|
414 |
+
state_tab: state_tab_val,
|
415 |
+
code_drawer: gr.update(open=False),
|
416 |
render_html: render_html_val,
|
417 |
render_other: render_other_val,
|
418 |
sandbox_other: sandbox_other_val,
|
419 |
+
code_syntax_check: code_syntax_check_val,
|
420 |
}
|
421 |
+
|
422 |
+
def code_language_example_change(code_language):
|
423 |
+
return code_language
|
424 |
+
code_language_example.change(
|
425 |
+
fn=code_language_example_change,
|
426 |
+
inputs=[code_language_example],
|
427 |
+
outputs=[code_language],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
428 |
)
|
429 |
def input_text_example_change(input_text_example):
|
430 |
return input_text_example
|
|
|
435 |
)
|
436 |
|
437 |
btn.click(generation_code,
|
438 |
+
inputs=[input, code_language, setting, history_state],
|
439 |
outputs=[code_output, history_state, sandbox, state_tab, code_drawer,
|
440 |
+
render_html, render_other, sandbox_other, code_syntax_check])
|
441 |
|
442 |
clear_btn.click(clear_history, inputs=[], outputs=[history_state])
|
443 |
|
requirements.txt
CHANGED
@@ -2,3 +2,6 @@ dashscope
|
|
2 |
modelscope_studio~=1.0.0b
|
3 |
sambanova_gradio
|
4 |
openai
|
|
|
|
|
|
|
|
2 |
modelscope_studio~=1.0.0b
|
3 |
sambanova_gradio
|
4 |
openai
|
5 |
+
tree-sitter
|
6 |
+
tree-sitter-languages
|
7 |
+
loguru
|