Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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("
|
| 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 |
-
|
| 80 |
-
|
| 81 |
-
|
| 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 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
query_engine = loadedindex.as_query_engine()
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|