testnow720/hfchat_code_executor_dev
0
1import gradio as gr2import requests3from bs4 import BeautifulSoup4 5DEF_SNIPPET = "print('Hello, World!')"6DEF_LANG = "Python"7 8def execute_snippet(code_snippet: str = DEF_SNIPPET, lang: str = DEF_LANG) -> str:9 lang_param = None10 11 match lang:12 case "C++": lang_param = "cpp"13 case "C#": lang_param = "cs"14 case _: lang_param = lang.lower()15 16 # FIXME: ERROR HANDLING17 res = requests.request("POST", f"https://try.w3schools.com/try_{lang_param}.php", data={18 "code": code_snippet19 })20 match lang_param:21 case "php": return res.text22 case _: return BeautifulSoup(res.text, "html.parser").find_all("pre")[0].string23 24demo = gr.Interface(25 fn=execute_snippet,26 inputs=[27 gr.Textbox(28 show_label=True,29 label="Code",30 max_lines=4_294_967_295,31 lines=4_294_967_295,32 value=DEF_SNIPPET,33 ),34 gr.Dropdown(35 show_label=True,36 label="Language",37 choices=["Python", "Java", "C", "C++", "C#", "PHP"],38 value=DEF_LANG39 ),40 ],41 outputs=gr.Textbox(label="Result"),42 title="HFChat Code Executor",43 description="Enter the code snippet and language that you want to execute.",44)45 46demo.launch()