Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from llama_index.node_parser import SemanticSplitterNodeParser
|
| 4 |
from llama_index.embeddings import OpenAIEmbedding
|
|
@@ -10,49 +11,55 @@ from llama_index import VectorStoreIndex
|
|
| 10 |
from llama_index.retrievers import VectorIndexRetriever
|
| 11 |
from llama_index.query_engine import RetrieverQueryEngine
|
| 12 |
|
| 13 |
-
# Streamlit
|
| 14 |
-
st.title("Annual Report Summary
|
| 15 |
-
pinecone_api_key = os.getenv("PINECONE_API_KEY")
|
| 16 |
-
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
user_query = st.text_input("Ask a question about the annual report:")
|
| 56 |
-
if st.button("Submit"):
|
| 57 |
-
llm_query = query_engine.query(user_query)
|
| 58 |
-
st.write(llm_query.response)
|
|
|
|
| 1 |
import os
|
| 2 |
+
from getpass import getpass
|
| 3 |
import streamlit as st
|
| 4 |
from llama_index.node_parser import SemanticSplitterNodeParser
|
| 5 |
from llama_index.embeddings import OpenAIEmbedding
|
|
|
|
| 11 |
from llama_index.retrievers import VectorIndexRetriever
|
| 12 |
from llama_index.query_engine import RetrieverQueryEngine
|
| 13 |
|
| 14 |
+
# Streamlit UI for API keys
|
| 15 |
+
st.title("Annual Report Summary Query")
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Retrieve API keys
|
| 18 |
+
pinecone_api_key = st.text_input("Enter your Pinecone API Key:", type="password")
|
| 19 |
+
openai_api_key = st.text_input("Enter your OpenAI API Key:", type="password")
|
| 20 |
|
| 21 |
+
# Initialize the model and pipeline
|
| 22 |
+
embed_model = OpenAIEmbedding(api_key=openai_api_key)
|
| 23 |
+
pipeline = IngestionPipeline(
|
| 24 |
+
transformations=[
|
| 25 |
+
SemanticSplitterNodeParser(
|
| 26 |
+
buffer_size=1,
|
| 27 |
+
breakpoint_percentile_threshold=95,
|
| 28 |
+
embed_model=embed_model,
|
| 29 |
+
),
|
| 30 |
+
embed_model,
|
| 31 |
+
],
|
| 32 |
+
)
|
| 33 |
|
| 34 |
+
# Initialize connection to Pinecone
|
| 35 |
+
pc = PineconeGRPC(api_key=pinecone_api_key)
|
| 36 |
+
index_name = "anualreport"
|
| 37 |
+
pinecone_index = pc.Index(index_name)
|
| 38 |
+
vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
|
| 39 |
+
pinecone_index.describe_index_stats()
|
| 40 |
|
| 41 |
+
# Set OpenAI API key environment variable if not set
|
| 42 |
+
if not os.getenv('OPENAI_API_KEY'):
|
| 43 |
+
os.environ['OPENAI_API_KEY'] = openai_api_key
|
| 44 |
|
| 45 |
+
# Instantiate VectorStoreIndex object
|
| 46 |
+
vector_index = VectorStoreIndex.from_vector_store(vector_store=vector_store)
|
| 47 |
+
retriever = VectorIndexRetriever(index=vector_index, similarity_top_k=5)
|
| 48 |
+
query_engine = RetrieverQueryEngine(retriever=retriever)
|
| 49 |
|
| 50 |
+
# User query input
|
| 51 |
+
query = st.text_input("Enter your query:", "Summary of the Annual Report?")
|
| 52 |
|
| 53 |
+
# Process query and display results
|
| 54 |
+
if st.button("Get Summary"):
|
| 55 |
+
llm_query = query_engine.query(query)
|
| 56 |
+
st.write("Results:")
|
| 57 |
+
st.write(llm_query.response)
|
| 58 |
|
| 59 |
+
# Display each result
|
| 60 |
+
for idx, result in enumerate(llm_query.response):
|
| 61 |
+
st.write(f"Result {idx+1}: {result.get_content()}")
|
| 62 |
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
st._main_run_clExplicit('--runner', '-')
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|