Ganesh43 commited on
Commit
20bb8ae
β€’
1 Parent(s): 25434c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -45
app.py CHANGED
@@ -1,47 +1,93 @@
1
  import streamlit as st
2
- from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings
3
- from transformers import AutoModel
4
- from pinecone import Pinecone
5
- import os # For environment variable access
6
-
7
- # Replace with your Space's environment variable name for API key
8
- API_KEY = os.environ.get("PINECONE_API_KEY")
9
- #pc = Pinecone(api_key=PINECONE_API_KEY)
10
- # Connect to Pinecone using the API key retrieved from the Space's environment variable
11
- #client = IndexClient(api_key=API_KEY)
12
- pc = Pinecone(api_key=API_KEY)
13
-
14
- # Load pre-trained model (replace with your chosen model)
15
- model =SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
16
-
17
- def process_and_search(query):
18
- # Preprocess user input (example: tokenization, normalization)
19
- #preprocessed_query = preprocess_query(query) # Replace with your implementation
20
-
21
- # Encode the preprocessed query using the pre-trained model
22
- encoded_query = model.embed_query(query, return_tensors="pt")
23
-
24
- # Perform vector search in Pinecone
25
- results = pc.query(INDEX_NAME, encoded_query.cpu().numpy())
26
-
27
- # Process search results (example: extract answers, format display)
28
- processed_results = []
29
- for result in results:
30
- # Example processing: extract answer from metadata
31
- answer = result["metadata"]["answer"] # Adapt based on your data structure
32
- processed_results.append(answer)
33
-
34
- return processed_results
35
- st.title("Pinecone Search App")
36
-
37
- user_query = st.text_area("Enter your question:", height=100)
38
-
39
- if st.button("Search"):
40
- if user_query:
41
- # Process, search, and display results (call the process_and_search function)
42
- answers = process_and_search(user_query)
43
- st.write("Search Results:")
44
- for answer in answers:
45
- st.write(f"- {answer}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  else:
47
- st.error("Please enter a question.")
 
1
  import streamlit as st
2
+ from utils import *
3
+ import constants
4
+
5
+ # Creating Session State Variable
6
+ if 'HuggingFace_API_Key' not in st.session_state:
7
+ st.session_state['HuggingFace_API_Key'] =''
8
+ if 'Pinecone_API_Key' not in st.session_state:
9
+ st.session_state['Pinecone_API_Key'] =''
10
+
11
+
12
+ #
13
+ st.title('πŸ€– AI Assistance For Website')
14
+
15
+ #********SIDE BAR Funtionality started*******
16
+
17
+ # Sidebar to capture the API keys
18
+ st.sidebar.title("πŸ˜ŽπŸ—οΈ")
19
+ st.session_state['HuggingFace_API_Key']= st.sidebar.text_input("What's your HuggingFace API key?",type="password")
20
+ st.session_state['Pinecone_API_Key']= st.sidebar.text_input("What's your Pinecone API key?",type="password")
21
+
22
+ #Recent changes by langchain team, expects ""PINECONE_API_KEY" environment variable for Pinecone usage! So we are creating it here
23
+ import os
24
+ os.environ["PINECONE_API_KEY"] = st.session_state['Pinecone_API_Key']
25
+
26
+
27
+ load_button = st.sidebar.button("Load data to Pinecone", key="load_button")
28
+
29
+ #If the bove button is clicked, pushing the data to Pinecone...
30
+ if load_button:
31
+ #Proceed only if API keys are provided
32
+ if st.session_state['HuggingFace_API_Key'] !="" and st.session_state['Pinecone_API_Key']!="" :
33
+
34
+ #Fetch data from site
35
+ site_data=get_website_data(constants.WEBSITE_URL)
36
+ st.write("Data pull done...")
37
+
38
+ #Split data into chunks
39
+ chunks_data=split_data(site_data)
40
+ st.write("Spliting data done...")
41
+
42
+ #Creating embeddings instance
43
+ embeddings=create_embeddings()
44
+ st.write("Embeddings instance creation done...")
45
+
46
+ #Push data to Pinecone
47
+
48
+ push_to_pinecone(st.session_state['Pinecone_API_Key'],constants.PINECONE_ENVIRONMENT,constants.PINECONE_INDEX,embeddings,chunks_data)
49
+ st.write("Pushing data to Pinecone done...")
50
+
51
+ st.sidebar.success("Data pushed to Pinecone successfully!")
52
+ else:
53
+ st.sidebar.error("Ooopssss!!! Please provide API keys.....")
54
+
55
+ #********SIDE BAR Funtionality ended*******
56
+
57
+ #Captures User Inputs
58
+ prompt = st.text_input('How can I help you my friend ❓',key="prompt") # The box for the text prompt
59
+ document_count = st.slider('No.Of links to return πŸ”— - (0 LOW || 5 HIGH)', 0, 5, 2,step=1)
60
+
61
+ submit = st.button("Search")
62
+
63
+
64
+ if submit:
65
+ #Proceed only if API keys are provided
66
+ if st.session_state['HuggingFace_API_Key'] !="" and st.session_state['Pinecone_API_Key']!="" :
67
+
68
+ #Creating embeddings instance
69
+ embeddings=create_embeddings()
70
+ st.write("Embeddings instance creation done...")
71
+
72
+ #Pull index data from Pinecone
73
+ index=pull_from_pinecone(st.session_state['Pinecone_API_Key'],constants.PINECONE_ENVIRONMENT,constants.PINECONE_INDEX,embeddings)
74
+ st.write("Pinecone index retrieval done...")
75
+
76
+ #Fetch relavant documents from Pinecone index
77
+ relavant_docs=get_similar_docs(index,prompt,document_count)
78
+ st.write(relavant_docs)
79
+
80
+ #Displaying search results
81
+ st.success("Please find the search results :")
82
+ #Displaying search results
83
+ st.write("search results list....")
84
+ for document in relavant_docs:
85
+
86
+ st.write("πŸ‘‰**Result : "+ str(relavant_docs.index(document)+1)+"**")
87
+ st.write("**Info**: "+document.page_content)
88
+ st.write("**Link**: "+ document.metadata['source'])
89
+
90
+
91
+
92
  else:
93
+ st.sidebar.error("Ooopssss!!! Please provide API keys.....")