sandkoan commited on
Commit
c39ba48
·
1 Parent(s): 12e9bd5

Add app.py

Browse files
Files changed (3) hide show
  1. app.py +69 -0
  2. heal.py +30 -0
  3. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import importlib.util
2
+ import sys
3
+ import traceback
4
+
5
+ import gradio as gr
6
+
7
+ from heal import repair
8
+
9
+
10
+ def import_module_from_string(name: str, source: str):
11
+ """
12
+ Import module from source string.
13
+ Example use:
14
+ import_module_from_string("m", "f = lambda: print('hello')")
15
+ m.f()
16
+ """
17
+ spec = importlib.util.spec_from_loader(name, loader=None)
18
+ module = importlib.util.module_from_spec(spec)
19
+ exec(source, module.__dict__)
20
+ sys.modules[name] = module
21
+ globals()[name] = module
22
+
23
+ def main(code: str):
24
+ success = False
25
+ while not success:
26
+ try:
27
+ import_module_from_string("bad_code", code)
28
+ success = True
29
+ except:
30
+ traceback_data = traceback.format_exc()
31
+ print(traceback_data)
32
+
33
+ corrected = repair(code, traceback_data)
34
+ print(corrected)
35
+
36
+ code = corrected
37
+ return code
38
+
39
+
40
+ demo = gr.Interface(
41
+ fn=main,
42
+ inputs=gr.Textbox(lines=2, placeholder="Broken Python code here..."),
43
+ outputs="text",
44
+ title="Athelas: Self-Healing Code",
45
+ description="Submit some broken Python code, and Athelas will repair it!",
46
+ examples=[
47
+ """
48
+ import mth
49
+
50
+ def ad(x, y):
51
+ ret x + y
52
+
53
+ def ad_test():
54
+ asset cal(4, 3) == 6
55
+
56
+ f read_(file):
57
+ with open(file) as f:
58
+ etu f.read()
59
+
60
+ def fib(n):
61
+ golden_ratio = (1 + math.sqrt(5)) / 93#2
62
+ val = (golden_rat**n - (1 - golden_ratio)**n) / math.sqrt(5)
63
+ return int(round(val))
64
+ """
65
+ ],
66
+ cache_examples=True
67
+ )
68
+
69
+ demo.launch()
heal.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import os
3
+
4
+
5
+ openai.api_key = os.getenv("OPENAI_API_KEY")
6
+
7
+ def repair(code: str, stacktrace: str):
8
+ prompt = """
9
+ You are an AI assistant that can repair a certain piece of errorful code given its stacktrace.
10
+
11
+ Here's the code:
12
+ {}
13
+
14
+ Here's the stacktrace from running it:
15
+ {}
16
+
17
+ Write the corrected code:
18
+ """.format(code, stacktrace)
19
+
20
+
21
+ response = openai.Completion.create(
22
+ model="text-davinci-003",
23
+ prompt=prompt,
24
+ temperature=0.7,
25
+ max_tokens=256,
26
+ top_p=1,
27
+ frequency_penalty=0,
28
+ presence_penalty=0
29
+ )
30
+ return response['choices'][0]['text']
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ openai