Skip to content

Commit

Permalink
Merge branch 'main' of github.com:WSE-research/LLM-based-explanations…
Browse files Browse the repository at this point in the history
…-for-SPARQL-queries
  • Loading branch information
anbo-de committed Mar 7, 2024
2 parents 749e2cc + e1e4e40 commit d34e7fe
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 26 deletions.
25 changes: 25 additions & 0 deletions css/style_buttons.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
button[kind="primary"] {
background-color: #33FF33;
border-color: #339933;
color: #000000;
}

button[kind="primary"]:hover {
background-color: #66FF66;
border-color: #336633;
color: #000000;
box-shadow: 0px 0px 1ex 0.5ex #CDC;
}

button[kind="secondary"] {
background-color: #FF3333;
border-color: #993333;
color: #000000;
}

button[kind="secondary"]:hover {
background-color: #FF6666;
border-color: #663333;
color: #000000;
box-shadow: 0px 0px 1ex 0.5ex #DCC;
}
2 changes: 1 addition & 1 deletion css/style_github_ribbon.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
overflow: hidden;
top: 0;
right: 0;
z-index: 9999;
z-index: 50;
pointer-events: none;
font-size: 13px;
text-decoration: none;
Expand Down
70 changes: 47 additions & 23 deletions loris--llm-based-explanations-for-sparql-queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
ADDITIONAL_HTML_HEAD_CONTENT = config('ADDITIONAL_HTML_HEAD_CONTENT', default=None)
WRAP = True

PAGE_TITLE = "LoriS -- LLM-based explanations for SPARQL queries"
PAGE_TITLE = "LoRiS -- LLM-based natural-language representations for SPARQL queries over Wikidata"

GPT3_5_TURBO = "GPT-3.5-turbo (from OpenAI)"
GPT3_5_TURBO = "GPT-3.5 (from OpenAI)"
GPT4 = "GPT-4 (from OpenAI)"
MISTRAL = "Mistral 7B (from Mistral AI)"
MISTRAL_FINETUNED = "Mistral 7B, fine-tuned"
ONESHOT = "One-shot"
ZEROSHOT = "Zero-shot"
ONESHOT = "①" # "One-shot"
ZEROSHOT = "⓪" # "Zero-shot"


MODEL_KEY = "model"
Expand All @@ -47,9 +47,7 @@
MISTRAL_MODEL = "mistral-7b"
MISTRAL_MODEL_FINETUNED = "mistral-7b-finetuned"

SEPARATOR = """,
"""
SEPARATOR = """, shots: """
GPT3_5_ZERO_SHOT = GPT3_5_TURBO + SEPARATOR + ZEROSHOT
GPT3_5_ONE_SHOT = GPT3_5_TURBO + SEPARATOR + ONESHOT
GPT4_ZERO_SHOT = GPT4 + SEPARATOR + ZEROSHOT
Expand Down Expand Up @@ -104,12 +102,25 @@
DE_STRING: "de",
RU_STRING: "ru"
}
language_text = {
EN_STRING: "🇺🇸",
DE_STRING: "🇩🇪",
RU_STRING: "🇷🇺"
}

explanation_languages = explanation_languages_dict.keys()

