Spaces:
Runtime error
Runtime error
ncoop57
commited on
Commit
•
321a2ed
1
Parent(s):
6842806
Add some janky syntax highlighting via rich
Browse files- app.py +27 -27
- requirements.txt +1 -0
app.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
|
|
|
|
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
|
5 |
# model_name = "flax-community/gpt-code-clippy-1.3B-apps-alldata"
|
@@ -8,34 +10,32 @@ model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
tokenizer.pad_token = tokenizer.eos_token
|
10 |
|
|
|
|
|
11 |
|
12 |
def format_input(question, starter_code=""):
|
13 |
-
answer_type =
|
|
|
|
|
14 |
return f"\nQUESTION:\n{question}\n{starter_code}\n{answer_type}\nANSWER:\n"
|
15 |
|
16 |
|
17 |
def format_outputs(text):
|
18 |
-
formatted_text =
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
</body>
|
28 |
-
'''
|
29 |
-
return formatted_text
|
30 |
-
|
31 |
-
|
32 |
-
def generate_solution(question, starter_code="", temperature=1., num_beams=1):
|
33 |
prompt = format_input(question, starter_code)
|
34 |
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
35 |
start = len(input_ids[0])
|
36 |
output = model.generate(
|
37 |
input_ids,
|
38 |
-
max_length=start+200,
|
39 |
do_sample=True,
|
40 |
top_p=0.95,
|
41 |
pad_token_id=tokenizer.pad_token_id,
|
@@ -46,8 +46,10 @@ def generate_solution(question, starter_code="", temperature=1., num_beams=1):
|
|
46 |
repetition_penalty=None,
|
47 |
num_return_sequences=None,
|
48 |
)
|
49 |
-
|
50 |
-
return format_outputs(
|
|
|
|
|
51 |
|
52 |
|
53 |
_EXAMPLES = [
|
@@ -83,24 +85,22 @@ otherwise | 'Hello guest'
|
|
83 |
def greet(name, owner):
|
84 |
""",
|
85 |
0.8,
|
86 |
-
]
|
87 |
-
]
|
88 |
|
89 |
|
90 |
inputs = [
|
91 |
gr.inputs.Textbox(placeholder="Define a problem here...", lines=7),
|
92 |
gr.inputs.Textbox(placeholder="Provide optional starter code...", lines=3),
|
93 |
gr.inputs.Slider(0.5, 1.5, 0.1, default=0.8, label="Temperature"),
|
94 |
-
gr.inputs.Slider(1,4,1,default=1, label="Beam size")
|
95 |
]
|
96 |
|
97 |
-
outputs = [
|
98 |
-
gr.outputs.HTML(label="Solution")
|
99 |
-
]
|
100 |
|
101 |
gr.Interface(
|
102 |
-
generate_solution,
|
103 |
-
inputs=inputs,
|
104 |
outputs=outputs,
|
105 |
title="Code Clippy: Problem Solver",
|
106 |
examples=_EXAMPLES,
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
from rich.console import Console
|
4 |
+
from rich.syntax import Syntax
|
5 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
6 |
|
7 |
# model_name = "flax-community/gpt-code-clippy-1.3B-apps-alldata"
|
|
|
10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
tokenizer.pad_token = tokenizer.eos_token
|
12 |
|
13 |
+
console = Console(record=True)
|
14 |
+
|
15 |
|
16 |
def format_input(question, starter_code=""):
|
17 |
+
answer_type = (
|
18 |
+
"\nUse Call-Based format\n" if starter_code else "\nUse Standard Input format\n"
|
19 |
+
)
|
20 |
return f"\nQUESTION:\n{question}\n{starter_code}\n{answer_type}\nANSWER:\n"
|
21 |
|
22 |
|
23 |
def format_outputs(text):
|
24 |
+
formatted_text = Syntax(
|
25 |
+
text, "python", line_numbers=True, indent_guides=True, word_wrap=True
|
26 |
+
)
|
27 |
+
console.print(formatted_text)
|
28 |
+
|
29 |
+
return console.export_html(inline_styles=True)
|
30 |
+
|
31 |
+
|
32 |
+
def generate_solution(question, starter_code="", temperature=1.0, num_beams=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
prompt = format_input(question, starter_code)
|
34 |
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
35 |
start = len(input_ids[0])
|
36 |
output = model.generate(
|
37 |
input_ids,
|
38 |
+
max_length=start + 200,
|
39 |
do_sample=True,
|
40 |
top_p=0.95,
|
41 |
pad_token_id=tokenizer.pad_token_id,
|
|
|
46 |
repetition_penalty=None,
|
47 |
num_return_sequences=None,
|
48 |
)
|
49 |
+
|
50 |
+
return format_outputs(
|
51 |
+
tokenizer.decode(output[0][start:], skip_special_tokens=True).strip()
|
52 |
+
)
|
53 |
|
54 |
|
55 |
_EXAMPLES = [
|
|
|
85 |
def greet(name, owner):
|
86 |
""",
|
87 |
0.8,
|
88 |
+
],
|
89 |
+
]
|
90 |
|
91 |
|
92 |
inputs = [
|
93 |
gr.inputs.Textbox(placeholder="Define a problem here...", lines=7),
|
94 |
gr.inputs.Textbox(placeholder="Provide optional starter code...", lines=3),
|
95 |
gr.inputs.Slider(0.5, 1.5, 0.1, default=0.8, label="Temperature"),
|
96 |
+
gr.inputs.Slider(1, 4, 1, default=1, label="Beam size"),
|
97 |
]
|
98 |
|
99 |
+
outputs = [gr.outputs.HTML(label="Solution")]
|
|
|
|
|
100 |
|
101 |
gr.Interface(
|
102 |
+
generate_solution,
|
103 |
+
inputs=inputs,
|
104 |
outputs=outputs,
|
105 |
title="Code Clippy: Problem Solver",
|
106 |
examples=_EXAMPLES,
|
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
torch
|
2 |
transformers
|
|
|
|
1 |
torch
|
2 |
transformers
|
3 |
+
rich
|