Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,18 @@
|
|
| 1 |
import os
|
| 2 |
from getpass import getpass
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from llama_index.node_parser import SemanticSplitterNodeParser
|
| 5 |
from llama_index.embeddings import OpenAIEmbedding
|
| 6 |
from llama_index.ingestion import IngestionPipeline
|
| 7 |
-
from pinecone.grpc import PineconeGRPC
|
| 8 |
-
from pinecone import ServerlessSpec
|
| 9 |
-
from llama_index.vector_stores import PineconeVectorStore
|
| 10 |
-
from llama_index import VectorStoreIndex
|
| 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 |
-
#
|
| 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(
|
|
@@ -31,35 +24,52 @@ pipeline = IngestionPipeline(
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 51 |
-
query = st.text_input("Enter your query:", "Summary of the Annual Report?")
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
llm_query = query_engine.query(query)
|
| 56 |
-
st.write("Results:")
|
| 57 |
-
st.write(llm_query.response)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from getpass import getpass
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
pinecone_api_key = os.getenv("PINECONE_API_KEY") or getpass("Enter your Pinecone API Key: ")
|
| 6 |
+
openai_api_key = os.getenv("OPENAI_API_KEY") or getpass("Enter your OpenAI API Key: ")
|
| 7 |
+
|
| 8 |
from llama_index.node_parser import SemanticSplitterNodeParser
|
| 9 |
from llama_index.embeddings import OpenAIEmbedding
|
| 10 |
from llama_index.ingestion import IngestionPipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# This will be the model we use both for Node parsing and for vectorization
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
embed_model = OpenAIEmbedding(api_key=openai_api_key)
|
| 14 |
+
|
| 15 |
+
# Define the initial pipeline
|
| 16 |
pipeline = IngestionPipeline(
|
| 17 |
transformations=[
|
| 18 |
SemanticSplitterNodeParser(
|
|
|
|
| 24 |
],
|
| 25 |
)
|
| 26 |
|
| 27 |
+
from pinecone.grpc import PineconeGRPC
|
| 28 |
+
from pinecone import ServerlessSpec
|
| 29 |
+
|
| 30 |
+
from llama_index.vector_stores import PineconeVectorStore
|
| 31 |
+
|
| 32 |
# Initialize connection to Pinecone
|
| 33 |
pc = PineconeGRPC(api_key=pinecone_api_key)
|
| 34 |
index_name = "anualreport"
|
| 35 |
+
|
| 36 |
+
# Initialize your index
|
| 37 |
pinecone_index = pc.Index(index_name)
|
| 38 |
+
|
| 39 |
+
# Initialize VectorStore
|
| 40 |
vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
|
| 41 |
+
|
| 42 |
pinecone_index.describe_index_stats()
|
| 43 |
|
| 44 |
+
from llama_index import VectorStoreIndex
|
| 45 |
+
from llama_index.retrievers import VectorIndexRetriever
|
| 46 |
+
|
| 47 |
+
# Set the OpenAI API key if not already set
|
| 48 |
if not os.getenv('OPENAI_API_KEY'):
|
| 49 |
os.environ['OPENAI_API_KEY'] = openai_api_key
|
| 50 |
|
| 51 |
+
# Instantiate VectorStoreIndex object from our vector_store object
|
| 52 |
vector_index = VectorStoreIndex.from_vector_store(vector_store=vector_store)
|
| 53 |
+
|
| 54 |
+
# Grab 5 search results
|
| 55 |
retriever = VectorIndexRetriever(index=vector_index, similarity_top_k=5)
|
|
|
|
| 56 |
|
| 57 |
+
from llama_index.query_engine import RetrieverQueryEngine
|
|
|
|
| 58 |
|
| 59 |
+
# Pass in your retriever from above, which is configured to return the top 5 results
|
| 60 |
+
query_engine = RetrieverQueryEngine(retriever=retriever)
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
def query_anual_report(query):
|
| 63 |
+
response = query_engine.query(query)
|
| 64 |
+
return response.response
|
| 65 |
|
| 66 |
+
# Define Gradio Interface
|
| 67 |
+
iface = gr.Interface(
|
| 68 |
+
fn=query_anual_report,
|
| 69 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Ask something..."),
|
| 70 |
+
outputs="text",
|
| 71 |
+
title="Annual Report Query",
|
| 72 |
+
description="Ask questions about the annual report."
|
| 73 |
+
)
|
| 74 |
|
| 75 |
+
iface.launch()
|