Skip to content

Commit

Permalink
Streamlit Updates (#28)
Browse files Browse the repository at this point in the history
Streamlit Updates
  • Loading branch information
manishshettym authored Feb 26, 2024
2 parents 297847a + effe389 commit 96140b8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 74 deletions.
5 changes: 0 additions & 5 deletions codescholar/apps/app_bench.json

This file was deleted.

17 changes: 4 additions & 13 deletions codescholar/apps/app_decl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
from codescholar.search.search import main as search_main
from codescholar.apps.utils import (
find_api,
clean_idiom,
write_idiom,
explain_idiom,
get_result_from_dir,
get_plot_metrics,
)
Expand Down Expand Up @@ -115,19 +114,11 @@ def search_status():
)


@scholarapp.route("/clean", methods=["POST"])
def clean():
@scholarapp.route("/explain", methods=["POST"])
def explain():
api = flask.request.json["api"]
idiom = flask.request.json["idiom"]
resp = {"idiom": clean_idiom(api, idiom)}
return flask.jsonify(resp)


@scholarapp.route("/write", methods=["POST"])
def write():
api = flask.request.json["api"]
idiom = flask.request.json["idiom"]
resp = {"idiom": write_idiom(api, idiom)}
resp = {"idiom": explain_idiom(api, idiom)}
return flask.jsonify(resp)


Expand Down
47 changes: 19 additions & 28 deletions codescholar/apps/streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,12 @@ def get_idioms(api, size):
):
API = st.session_state.search_api

size = st.slider("Choose the size of your idiom:", 4, 20, 4)
size, sortby = None, None
col1, col2 = st.columns([3, 1])
with col1:
size = st.slider("Choose the size of your idiom:", 4, 20, 4)
with col2:
sortby = st.selectbox("Sort idioms by:", ("Program Length", "Frequency"))

status_placeholder = st.empty()

Expand All @@ -192,12 +197,15 @@ def get_idioms(api, size):
if len(idioms) == 0:
st.error("No idioms found for API: {} 🫙".format(API))
else:
idioms = [
v
for k, v in sorted(
idioms.items(), key=lambda item: int(item[1]["freq"]), reverse=True
)
]

idiom_freq = lambda item: int(item[1]["freq"])
prog_len = lambda item: len(item[1]["idiom"])

if sortby == "Frequency":
idioms = [v for k, v in sorted(idioms.items(), key=idiom_freq, reverse=True)]
else:
idioms = [v for k, v in sorted(idioms.items(), key=prog_len, reverse=False)]

tabs = st.tabs(["Idiom {}".format(i + 1) for i in range(len(idioms))])

for idiom, tab in zip(idioms, tabs):
Expand All @@ -207,36 +215,19 @@ def get_idioms(api, size):
maxh = max(100, min(30 * idiom["idiom"].count("\n"), 500))
components.html(highlighted_idiom_html, height=maxh, scrolling=True)

colbut1, colbut2 = st.columns([0.25, 0.8])
with colbut1:
but1 = st.button("Clean this idiom?", key=f"clean_{tabs.index(tab)}")
with colbut2:
but2 = st.button(
"Code with this idiom?", key=f"write_{tabs.index(tab)}"
)
but1 = st.button("Explain this idiom?", key=f"explain_{tabs.index(tab)}")

if but1:
with st.spinner("Cleaning your idioms 🧹..."):
with st.spinner("Summarizing your idiom 👩🏻‍💻..."):
response = requests.post(
f"http://{endpoint}/clean",
f"http://{endpoint}/explain",
json={"api": API, "idiom": idiom["idiom"]},
)
# st.error("This feature is not available yet! 🫙")
st.code(response.json()["idiom"], language="python")
if but2:
with st.spinner("Writing some code 👩🏻‍💻..."):
response = requests.post(
f"http://{endpoint}/write",
json={"api": API, "idiom": idiom["idiom"]},
)
# st.error("This feature is not available yet! 🫙")
st.code(response.json()["idiom"], language="python")
st.markdown(response.json()["idiom"])

st.divider()

"""
##### CodeScholar Suggestions
"""
with st.spinner("Analyzing your idioms 📊..."):
response = requests.post(f"http://{endpoint}/plot", json={"api": API})

Expand Down
37 changes: 9 additions & 28 deletions codescholar/apps/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,16 @@
\"\"\"
"""

gpt_prompt_clean = """
The following is an example of a idiomatic usage example for {api}
gpt_prompt_explain = """
The following is an example of a idiomatic usage example for {api}.
The idiomatic usage is annotated in the code example.
\"\"\"
{idiom}
\"\"\"
Rename the variables and constants to make it more generic. Return only the code with no explanations.
\"\"\"
"""

gpt_prompt_write = """
The following is an example of a idiomatic usage example for {api}
\"\"\"
{idiom}
\"\"\"
Write a 2-4 line snippet of code using exactly the above given example. Return only the code with no explanations.
Write a 4-6 line explanation of how the API is used in the code example.
Write an explanation that is specific to this code example.
Explain the setting not just the usage. Use markdown syntax to format the explanation
and highlight the important parts. Return only the explanation with no code.
\"\"\"
"""

Expand All @@ -60,24 +55,10 @@ def find_api(query):
return response.choices[0].text


def write_idiom(api, idiom):
response = openai.Completion.create(
model="gpt-3.5-turbo-instruct",
prompt=gpt_prompt_write.format(api=api, idiom=idiom),
temperature=0,
max_tokens=250,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=['"""', "```"],
)
return response.choices[0].text


def clean_idiom(api, idiom):
def explain_idiom(api, idiom):
response = openai.Completion.create(
model="gpt-3.5-turbo-instruct",
prompt=gpt_prompt_clean.format(api=api, idiom=idiom),
prompt=gpt_prompt_explain.format(api=api, idiom=idiom),
temperature=0,
max_tokens=250,
top_p=1.0,
Expand Down

0 comments on commit 96140b8

Please sign in to comment.