Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,44 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
demo = gr.Interface(
|
| 8 |
-
fn=
|
| 9 |
-
inputs=
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
|
| 5 |
+
DEF_SNIPPET = "print('Hello, World!')"
|
| 6 |
+
DEF_LANG = "Python"
|
| 7 |
+
|
| 8 |
+
def execute_snippet(code_snippet: str = DEF_SNIPPET, lang: str = DEF_LANG) -> str:
|
| 9 |
+
lang_param = None
|
| 10 |
+
|
| 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 HANDLING
|
| 17 |
+
res = requests.request("POST", f"https://try.w3schools.com/try_{lang_param}.php", data={
|
| 18 |
+
"code": code_snippet
|
| 19 |
+
})
|
| 20 |
+
return BeautifulSoup(res.text, "html.parser").find_all("pre")[0].string
|
| 21 |
|
| 22 |
demo = gr.Interface(
|
| 23 |
+
fn=execute_snippet,
|
| 24 |
+
inputs=[
|
| 25 |
+
gr.Textbox(
|
| 26 |
+
show_label=True,
|
| 27 |
+
label="Code",
|
| 28 |
+
max_lines=4_294_967_295,
|
| 29 |
+
lines=4_294_967_295,
|
| 30 |
+
value=DEF_SNIPPET,
|
| 31 |
+
),
|
| 32 |
+
gr.Dropdown(
|
| 33 |
+
show_label=True,
|
| 34 |
+
label="Language",
|
| 35 |
+
choices=["Python", "Java", "C", "C++", "C#", "PHP"],
|
| 36 |
+
value=DEF_LANG
|
| 37 |
+
),
|
| 38 |
+
],
|
| 39 |
+
outputs=gr.Textbox(label="Result"),
|
| 40 |
+
title="HFChat Code Executor",
|
| 41 |
+
description="Enter the code snippet and language that you want to execute.",
|
| 42 |
)
|
| 43 |
|
| 44 |
demo.launch()
|