|
import tempfile |
|
import os |
|
import tiktoken |
|
import streamlit as st |
|
|
|
from llama_index.llms.gemini import Gemini |
|
from llama_index.llms.huggingface import HuggingFaceLLM |
|
from llama_index.llms.mistralai import MistralAI |
|
from llama_index.llms.openai import OpenAI |
|
|
|
from llama_index.embeddings.openai import OpenAIEmbedding |
|
|
|
from llama_index.core import ( |
|
VectorStoreIndex, |
|
Settings, |
|
) |
|
|
|
from llama_parse import LlamaParse |
|
|
|
from streamlit_pdf_viewer import pdf_viewer |
|
|
|
def main(): |
|
with st.sidebar: |
|
st.title('Document Summarization and QA System') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
provider = st.selectbox( |
|
label="Select LLM Provider", |
|
options=['google', 'huggingface', 'mistralai', 'openai'], |
|
index=3 |
|
) |
|
|
|
|
|
if provider == 'google': |
|
llm_list = ['gemini'] |
|
elif provider == 'huggingface': |
|
llm_list = [] |
|
elif provider == 'mistralai': |
|
llm_list =[] |
|
elif provider == 'openai': |
|
llm_list = ['gpt-3.5-turbo', 'gpt-4', 'gpt-4-turbo', 'gpt-4o', 'gpt-4o-mini'] |
|
else: |
|
llm_list = [] |
|
|
|
llm_name = st.selectbox( |
|
label="Select LLM Model", |
|
options=llm_list, |
|
index=0 |
|
) |
|
|
|
|
|
temperature = st.slider( |
|
"Temperature", |
|
min_value=0.0, |
|
max_value=1.0, |
|
value=0.0, |
|
step=0.05, |
|
) |
|
|
|
max_output_tokens = 2048 |
|
|
|
|
|
llm_key = st.text_input( |
|
"Enter your LLM API Key", |
|
value=None, |
|
) |
|
|
|
|
|
|
|
|
|
if llm_key is not None: |
|
if provider == 'openai': |
|
os.environ["OPENAI_API_KEY"] = str(llm_key) |
|
Settings.llm = OpenAI( |
|
model=llm_name, |
|
temperature=temperature, |
|
max_tokens=max_output_tokens |
|
) |
|
Settings.tokenizer = tiktoken.encoding_for_model(llm_name).encode |
|
Settings.num_output = max_output_tokens |
|
Settings.context_window = 4096 |
|
Settings.embed_model = OpenAIEmbedding() |
|
elif provider == 'huggingface': |
|
os.environ['HFTOKEN'] = str(llm_key) |
|
|
|
|
|
parse_key = st.text_input( |
|
"Enter your LlamaParse API Key", |
|
value=None, |
|
) |
|
|
|
uploaded_file = st.file_uploader( |
|
"Choose a PDF file to upload", |
|
type=['pdf'], |
|
accept_multiple_files=False |
|
) |
|
|
|
parsed_document = None |
|
if uploaded_file is not None: |
|
|
|
parser = LlamaParse( |
|
api_key=parse_key, |
|
result_type="text" |
|
) |
|
|
|
|
|
temp_dir = tempfile.TemporaryDirectory() |
|
temp_filename = os.path.join(temp_dir.name, uploaded_file.name) |
|
with open(temp_filename, "wb") as f: |
|
f.write(uploaded_file.getvalue()) |
|
parsed_document = parser.load_data(temp_filename) |
|
temp_dir.cleanup() |
|
|
|
col1, col2 = st.columns(2) |
|
|
|
with col1: |
|
st.markdown( |
|
""" |
|
# Instructions |
|
|
|
1. Obtain an [API Key](https://cloud.llamaindex.ai/api-key) from LlamaParse to parse your document. |
|
2. Obtain a similar API Key from your preferred LLM provider. |
|
3. Make selections at the left and upload a document to use a context. |
|
4. Begin asking questions below! |
|
""" |
|
) |
|
|
|
st.divider() |
|
|
|
prompt_txt = 'Summarize this document in a 3-5 sentences.' |
|
prompt = st.text_area( |
|
label="Enter your query.", |
|
key="prompt_widget", |
|
value=prompt_txt |
|
) |
|
|
|
if parsed_document is not None: |
|
index = VectorStoreIndex.from_documents(parsed_document) |
|
query_engine = index.as_query_engine() |
|
response = query_engine.query(prompt) |
|
st.write(response.response) |
|
|
|
with col2: |
|
tab1, tab2 = st.tabs(["Uploaded File", "Parsed File",]) |
|
|
|
with tab1: |
|
if uploaded_file is not None: |
|
bytes_data = uploaded_file.getvalue() |
|
pdf_viewer(input=bytes_data, width=700) |
|
|
|
with tab2: |
|
if parsed_document is not None: |
|
st.write(parsed_document) |
|
|
|
if __name__ == '__main__': |
|
|
|
from llama_index.core import set_global_handler |
|
set_global_handler("langfuse") |
|
st.set_page_config(layout="wide") |
|
|
|
main() |