generated from restackio/starter-genai-streamlit-llamaindex-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
39 lines (34 loc) · 1.21 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os.path
import streamlit as st
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
StorageContext,
load_index_from_storage,
)
# check if storage already exists
PERSIST_DIR = "./storage"
if not os.path.exists(PERSIST_DIR):
# load the documents and create the index
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)
# store it for later
index.storage_context.persist(persist_dir=PERSIST_DIR)
else:
# load the existing index
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
index = load_index_from_storage(storage_context)
query_engine = index.as_query_engine()
# Define a simple Streamlit app
st.title('Ask Llama about the "GPT-4 Technical Report"')
query = st.text_input("What would you like to ask? (source: data/gpt-4.pdf)", "What insights from the report?")
# If the 'Submit' button is clicked
if st.button("Submit"):
if not query.strip():
st.error(f"Please provide the search query.")
else:
try:
response = query_engine.query(query)
st.success(response)
except Exception as e:
st.error(f"An error occurred: {e}")