Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Code Formatting to Model outputs #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ build/
.idea/

.env

.vscode/
7 changes: 6 additions & 1 deletion .streamlit/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
[theme]
base="light"
base="light"
primaryColor="#F63366"
backgroundColor="#FFFFFF"
secondaryBackgroundColor="#F0F2F6"
textColor="#262730"
font="sans serif"
47 changes: 34 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@

columns = []


def get_max_line_height(responses):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets leave this out since we don't need responses to be same height.

max_num_lines = 1
for response in responses:
content = response["content"]
num_lines = len(content.split("\n"))
max_num_lines = max(max_num_lines, num_lines)
return 35 * max_num_lines


def grade_drafts(gladiator, responses):
with st.spinner("Grading Drafts..."):
columns = st.columns(3)
max_line_height = get_max_line_height(responses=responses)
for i, column in enumerate(columns):
draft_template = drafts_template(i, responses[i])
draft_template = drafts_template(i, responses[i], max_line_height)
with column:
st.markdown(draft_template, unsafe_allow_html=True)

grades_json = gladiator.grade_drafts()
grades_json = gladiator.grade_drafts()
return grades_json, columns


Expand All @@ -23,27 +34,43 @@ def select_winner(grades_json, columns):

for i, column in enumerate(columns):
is_winner = i == winning_index
grade_template = grades_template(grades_json[i]['score'], grades_json[i]['explanation'], is_winner)
grade_template = grades_template(
grades_json[i]["score"], grades_json[i]["explanation"], is_winner
)
with column:
st.markdown(grade_template, unsafe_allow_html=True)

st.subheader("Winning Response:")
with st.container():
st.write(winning_content)
st.code(winning_content)


def generate_drafts(gladiator):
with st.spinner("Generating Drafts..."):

responses = gladiator.generate_drafts(prompt)
responses = gladiator.generate_drafts(prompt)
return responses


dotenv.load_dotenv()

st.set_page_config(layout="wide")

st.markdown(stylesheet, unsafe_allow_html=True)

# Prism Library for Code Formatting
st.markdown(
"""
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/themes/prism.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.17.1/components/prism-python.min.js"></script>
""",
unsafe_allow_html=True,
)

st.title("Gladiator: May the best response win")
prompt = st.text_area("Enter a prompt and get the best answer", defaults.default_question, height=300)
prompt = st.text_area(
"Enter a prompt and get the best answer", defaults.default_question, height=300
)


if st.button("Generate best answer"):
Expand All @@ -55,9 +82,3 @@ def generate_drafts(gladiator):
except Exception as e:
print("Error: ", e)
st.error("An error occurred: {}".format(str(e)))






10 changes: 6 additions & 4 deletions ui.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
def drafts_template(i, content):
return f"<div class='draft'><div><b>Draft {i + 1}</b></div>{content['content']}</div>"
def drafts_template(i, response, max_line_height):
return f"<div class='draft' style='height:{max_line_height}px;'><div><b>Draft {i + 1}</b></div><div><pre><code class='language-python'>{response['content']}</code></pre></div></div>"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we shouldn't hardcode the code language here



def grades_template(score, explanation, is_winner):
class_name = 'winner' if is_winner else ''
class_name = "winner" if is_winner else ""
return f"<div class='{class_name} score'><div><b>Score {score}</b></div>{explanation}</div>"


stylesheet = """
<style>
.draft {
Expand All @@ -25,4 +27,4 @@ def grades_template(score, explanation, is_winner):
border: 3px solid #33cc33;
}
</style>
"""
"""