Spaces:
Runtime error
Runtime error
switch to streamlit
Browse files- README.md +1 -1
- app.py +63 -39
- gradio_app.py +107 -0
- requirements.txt +1 -2
README.md
CHANGED
|
@@ -3,7 +3,7 @@ title: Code Clippy Problem Solver
|
|
| 3 |
emoji: 💻
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
-
sdk:
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
| 9 |
---
|
|
|
|
| 3 |
emoji: 💻
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: green
|
| 6 |
+
sdk: streamlit
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
| 9 |
---
|
app.py
CHANGED
|
@@ -1,41 +1,33 @@
|
|
| 1 |
-
import
|
| 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"
|
| 8 |
model_name = "flax-community/gpt-code-clippy-125M-apps-alldata"
|
| 9 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 10 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def format_input(question, starter_code=""):
|
| 17 |
-
answer_type =
|
| 18 |
-
|
| 19 |
-
)
|
| 20 |
return f"\nQUESTION:\n{question}\n{starter_code}\n{answer_type}\nANSWER:\n"
|
| 21 |
|
| 22 |
|
| 23 |
-
def
|
| 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 +
|
| 39 |
do_sample=True,
|
| 40 |
top_p=0.95,
|
| 41 |
pad_token_id=tokenizer.pad_token_id,
|
|
@@ -47,9 +39,7 @@ def generate_solution(question, starter_code="", temperature=1.0, num_beams=1):
|
|
| 47 |
num_return_sequences=None,
|
| 48 |
)
|
| 49 |
|
| 50 |
-
return
|
| 51 |
-
tokenizer.decode(output[0][start:], skip_special_tokens=True).strip()
|
| 52 |
-
)
|
| 53 |
|
| 54 |
|
| 55 |
_EXAMPLES = [
|
|
@@ -87,21 +77,55 @@ def greet(name, owner):
|
|
| 87 |
0.8,
|
| 88 |
],
|
| 89 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
inputs=inputs,
|
| 104 |
-
outputs=outputs,
|
| 105 |
-
title="Code Clippy: Problem Solver",
|
| 106 |
-
examples=_EXAMPLES,
|
| 107 |
-
).launch(share=False)
|
|
|
|
| 1 |
+
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
|
| 4 |
# model_name = "flax-community/gpt-code-clippy-1.3B-apps-alldata"
|
| 5 |
model_name = "flax-community/gpt-code-clippy-125M-apps-alldata"
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
@st.cache(allow_output_mutation=True)
|
| 8 |
+
def get_model():
|
| 9 |
+
return AutoModelForCausalLM.from_pretrained(model_name)
|
| 10 |
|
| 11 |
+
@st.cache
|
| 12 |
+
def get_tokenizer():
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 14 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 15 |
+
return tokenizer
|
| 16 |
|
| 17 |
def format_input(question, starter_code=""):
|
| 18 |
+
answer_type = "\nUse Call-Based format\n" if starter_code else \
|
| 19 |
+
"\nUse Standard Input format\n"
|
|
|
|
| 20 |
return f"\nQUESTION:\n{question}\n{starter_code}\n{answer_type}\nANSWER:\n"
|
| 21 |
|
| 22 |
|
| 23 |
+
def generate_solution(model, tokenizer, question, starter_code="", temperature=1.0, num_beams=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
prompt = format_input(question, starter_code)
|
| 25 |
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
| 26 |
start = len(input_ids[0])
|
| 27 |
+
|
| 28 |
output = model.generate(
|
| 29 |
input_ids,
|
| 30 |
+
max_length=start + 150,
|
| 31 |
do_sample=True,
|
| 32 |
top_p=0.95,
|
| 33 |
pad_token_id=tokenizer.pad_token_id,
|
|
|
|
| 39 |
num_return_sequences=None,
|
| 40 |
)
|
| 41 |
|
| 42 |
+
return tokenizer.decode(output[0][start:], skip_special_tokens=True).strip()
|
|
|
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
_EXAMPLES = [
|
|
|
|
| 77 |
0.8,
|
| 78 |
],
|
| 79 |
]
|
| 80 |
+
def run():
|
| 81 |
+
st.set_page_config(
|
| 82 |
+
page_title="Code Clippy Problem Solver"
|
| 83 |
+
)
|
| 84 |
+
# sidebar
|
| 85 |
+
st.sidebar.title("Code Clippy")
|
| 86 |
+
st.sidebar.image(
|
| 87 |
+
"https://raw.githubusercontent.com/ncoop57/gpt-code-clippy/camera-ready/code_clippy_logo.jpg",
|
| 88 |
+
caption="(c) awesome Aimee Trevett",
|
| 89 |
+
)
|
| 90 |
+
st.sidebar.markdown("[Github](https://github.com/ncoop57/gpt-code-clippy)")
|
| 91 |
+
|
| 92 |
+
st.sidebar.markdown("### Controls:")
|
| 93 |
+
|
| 94 |
+
temperature = st.sidebar.slider(
|
| 95 |
+
"Temperature",
|
| 96 |
+
min_value=0.5,
|
| 97 |
+
max_value=1.5,
|
| 98 |
+
value=0.8,
|
| 99 |
+
step=0.1,
|
| 100 |
+
)
|
| 101 |
+
num_beams = st.sidebar.slider(
|
| 102 |
+
"Num beams",
|
| 103 |
+
min_value=1,
|
| 104 |
+
max_value=4,
|
| 105 |
+
step=1,
|
| 106 |
+
)
|
| 107 |
|
| 108 |
+
# main body
|
| 109 |
+
model = get_model()
|
| 110 |
+
tokenizer = get_tokenizer()
|
| 111 |
|
| 112 |
+
question = st.text_input(
|
| 113 |
+
"Problem: ",
|
| 114 |
+
value="A function that can greet user by name. Given a name it should say hello to user.",
|
| 115 |
+
help="Text description of the coding problem to be solved",
|
| 116 |
+
)
|
| 117 |
+
starter_code = st.text_input(
|
| 118 |
+
"Started code: ",
|
| 119 |
+
value="def greet(name):",
|
| 120 |
+
help="Optional starter code"
|
| 121 |
+
)
|
| 122 |
+
submit_button = st.button("Solve")
|
| 123 |
|
| 124 |
+
if submit_button:
|
| 125 |
+
|
| 126 |
+
generate_solution(model, tokenizer, question, starter_code, temperature, num_beams)
|
| 127 |
+
st.code(tmp, language="python")
|
| 128 |
+
|
| 129 |
|
| 130 |
+
if __name__=="__main__":
|
| 131 |
+
run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gradio_app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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"
|
| 8 |
+
model_name = "flax-community/gpt-code-clippy-125M-apps-alldata"
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 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,
|
| 42 |
+
early_stopping=True,
|
| 43 |
+
temperature=temperature,
|
| 44 |
+
num_beams=int(num_beams),
|
| 45 |
+
no_repeat_ngram_size=None,
|
| 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 = [
|
| 56 |
+
[
|
| 57 |
+
"""
|
| 58 |
+
Given a 2D list of size `m * n`. Your task is to find the sum of minimum value in each row.
|
| 59 |
+
For Example:
|
| 60 |
+
```python
|
| 61 |
+
[
|
| 62 |
+
[1, 2, 3, 4, 5], # minimum value of row is 1
|
| 63 |
+
[5, 6, 7, 8, 9], # minimum value of row is 5
|
| 64 |
+
[20, 21, 34, 56, 100] # minimum value of row is 20
|
| 65 |
+
]
|
| 66 |
+
```
|
| 67 |
+
So, the function should return `26` because sum of minimums is as `1 + 5 + 20 = 26`
|
| 68 |
+
""",
|
| 69 |
+
"",
|
| 70 |
+
0.8,
|
| 71 |
+
],
|
| 72 |
+
[
|
| 73 |
+
"""
|
| 74 |
+
# Personalized greeting
|
| 75 |
+
|
| 76 |
+
Create a function that gives a personalized greeting. This function takes two parameters: `name` and `owner`.
|
| 77 |
+
""",
|
| 78 |
+
"""
|
| 79 |
+
Use conditionals to return the proper message:
|
| 80 |
+
|
| 81 |
+
case| return
|
| 82 |
+
--- | ---
|
| 83 |
+
name equals owner | 'Hello boss'
|
| 84 |
+
otherwise | 'Hello guest'
|
| 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,
|
| 107 |
+
).launch(share=False)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,2 @@
|
|
| 1 |
torch
|
| 2 |
-
transformers
|
| 3 |
-
rich
|
|
|
|
| 1 |
torch
|
| 2 |
+
transformers
|
|
|