-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ui for demo-question-answering (#7713)
Co-authored-by: berkecanrizai <[email protected]> GitOrigin-RevId: 57a508d9a63ffd15564e6a5fead3d3a2ff12731d
- Loading branch information
1 parent
f536ab9
commit 60adb51
Showing
7 changed files
with
242 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,4 @@ RUN pip install -U --no-cache-dir -r requirements.txt | |
|
||
COPY . . | ||
|
||
EXPOSE 8000 | ||
|
||
CMD ["python", "app.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
examples/pipelines/demo-question-answering/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
services: | ||
app: | ||
build: | ||
context: . | ||
ports: | ||
- "${PATHWAY_PORT:-8000}:${PATHWAY_PORT:-8000}" | ||
networks: | ||
- network | ||
volumes: | ||
- ./data:/app/data | ||
- ./Cache:/app/Cache | ||
|
||
ui: | ||
build: | ||
context: ui | ||
networks: | ||
- network | ||
environment: | ||
PATHWAY_HOST: "app" | ||
PATHWAY_PORT: "${PATHWAY_PORT:-8000}" | ||
UI_PORT: 8501 | ||
ports: | ||
- "8501:8501" | ||
|
||
networks: | ||
network: | ||
driver: bridge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
ARG PATHWAY_SRC_IMAGE=pathwaycom/pathway:latest | ||
|
||
FROM ${PATHWAY_SRC_IMAGE} | ||
|
||
ENV PYTHONUNBUFFERED=1 | ||
|
||
WORKDIR /ui | ||
|
||
COPY requirements.txt . | ||
RUN pip install -U --no-cache-dir -r requirements.txt | ||
|
||
COPY . . | ||
|
||
CMD exec streamlit run ui.py --server.port ${UI_PORT} |
Binary file not shown.
6 changes: 6 additions & 0 deletions
6
examples/pipelines/demo-question-answering/ui/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
streamlit==1.35.0 | ||
load_dotenv==0.1.0 | ||
nest_asyncio==1.6.0 | ||
aiohttp==3.9.5 | ||
beautifulsoup4==4.12.3 | ||
openai==1.35.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
# Copyright © 2024 Pathway | ||
|
||
import logging | ||
import os | ||
|
||
import requests | ||
import streamlit as st | ||
from dotenv import load_dotenv | ||
from pathway.xpacks.llm.question_answering import RAGClient | ||
|
||
load_dotenv() | ||
|
||
PATHWAY_HOST = os.environ.get("PATHWAY_HOST", "app") | ||
PATHWAY_PORT = os.environ.get("PATHWAY_PORT", 8000) | ||
|
||
st.set_page_config(page_title="Pathway RAG App", page_icon="favicon.ico") | ||
|
||
logging.basicConfig( | ||
level=logging.INFO, | ||
format="%(asctime)s %(name)s %(levelname)s %(message)s", | ||
datefmt="%Y-%m-%d %H:%M:%S", | ||
force=True, | ||
) | ||
|
||
logger = logging.getLogger("streamlit") | ||
logger.setLevel(logging.INFO) | ||
|
||
conn = RAGClient(url=f"http://{PATHWAY_HOST}:{PATHWAY_PORT}") | ||
|
||
note = """ | ||
<H4><b>Ask a question""" | ||
st.markdown(note, unsafe_allow_html=True) | ||
|
||
st.markdown( | ||
""" | ||
<style> | ||
div[data-baseweb="base-input"]{ | ||
} | ||
input[class]{ | ||
font-size:150%; | ||
color: black;} | ||
button[data-testid="baseButton-primary"], button[data-testid="baseButton-secondary"]{ | ||
border: none; | ||
display: flex; | ||
background-color: #E7E7E7; | ||
color: #454545; | ||
transition: color 0.3s; | ||
} | ||
button[data-testid="baseButton-primary"]:hover{ | ||
color: #1C1CF0; | ||
background-color: rgba(28,28,240,0.3); | ||
} | ||
button[data-testid="baseButton-secondary"]:hover{ | ||
color: #DC280B; | ||
background-color: rgba(220,40,11,0.3); | ||
} | ||
div[data-testid="stHorizontalBlock"]:has(button[data-testid="baseButton-primary"]){ | ||
display: flex; | ||
flex-direction: column; | ||
z-index: 0; | ||
width: 3rem; | ||
transform: translateY(-500px) translateX(672px); | ||
} | ||
</style> | ||
""", | ||
unsafe_allow_html=True, | ||
) | ||
|
||
|
||
question = st.text_input(label="", placeholder="Ask your question?") | ||
|
||
|
||
def get_options_list(metadata_list: list[dict], opt_key: str) -> list: | ||
"""Get all available options in a specific metadata key.""" | ||
options = set(map(lambda x: x[opt_key], metadata_list)) | ||
return list(options) | ||
|
||
|
||
logger.info("Requesting pw_list_documents...") | ||
document_meta_list = conn.pw_list_documents(keys=[]) | ||
logger.info("Received response pw_list_documents") | ||
|
||
st.session_state["document_meta_list"] = document_meta_list | ||
|
||
available_files = get_options_list(st.session_state["document_meta_list"], "path") | ||
|
||
|
||
with st.sidebar: | ||
st.info( | ||
body="See the source code [here](https://github.com/pathwaycom/llm-app/tree/main/examples/pipelines/demo-question-answering).", # noqa: E501 | ||
icon=":material/code:", | ||
) | ||
|
||
file_names = [i.split("/")[-1] for i in available_files] | ||
|
||
markdown_table = "| Indexed files |\n| --- |\n" | ||
for file_name in file_names: | ||
markdown_table += f"| {file_name} |\n" | ||
st.markdown(markdown_table, unsafe_allow_html=True) | ||
|
||
st.button("⟳ Refresh", use_container_width=True) | ||
|
||
css = """ | ||
<style> | ||
.slider-container { | ||
margin-top: 20px; /* Add some space between the main image and the slider */ | ||
} | ||
.slider-item { | ||
float: left; | ||
margin: 10px; | ||
width: 120px; /* Adjust the width to your liking */ | ||
// height: 50px; /* Adjust the height to your liking */ | ||
border: 1px solid #ccc; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
} | ||
.slider-item img { | ||
width: 100%; | ||
height: 100%; | ||
object-fit: cover; | ||
border-radius: 5px; | ||
} | ||
.slider-wrapper { | ||
display: flex; | ||
justify-content: center; | ||
flex-wrap: wrap; | ||
} | ||
.slider-item { | ||
margin: 10px; | ||
} | ||
</style>""" | ||
|
||
|
||
st.markdown(css, unsafe_allow_html=True) | ||
|
||
|
||
def send_post_request( | ||
url: str, data: dict, headers: dict = {}, timeout: int | None = None | ||
): | ||
response = requests.post(url, json=data, headers=headers, timeout=timeout) | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
|
||
if question: | ||
logger.info( | ||
{ | ||
"_type": "search_request_event", | ||
"query": question, | ||
} | ||
) | ||
|
||
api_url = f"http://{PATHWAY_HOST}:{PATHWAY_PORT}/v1/pw_ai_answer" | ||
payload = { | ||
"prompt": question, | ||
"response_type": "long", | ||
} | ||
with st.spinner("Retrieving response..."): | ||
response = send_post_request(api_url, payload) | ||
|
||
# response = conn.pw_ai_answer(question) | ||
|
||
logger.info( | ||
{ | ||
"_type": "search_response_event", | ||
"query": question, | ||
"response": type(response), | ||
} | ||
) | ||
|
||
logger.info(type(response)) | ||
|
||
st.markdown(f"**Answering question:** {question}") | ||
st.markdown(f"""{response}""") |