example_code = """SELECT *
WHERE {
?s ?p ?o .
}
example_code = """PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT DISTINCT ?uri
WHERE {
?type wdt:P279* wd:Q4830453 .
?aerospace wdt:P279* wd:Q3477363 .
?healthcare wdt:P279* wd:Q15067276 .
?pharma wdt:P279* wd:Q507443 .
?uri wdt:P452 ?aerospace ;
wdt:P452 ?healthcare .
}
"""

agree_on_showing_additional_information = True
Expand All @@ -130,7 +141,7 @@
page_title=PAGE_TITLE,
page_icon=Image.open(PAGE_ICON)
)
include_css(st, ["css/style_github_ribbon.css", "css/style_menu_logo.css", "css/style_logo.css", "css/style_tabs.css"])
include_css(st, ["css/style_github_ribbon.css", "css/style_menu_logo.css", "css/style_logo.css", "css/style_tabs.css", "css/style_buttons.css"])

# if the dry run is enabled, we will stop the script
if config('DRY_RUN', default=False, cast=bool):
Expand All @@ -154,13 +165,13 @@ def get_explanation(sparql_query, model, lang):

if response.status_code == 200 and len(response.json()) > 0 and "explanation" in response.json()[0] and "message" not in response.json()[0] and "explanation" in response.json()[0]:
explanations = {
"subtitle": "Explanation generated by " + model_name,
"subtitle": "Natural-language representation generated by *" + model_name + "*",
"explanation": response.json()[0]["explanation"],
"ok": True
}
else:
explanations = {
"subtitle": "Failed generating explanation by " + model_name,
"subtitle": "Failed to generate the natural-language representation by *" + model_name + "*",
"explanation": response.json()[0]["message"],
"ok": False
}
Expand Down Expand Up @@ -217,7 +228,7 @@ def update_width_input():
"commands": ["copyAll"],
"style": {"top": "0rem", "right": "0.4rem"}
},{
"name": "explain me",
"name": "translate me",
"feather": "Play",
"primary": True,
"hasText": True,
Expand All @@ -228,10 +239,10 @@ def update_width_input():
}]

# introduce the tool
page_header = """## LoriS &#8212; LLM-based explanations for SPARQL queries
page_header = """## {}
{}
""".format(DESCRIPTION)
""".format(PAGE_TITLE.replace("--", "—"), DESCRIPTION)

# show the page header only if the user is not minimizing the layout
if agree_on_showing_additional_information:
Expand All @@ -257,7 +268,7 @@ def update_width_input():
)
)

st.subheader("Explanation for the given SPARQL query")
st.subheader(f"Natural-language representation ({language}) for the given SPARQL query")
if response_dict["type"] == "submit":
# chosen_id = stx.tab_bar(data=tab_data, key="tab_bar", default=default_model)
# logging.info("chosen_id: " + chosen_id)
Expand All @@ -266,7 +277,10 @@ def update_width_input():
# else:
logging.debug(selected_model)

explanation = get_explanation(sparql_query, selected_model, language)
with st.spinner("Generating natural-language representation of the SPARQL query..."):
explanation = get_explanation(sparql_query, selected_model, language)
st.toast(f"The representation has been generated successfully using *{selected_model[MODEL_KEY]}*.", icon="🎉")

try:
subtitle = explanation["subtitle"]
explanation_text = explanation["explanation"]
Expand All @@ -279,13 +293,23 @@ def update_width_input():
ok = False

if ok:
st.markdown("#### " + subtitle)
st.write(explanation_text)
st.markdown(subtitle + ":")
left, right = st.columns([1,1])

left.success(explanation_text)
with right:
st.warning("We think the generated natural-language representation is correct. Do you agree?")
placeholder, correct_column, incorrect_column = st.columns([2,2,3])
with correct_column:
st.button("✅ Yes", key="correct", type="primary")
with incorrect_column:
st.button("❌ No", key="incorrect", type="secondary")

else:
st.error("An error occurred: " + explanation_text)
st.error("An error occurred: " + explanation_text, icon="⚠")

else:
st.warning("Please click on the 'explain me' button to generate the explanation.")
st.warning("Please click on the 'translate me' button to generate the natural-language representation.")

st.markdown("""
---
Expand Down
4 changes: 2 additions & 2 deletions service_config/files/env
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PAGE_ICON=images/loris-small.png
PAGE_IMAGE=images/loris.png
GITHUB_REPO=https://github.com/WSE-research/LLM-based-explanations-for-SPARQL-queries
DESCRIPTION="\nLoriS explains your SPARQL query using multiple LLMs (Large Language Models), s.t., it becomes easier to understand [SPARQL queries](https://www.w3.org/TR/rdf-sparql-query/). For configuration, please see the sidebar. \n\nFor more details, see our [GitHub repository](%s) where you can also [submit bug reports](%s) and [feature requests](%s)."
META_DESCRIPTION="Loris -- LLM-based explanations for SPARQL queries -- explains your SPARQL query using multiple LLMs, s.t., it becomes easier to understand SPARQL queries."
DESCRIPTION="\nLoRiS translates your SPARQL query over the Wikidata knowledge graph into a natural-language representation using multiple LLMs (Large Language Models), s.t., it becomes easier to understand [SPARQL queries](https://www.w3.org/TR/rdf-sparql-query/). For configuration, please see the sidebar. For more details, see our [GitHub repository](%s) where you can also [submit bug reports](%s) and [feature requests](%s)."
META_DESCRIPTION="LoRiS -- LLM-based natural-language representations for SPARQL queries -- translates your SPARQL query over Wikidata using multiple LLMs, s.t., it becomes easier to understand SPARQL queries."
REPLACE_INDEX_HTML_CONTENT=True
CANONICAL_URL=https://wse-research.org/LoriS--LLM-based-explanations-for-SPARQL-queries
BACKEND_URL=http://141.57.8.18:8099

0 comments on commit d34e7fe

Please sign in to comment.