import os
import re
import base64
import json
from typing import Dict, List, Optional, Tuple
import gradio as gr
from loguru import logger
try:
from utils import init_logger
logger = init_logger()
except Exception as e:
logger.info(f"import_error: {str(e)}")
import sambanova_gradio
from sambanova_gradio import get_fn
import modelscope_studio.components.base as ms
import modelscope_studio.components.legacy as legacy
import modelscope_studio.components.antd as antd
from config import DEMO_LIST, SystemPrompt
YOUR_API_TOKEN = os.getenv('SAMBANOVA_API_KEY')
History = List[Tuple[str, str]]
Messages = List[Dict[str, str]]
from tree_sitter_languages import get_parser
def check_code_error(code, language='python'):
language_ext = {'go': 'go',
'html': 'html',
'python': 'python',
'cpp': 'cpp',
# 'sql': 'sql',
'php': 'php',
'javascript': 'javascript',
# 'R': 'r',
'typescript': 'typescript',
'ruby': 'ruby',
'bash': 'bash',
'rust': 'rust',
# 'D': 'd',
'scala': 'scala',
'csharp': 'c_sharp',
'java': 'java',
'json': 'json',
'julia': 'julia',
'css': 'css',
'jsdoc': 'jsdoc',
'swift': 'swift',
'cli': 'cli',
}
if language not in language_ext.keys():
return {'result': True, 'error': 'no language in tree_sitter_languages'}
try:
parser = get_parser(language_ext[language])
code_bytes = code.encode('utf-8')
tree = parser.parse(code_bytes)
root_node = tree.root_node
visit_list = [root_node]
ans = []
while visit_list:
node = visit_list.pop()
if node.type == 'ERROR':
return {'result': True, 'error': True, 'root_node': root_node}
is_inner = True if node.children else False
ans.append([node, is_inner])
for child in node.children:
visit_list.append(child)
return {'result': True, 'error': False, 'root_node': root_node}
except Exception as e:
return {'result': False, 'error': f"error:{str(e)}"}
def history_to_messages(history: History, system: str) -> Messages:
messages = [{'role': 'system', 'content': system}]
for h in history:
messages.append({'role': 'user', 'content': h[0]})
messages.append({'role': 'assistant', 'content': h[1]})
return messages
def messages_to_history(messages: Messages) -> History:
assert messages[0]['role'] == 'system'
history = []
for q, r in zip(messages[1::2], messages[2::2]):
history.append([q['content'], r['content']])
return history
def remove_code_block(text, language='html'):
pattern = r'```' + language.lower() + '\n(.+?)\n```'
match = re.search(pattern, text, re.DOTALL)
if match:
return match.group(1).strip()
else:
return text.strip()
def replace_newlines_outside_code_blocks(text):
pattern = r'(```.*?```)|(\\n)'
def replacement(match):
if match.group(1):
return match.group(1)
elif match.group(2):
return '
'
result = re.sub(pattern, replacement, text, flags=re.DOTALL)
return result
def history_render(history: History):
local_history = history.copy()
render_history = []
for his in local_history:
replace_newline = replace_newlines_outside_code_blocks(his[0])
render_history.append([replace_newline, his[1]])
return gr.update(open=True), render_history
def clear_history():
return []
def send_to_sandbox(code):
encoded_html = base64.b64encode(code.encode('utf-8')).decode('utf-8')
data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
return f""
def demo_card_click(e: gr.EventData):
index = e._data['component']['index']
return DEMO_LIST[index]['description']
def read_jsonl(fn):
with open(fn, encoding="utf-8") as f:
return [json.loads(l) for l in f.readlines()]
def get_item_by_index(item_list, index, val=None):
if val is not None and val in item_list:
return val
if len(item_list) > index:
return item_list[index]
return ""
def get_item_list(locale="zh", programming_language="", category="", difficulty="", debugInfo=""):
samples = read_jsonl(f'./FullStackBench_data/fsb_{locale}_20241204.jsonl')
item_list_count = {}
item_samples = []
for sample in samples:
labels = sample['labels']
content = [sample['content']]
sample_dict = [labels['programming_language'], f"/{labels['category']}/{labels['difficulty']}/", content]
if (locale != "" and labels['locale'] == locale):
if programming_language == "":
item = labels['programming_language']
item_list_count[item] = item_list_count.get(item, 0) + 1
item_samples.append(sample_dict)
elif category == "" and labels['programming_language'] == programming_language:
item = labels['category']
item_list_count[item] = item_list_count.get(item, 0) + 1
item_samples.append(sample_dict)
elif difficulty == "" and labels['programming_language'] == programming_language and labels['category'] == category:
item = labels['difficulty']
item_list_count[item] = item_list_count.get(item, 0) + 1
item_samples.append(sample_dict)
elif labels['programming_language'] == programming_language and labels['category'] == category and labels['difficulty'] == difficulty:
item = labels['difficulty']
item_list_count[item] = item_list_count.get(item, 0) + 1
item_samples.append(sample_dict)
item_list = list(item_list_count.keys())
item_list_dropdown = []
all_count = 0
for item in item_list:
all_count += item_list_count[item]
item_list_dropdown.append((f"{item}[{item_list_count[item]}]",item))
item_list_dropdown.insert(0,(f"All[{all_count}]", ""))
return item_list, item_list_dropdown, item_samples
item_list_locale = ["en", "zh"]
item_list_dropdown_locale = [("en[1687]", "en"), ("zh[1687]", "zh")]
item_list_programming_language, item_list_dropdown_programming_language, item_samples = get_item_list(locale=item_list_locale[0], programming_language="", category="", difficulty="")
item_list_category, item_list_dropdown_category, item_samples = get_item_list(locale=item_list_locale[0], programming_language=get_item_by_index(item_list_programming_language, 0, "python"), category="", difficulty="")
item_list_difficulty, item_list_dropdown_difficulty, item_samples = get_item_list(locale=item_list_locale[0], programming_language=get_item_by_index(item_list_programming_language, 0, "python"), category=get_item_by_index(item_list_category, 0, "Basic Programming"), difficulty="")
item_list_select, item_list_select_dropdown, item_samples = get_item_list(locale=item_list_locale[0], programming_language=get_item_by_index(item_list_programming_language, 0, "python"), category=get_item_by_index(item_list_category, 0, "Basic Programming"), difficulty=get_item_by_index(item_list_difficulty, 0))
def get_input_examples():
input_examples = []
for demo in DEMO_LIST:
input_examples.append(['html', demo['title'], demo['description']])
return input_examples
def code_syntax_check_fun(code, language='html'):
check_result = check_code_error(code, language=language.lower())
if check_result['result'] == True:
if check_result['error'] == True:
return 'No pass'
elif check_result['error'] == False:
return 'Pass'
else:
return f"No check ['{language}' not in check_languages]"
else:
return check_result['error']
with gr.Blocks(css_paths="app.css", title="Qwen2.5 Coder Artifacts (+SambaNova+FullStackBench)") as demo:
history_state = gr.State([])
setting = gr.State({
"system": SystemPrompt,
})
with gr.Row():
code_language_example = gr.Textbox(show_label=False, lines=1, container=False, visible=False)
input_text_title= gr.TextArea(show_label=False, container=False, visible=False)
input_text_example = gr.TextArea(show_label=False, container=False, visible=False)
with gr.Tab("Examples_HTML"):
input_examples = get_input_examples()
gr.Examples(
examples=input_examples,
inputs=[code_language_example, input_text_title, input_text_example],
outputs=None,
label='',
examples_per_page=10,
cache_examples=False,
)
with gr.Tab("Examples_from_FullStackBench"):
with gr.Row():
with gr.Column():
locale = gr.Dropdown(
choices=item_list_dropdown_locale,
label="locale",
value=get_item_by_index(item_list_locale, 0),
)
with gr.Column():
programming_language = gr.Dropdown(
choices=item_list_dropdown_programming_language,
label="programming_language",
value=get_item_by_index(item_list_programming_language, 0, "python"),
)
with gr.Column():
category = gr.Dropdown(
choices=item_list_dropdown_category,
label="category",
value=get_item_by_index(item_list_category, 0, "Basic Programming"),
)
with gr.Column():
difficulty = gr.Dropdown(
choices=item_list_dropdown_difficulty,
label="difficulty",
value=get_item_by_index(item_list_difficulty, 0),
)
with gr.Row():
test_examples = gr.Examples(
examples=item_samples,
inputs=[code_language_example, input_text_title, input_text_example],
outputs=None,
label='Examples:',
examples_per_page=5,
cache_examples=False,
)
def locale_select(locale):
item_list_programming_language, item_list_dropdown_programming_language, item_samples = get_item_list(locale=locale, programming_language="", category="", difficulty="", debugInfo="locale_1_")
item_list_category, item_list_dropdown_category, item_samples = get_item_list(locale=locale, programming_language=get_item_by_index(item_list_programming_language, 0), category="", difficulty="", debugInfo="locale_2_")
item_list_difficulty, item_list_dropdown_difficulty, item_samples = get_item_list(locale=locale, programming_language=get_item_by_index(item_list_programming_language, 0), category=get_item_by_index(item_list_category, 0), difficulty="", debugInfo="locale_3_")
_, _, item_samples = get_item_list(locale=locale, programming_language=get_item_by_index(item_list_programming_language, 0), category=get_item_by_index(item_list_category, 0), difficulty=get_item_by_index(item_list_difficulty, 0), debugInfo="locale_4_")
return (gr.Dropdown(choices=item_list_dropdown_programming_language, value=item_list_dropdown_programming_language[0][1]),
gr.Dropdown(choices=item_list_dropdown_category, value=item_list_dropdown_category[0][1]),
gr.Dropdown(choices=item_list_dropdown_difficulty, value=item_list_dropdown_difficulty[0][1]),
gr.Dataset(samples=item_samples))
locale.select(
fn=locale_select,
inputs=[locale],
outputs=[programming_language, category, difficulty, test_examples.dataset]
)
def programming_language_select(locale, programming_language):
item_list_category, item_list_dropdown_category, item_samples = get_item_list(locale=locale, programming_language=programming_language, category="", difficulty="", debugInfo="programming_language_1_")
item_list_difficulty, item_list_dropdown_difficulty, item_samples = get_item_list(locale=locale, programming_language=programming_language, category=get_item_by_index(item_list_category, 0), difficulty="", debugInfo="programming_language_2_")
_, _, item_samples = get_item_list(locale=locale, programming_language=programming_language, category=get_item_by_index(item_list_category, 0), difficulty=get_item_by_index(item_list_difficulty, 0), debugInfo="programming_language_3_")
return (gr.Dropdown(choices=item_list_dropdown_category, value=item_list_dropdown_category[0][1]),
gr.Dropdown(choices=item_list_dropdown_difficulty, value=item_list_dropdown_difficulty[0][1]),
gr.Dataset(samples=item_samples))
programming_language.select(
fn=programming_language_select,
inputs=[locale, programming_language],
outputs=[category, difficulty, test_examples.dataset]
)
def category_select(locale, programming_language, category):
item_list_difficulty, item_list_dropdown_difficulty, item_samples = get_item_list(locale=locale, programming_language=programming_language, category=category, difficulty="", debugInfo="category_1_")
_, _, item_samples = get_item_list(locale=locale, programming_language=programming_language, category=category, difficulty=get_item_by_index(item_list_difficulty, 0), debugInfo="category_2_")
return (gr.Dropdown(choices=item_list_dropdown_difficulty, value=item_list_dropdown_difficulty[0][1]),
gr.Dataset(samples=item_samples))
category.select(
fn=category_select,
inputs=[locale, programming_language, category],
outputs=[difficulty, test_examples.dataset]
)
def difficulty_select(locale, programming_language, category, difficulty):
_, _, item_samples = get_item_list(locale=locale, programming_language=programming_language, category=category, difficulty=difficulty, debugInfo="difficulty_1_")
return gr.Dataset(samples=item_samples)
difficulty.select(
fn=difficulty_select,
inputs=[locale, programming_language, category, difficulty],
outputs=[test_examples.dataset]
)
with ms.Application() as app:
with antd.ConfigProvider():
with antd.Row(gutter=[32, 12]) as layout:
with antd.Col(span=24, md=8):
with antd.Flex(vertical=True, gap="middle", wrap=True):
header = gr.HTML("""