Skip to content

Commit

Permalink
fix and some cleanups from migration (#7287)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 7900bcadc60c5b2291cce5e50f1e400b6a6f4f2e
  • Loading branch information
berkecanrizai authored and Manul from Pathway committed Sep 16, 2024
1 parent 7ddca2a commit b2038be
Showing 1 changed file with 11 additions and 32 deletions.
43 changes: 11 additions & 32 deletions examples/pipelines/slides_ai_search/ui/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
from dotenv import load_dotenv
from pathway.xpacks.llm.question_answering import RAGClient

try:
load_dotenv()
except Exception:
pass
load_dotenv()

PATHWAY_HOST = os.environ.get("PATHWAY_HOST", "app")
PATHWAY_PORT = os.environ.get("PATHWAY_PORT", 8000)
Expand Down Expand Up @@ -81,7 +78,7 @@
)


question = st.text_input(label="", placeholder="Why buy")
question = st.text_input(label="", placeholder="What are you searching for?")


def get_options_list(metadata_list: list[dict], opt_key: str) -> list:
Expand All @@ -93,7 +90,7 @@ def get_options_list(metadata_list: list[dict], opt_key: str) -> list:
def parse_slide_id_components(slide_id: str) -> tuple[str, int, int]:
stem = PurePosixPath(slide_id).stem
(name_page, _, page_count) = stem.rpartition("_")
(name, _, page) = stem.rpartition("_")
(name, _, page) = name_page.rpartition("_")
return (name, int(page), int(page_count))


Expand Down Expand Up @@ -149,10 +146,10 @@ def get_slide_link(file_name, page_num=None) -> str:
return image_url


def get_all_drive_files() -> list[str]:
logger.info("request get_all_drive_files")
response = requests.get(file_server_pdf_base_url)
logger.info("response get_all_drive_files")
def get_all_index_files() -> list[str]:
logger.info("request get_all_index_files")
response = requests.get(file_server_pdf_base_url + "/")
logger.info("response get_all_index_files")

if response.status_code == 200:
soup = BeautifulSoup(response.content, "html.parser")
Expand Down Expand Up @@ -183,7 +180,7 @@ def get_all_drive_files() -> list[str]:
markdown_table += f"| [{file_name}]({link}) |\n"
st.markdown(markdown_table, unsafe_allow_html=True)

all_drive_files = get_all_drive_files()
all_drive_files = get_all_index_files()
all_drive_files = [urllib.parse.unquote(i) for i in all_drive_files]
all_drive_files = [
i for i in all_drive_files if i.endswith(".pdf") or i.endswith(".pptx")
Expand Down Expand Up @@ -250,37 +247,19 @@ def get_all_drive_files() -> list[str]:


def get_category_filter(category: str) -> str:
return f"contains({str(category)}, category)"
if category == "No Filter":
return None
else:
return f"category == `{category}`"
return f"contains(`{str(category)}`, category)"


# TODO: merge these
def get_language_filter(lang: str) -> str:
return f"contains({str(lang)}, language)"
if lang == "No Filter":
return None
else:
return f"language == `{lang}`"
return f"contains(`{str(lang)}`, language)"


def combine_filters(*args: str | None) -> str:
"""Construct single jmespath filter with `&&` from number of filters."""
return " && ".join([arg for arg in args if arg is not None])


@st.cache_resource()
def get_b64img_with_href(bin_str, target_url, width: int = 350, margin=20):
html_code = f"""<a href="{target_url}"><img style="display: block; margin: {margin}px auto {margin}px auto" loading="lazy" src="data:image/png;base64,{bin_str}" width="{width}" /></a>""" # noqa: E501
return html_code


icon_thumbs_up = '<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24"><path fill="currentColor" d="M7.493 18.5c-.425 0-.82-.236-.975-.632A7.5 7.5 0 0 1 6 15.125a7.47 7.47 0 0 1 1.602-4.634c.151-.192.373-.309.6-.397c.473-.183.89-.514 1.212-.924a9 9 0 0 1 2.861-2.4c.723-.384 1.35-.956 1.653-1.715a4.5 4.5 0 0 0 .322-1.672V2.75A.75.75 0 0 1 15 2a2.25 2.25 0 0 1 2.25 2.25c0 1.152-.26 2.243-.723 3.218c-.266.558.107 1.282.725 1.282h3.126c1.026 0 1.945.694 2.054 1.715q.068.633.068 1.285a11.95 11.95 0 0 1-2.649 7.521c-.388.482-.987.729-1.605.729H14.23a4.5 4.5 0 0 1-1.423-.23l-3.114-1.04a4.5 4.5 0 0 0-1.423-.23zm-5.162-7.773a12 12 0 0 0-.831 4.398a12 12 0 0 0 .52 3.507C2.28 19.482 3.105 20 3.994 20H4.9c.445 0 .72-.498.523-.898a9 9 0 0 1-.924-3.977c0-1.708.476-3.305 1.302-4.666c.245-.403-.028-.959-.5-.959H4.25c-.832 0-1.612.453-1.918 1.227"/></svg>' # noqa: E501

icon_thumbs_down = '<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24"><path fill="currentColor" d="M15.73 5.5h1.035A7.47 7.47 0 0 1 18 9.625a7.47 7.47 0 0 1-1.235 4.125h-.148c-.806 0-1.533.446-2.031 1.08a9 9 0 0 1-2.861 2.4c-.723.384-1.35.956-1.653 1.715a4.5 4.5 0 0 0-.322 1.672v.633A.75.75 0 0 1 9 22a2.25 2.25 0 0 1-2.25-2.25c0-1.152.26-2.243.723-3.218c.266-.558-.107-1.282-.725-1.282H3.622c-1.026 0-1.945-.694-2.054-1.715A12 12 0 0 1 1.5 12.25c0-2.848.992-5.464 2.649-7.521C4.537 4.247 5.136 4 5.754 4H9.77a4.5 4.5 0 0 1 1.423.23l3.114 1.04a4.5 4.5 0 0 0 1.423.23m5.939 8.523c.536-1.362.831-2.845.831-4.398c0-1.22-.182-2.398-.52-3.507c-.26-.85-1.084-1.368-1.973-1.368H19.1c-.445 0-.72.498-.523.898c.591 1.2.924 2.55.924 3.977a8.96 8.96 0 0 1-1.302 4.666c-.245.403.028.959.5.959h1.053c.832 0 1.612-.453 1.918-1.227"/></svg>' # noqa: E501

css = """
<style>
.slider-container {
Expand Down Expand Up @@ -447,5 +426,5 @@ def log_rate_answer(event, idx, kwargs):
else:
st.markdown(
f"""No results were found for search query: `{question}`
and filter criteria: `{combine_filters(*filter_ls)}`"""
and filter criteria: `{combined_query_filter}`"""
)

0 comments on commit b2038be

Please sign in to comment.