iShare commited on
Commit
ca67adb
·
1 Parent(s): 42e0e6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -33
app.py CHANGED
@@ -10,13 +10,13 @@ from pathlib import Path
10
  from time import sleep
11
  import random
12
  import string
13
-
14
  import os
15
  from dotenv import load_dotenv
16
  load_dotenv()
17
 
18
  st.set_page_config(page_title="Cheers! Open AI Doc-Chat Assistant", layout="wide")
19
- st.subheader("Cheers! Open AI Doc-Chat Assistant: Life Enhancing with AI!")
20
 
21
  css_file = "main.css"
22
  with open(css_file) as f:
@@ -25,6 +25,7 @@ with open(css_file) as f:
25
  HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
26
 
27
  documents=[]
 
28
 
29
  def generate_random_string(length):
30
  letters = string.ascii_lowercase
@@ -32,8 +33,6 @@ def generate_random_string(length):
32
  random_string = generate_random_string(20)
33
  directory_path=random_string
34
 
35
- wechat_image= "WeChatCode.jpg"
36
-
37
  st.sidebar.markdown(
38
  """
39
  <style>
@@ -60,6 +59,9 @@ st.markdown(
60
  """, unsafe_allow_html=True
61
  )
62
 
 
 
 
63
  with st.sidebar:
64
  pdf_files = st.file_uploader("Upload file and start AI Doc-Chat.", type=['pdf'], accept_multiple_files=True)
65
  st.write("Disclaimer: This app is for information purpose only. NO liability could be claimed against whoever associated with this app in any manner. User should consult a qualified legal professional for legal advice.")
@@ -75,11 +77,10 @@ with st.sidebar:
75
  with open(file_path, 'wb') as f:
76
  f.write(pdf_file.read())
77
  st.success(f"File '{pdf_file.name}' saved successfully.")
78
-
79
- try:
80
- documents = SimpleDirectoryReader(directory_path).load_data()
81
- except Exception as e:
82
- print("waiting for path creation.")
83
 
84
  embed_model = LangchainEmbedding(HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2'))
85
 
@@ -92,27 +93,20 @@ new_index = VectorStoreIndex.from_documents(
92
  service_context=service_context,
93
  )
94
 
95
- new_index.storage_context.persist("directory_path")
96
-
97
- storage_context = StorageContext.from_defaults(persist_dir="directory_path")
98
-
99
- loadedindex = load_index_from_storage(storage_context=storage_context, service_context=service_context)
100
-
101
- query_engine = loadedindex.as_query_engine()
102
-
103
- while True:
104
- try:
105
- question = st.text_input("Enter your query here:")
106
- print("Your query:\n"+question)
107
- if question.strip().isspace() or question == "" or question.strip() == "" or question.isspace():
108
- break
109
- elif question=="exit":
110
- break
111
- elif question!="":
112
- with st.spinner("AI Thinking...Please wait a while to Cheers!"):
113
- initial_response = query_engine.query(question)
114
- temp_ai_response=str(initial_response)
115
- final_ai_response=temp_ai_response.partition('<|end|>')[0]
116
- st.write("AI Response:\n\n"+final_ai_response)
117
- except Exception as e:
118
- st.stop()
 
10
  from time import sleep
11
  import random
12
  import string
13
+ import sys
14
  import os
15
  from dotenv import load_dotenv
16
  load_dotenv()
17
 
18
  st.set_page_config(page_title="Cheers! Open AI Doc-Chat Assistant", layout="wide")
19
+ st.subheader("Open AI Doc-Chat Assistant: Life Enhancing with AI!")
20
 
21
  css_file = "main.css"
22
  with open(css_file) as f:
 
25
  HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
26
 
27
  documents=[]
28
+ wechat_image= "WeChatCode.jpg"
29
 
30
  def generate_random_string(length):
31
  letters = string.ascii_lowercase
 
33
  random_string = generate_random_string(20)
34
  directory_path=random_string
35
 
 
 
36
  st.sidebar.markdown(
37
  """
38
  <style>
 
59
  """, unsafe_allow_html=True
60
  )
61
 
62
+ question = st.text_input("Enter your query here:")
63
+ display_output_text = st.checkbox("Check AI Repsonse", key="key_checkbox", help="Check me to get AI Response.")
64
+
65
  with st.sidebar:
66
  pdf_files = st.file_uploader("Upload file and start AI Doc-Chat.", type=['pdf'], accept_multiple_files=True)
67
  st.write("Disclaimer: This app is for information purpose only. NO liability could be claimed against whoever associated with this app in any manner. User should consult a qualified legal professional for legal advice.")
 
77
  with open(file_path, 'wb') as f:
78
  f.write(pdf_file.read())
79
  st.success(f"File '{pdf_file.name}' saved successfully.")
80
+ documents = SimpleDirectoryReader(directory_path).load_data()
81
+ else:
82
+ print("waiting for path creation.")
83
+ sys.exit()
 
84
 
85
  embed_model = LangchainEmbedding(HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2'))
86
 
 
93
  service_context=service_context,
94
  )
95
 
96
+ if question !="" and not question.strip().isspace() and not question == "" and not question.strip() == "" and not question.isspace():
97
+ if display_output_text==True:
98
+ with st.spinner("AI Thinking...Please wait a while to Cheers!"):
99
+ new_index.storage_context.persist("directory_path")
100
+ storage_context = StorageContext.from_defaults(persist_dir="directory_path")
101
+ loadedindex = load_index_from_storage(storage_context=storage_context, service_context=service_context)
102
+ query_engine = loadedindex.as_query_engine()
103
+ initial_response = query_engine.query(question)
104
+ temp_ai_response=str(initial_response)
105
+ final_ai_response=temp_ai_response.partition('<|end|>')[0]
106
+ st.write("AI Response:\n\n"+final_ai_response)
107
+ else:
108
+ print("Check the Checkbox to get AI Response.")
109
+ sys.exit()
110
+ else:
111
+ print("Please enter your question first.")
112
+ st.stop()