testnow720 commited on
Commit
6e2696a
·
verified ·
1 Parent(s): 2601478

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -8
app.py CHANGED
@@ -1,15 +1,44 @@
1
  import gradio as gr
2
- import random
 
3
 
4
- def roll_dice(sides=6):
5
- return random.randint(1, sides)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  demo = gr.Interface(
8
- fn=roll_dice,
9
- inputs=gr.Number(value=6, label="Number of Sides"),
10
- outputs="text",
11
- title="Dice Roller",
12
- description="Enter the number of sides for the dice and get the roll result."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()