Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,71 @@
|
|
| 1 |
-
|
| 2 |
import streamlit as st
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
from
|
|
|
|
|
|
|
| 6 |
from llama_index.embeddings import OpenAIEmbedding
|
| 7 |
from llama_index.ingestion import IngestionPipeline
|
| 8 |
-
from
|
|
|
|
| 9 |
from llama_index.vector_stores import PineconeVectorStore
|
| 10 |
-
from llama_index
|
| 11 |
from llama_index.retrievers import VectorIndexRetriever
|
| 12 |
-
from
|
| 13 |
-
|
| 14 |
-
# Load environment variables
|
| 15 |
-
load_dotenv()
|
| 16 |
-
pinecone_api_key = os.getenv("PINECONE_API_KEY")
|
| 17 |
-
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 18 |
-
index_name = os.getenv("INDEX_NAME")
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
breakpoint_percentile_threshold=95,
|
| 34 |
-
embed_model=embed_model,
|
| 35 |
-
),
|
| 36 |
-
embed_model,
|
| 37 |
-
],
|
| 38 |
-
)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
retriever = VectorIndexRetriever(index=vector_index, similarity_top_k=5)
|
| 43 |
-
query_engine = RetrieverQueryEngine(retriever=retriever)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
st.session_state.chat_history = response['chat_history']
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
| 58 |
-
load_dotenv()
|
| 59 |
-
st.set_page_config(page_title="Chat with Annual Reports", page_icon=":books:")
|
| 60 |
-
st.write(css, unsafe_allow_html=True)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
handle_userinput(user_question)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 1 |
+
# Streamlit application
|
| 2 |
import streamlit as st
|
| 3 |
+
import os
|
| 4 |
+
from getpass import getpass
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
|
| 7 |
+
from llama_index.node_parser import SemanticSplitterNodeParser
|
| 8 |
from llama_index.embeddings import OpenAIEmbedding
|
| 9 |
from llama_index.ingestion import IngestionPipeline
|
| 10 |
+
from pinecone.grpc import PineconeGRPC
|
| 11 |
+
from pinecone import ServerlessSpec
|
| 12 |
from llama_index.vector_stores import PineconeVectorStore
|
| 13 |
+
from llama_index import VectorStoreIndex
|
| 14 |
from llama_index.retrievers import VectorIndexRetriever
|
| 15 |
+
from llama_index.query_engine import RetrieverQueryEngine
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Function to initialize the Pinecone and LlamaIndex setup
|
| 18 |
+
def initialize_pipeline():
|
| 19 |
+
pinecone_api_key = os.getenv("PINECONE_API_KEY")
|
| 20 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 21 |
|
| 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 |
+
pc = PineconeGRPC(api_key=pinecone_api_key)
|
| 35 |
+
index_name = "anualreport"
|
| 36 |
+
pinecone_index = pc.Index(index_name)
|
| 37 |
+
vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
|
| 38 |
+
pinecone_index.describe_index_stats()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
if not os.getenv('OPENAI_API_KEY'):
|
| 41 |
+
os.environ['OPENAI_API_KEY'] = openai_api_key
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
vector_index = VectorStoreIndex.from_vector_store(vector_store=vector_store)
|
| 44 |
+
retriever = VectorIndexRetriever(index=vector_index, similarity_top_k=5)
|
| 45 |
+
query_engine = RetrieverQueryEngine(retriever=retriever)
|
|
|
|
| 46 |
|
| 47 |
+
return query_engine
|
| 48 |
+
|
| 49 |
+
# Streamlit UI
|
| 50 |
+
st.title("Chat with Annual Reports")
|
| 51 |
+
|
| 52 |
+
# Initialize the query engine
|
| 53 |
+
query_engine = initialize_pipeline()
|
| 54 |
+
|
| 55 |
+
# Conversation model using Hugging Face transformers
|
| 56 |
+
conversation_pipeline = pipeline("conversational", model="microsoft/DialoGPT-medium")
|
| 57 |
|
| 58 |
+
# User input
|
| 59 |
+
user_input = st.text_input("You: ", "")
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
if user_input:
|
| 62 |
+
# Query the vector DB
|
| 63 |
+
llm_query = query_engine.query(user_input)
|
| 64 |
+
response = llm_query.response
|
| 65 |
|
| 66 |
+
# Generate response using Hugging Face conversation model
|
| 67 |
+
conversation = conversation_pipeline([user_input, response])
|
| 68 |
+
bot_response = conversation[-1]["generated_text"]
|
|
|
|
| 69 |
|
| 70 |
+
# Display response
|
| 71 |
+
st.text_area("Bot: ", bot_response, height=200)
|