AI-RESEARCHER-2024 commited on
Commit
793d65a
·
verified ·
1 Parent(s): a82e23e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -24
app.py CHANGED
@@ -43,8 +43,8 @@ def get_text_chunks(text):
43
 
44
 
45
 
46
- def get_vector_store(text_chunks, user_api_key=None):
47
- api_key = user_api_key if user_api_key else switch_api_key()
48
  embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
49
  vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
50
  vector_store.save_local("faiss_index")
@@ -52,8 +52,8 @@ def get_vector_store(text_chunks, user_api_key=None):
52
 
53
 
54
 
55
- def get_conversational_chain(api_key):
56
- #api_key = switch_api_key()
57
  prompt_template = """
58
  You are a helpful assistant that only answers based on the context provided from the PDF documents.
59
  Do not use any external knowledge or assumptions. If the answer is not found in the context below, reply with "I don't know."
@@ -77,11 +77,13 @@ def get_conversational_chain(api_key):
77
 
78
 
79
 
80
- def user_input(user_question, api_key):
 
81
  embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
82
  new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
83
  docs = new_db.similarity_search(user_question)
84
- chain = get_conversational_chain(api_key)
 
85
 
86
  response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
87
  st.write("Reply: ", response["output_text"])
@@ -91,28 +93,22 @@ def user_input(user_question, api_key):
91
 
92
  # Streamlit application
93
  def main():
94
- st.set_page_config("Chat PDF")
95
- st.header("CSC 121: Computers and Scientific Thinking (Chatbot)")
96
- st.subheader("Ask a question ONLY from the CSC 121 textbook of Dr. Reed", divider=True)
97
-
98
- user_api_key = st.text_input("Enter your API key (optional)")
 
 
 
 
 
 
99
 
100
  user_question = st.text_input("Ask a question")
101
 
102
  if user_question:
103
- if user_api_key:
104
- api_key = user_api_key
105
- else:
106
- api_key = switch_api_key()
107
- user_input(user_question, api_key)
108
-
109
- pdf_docs = st.file_uploader("Upload PDF files", accept_multiple_files=True)
110
- if st.button("Submit & Process"):
111
- with st.spinner("Processing..."):
112
- raw_text = get_pdf_text(pdf_docs)
113
- text_chunks = get_text_chunks(raw_text)
114
- get_vector_store(text_chunks, user_api_key)
115
- st.success("Done")
116
 
117
 
118
 
 
43
 
44
 
45
 
46
+ def get_vector_store(text_chunks):
47
+ api_key = switch_api_key()
48
  embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
49
  vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
50
  vector_store.save_local("faiss_index")
 
52
 
53
 
54
 
55
+ def get_conversational_chain():
56
+ api_key = switch_api_key()
57
  prompt_template = """
58
  You are a helpful assistant that only answers based on the context provided from the PDF documents.
59
  Do not use any external knowledge or assumptions. If the answer is not found in the context below, reply with "I don't know."
 
77
 
78
 
79
 
80
+ def user_input(user_question):
81
+ api_key = switch_api_key()
82
  embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=api_key)
83
  new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
84
  docs = new_db.similarity_search(user_question)
85
+ chain = get_conversational_chain()
86
+
87
 
88
  response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
89
  st.write("Reply: ", response["output_text"])
 
93
 
94
  # Streamlit application
95
  def main():
96
+ st.markdown(
97
+ """
98
+ <style>
99
+ .header {font-size: 20px !important;}
100
+ .subheader {font-size: 16px !important;}
101
+ </style>
102
+ """,
103
+ unsafe_allow_html=True
104
+ )
105
+ st.markdown('<h1 class="header">CSC 121: Computers and Scientific Thinking (Chatbot)</h1>', unsafe_allow_html=True)
106
+ st.markdown('<h2 class="subheader">Ask a question ONLY from the CSC 121 textbook of Dr. Reed</h2>', unsafe_allow_html=True)
107
 
108
  user_question = st.text_input("Ask a question")
109
 
110
  if user_question:
111
+ user_input(user_question)
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
 
114