Spaces:
Sleeping
Sleeping
Commit
·
aa26315
1
Parent(s):
41f45bb
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from llama_index import VectorStoreIndex, SimpleDirectoryReader
|
| 3 |
+
from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
| 4 |
+
from llama_index import LangchainEmbedding, ServiceContext
|
| 5 |
+
from llama_index import StorageContext, load_index_from_storage
|
| 6 |
+
from llama_index import LLMPredictor
|
| 7 |
+
from langchain import HuggingFaceHub
|
| 8 |
+
from streamlit.components.v1 import html
|
| 9 |
+
from pathlib import Path
|
| 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:
|
| 23 |
+
st.markdown("<style>{}</style>".format(f.read()), unsafe_allow_html=True)
|
| 24 |
+
|
| 25 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 26 |
+
repo_id=os.getenv("repo_id")
|
| 27 |
+
model_name=os.getenv("model_name")
|
| 28 |
+
|
| 29 |
+
documents=[]
|
| 30 |
+
wechat_image= "WeChatCode.jpg"
|
| 31 |
+
|
| 32 |
+
def generate_random_string(length):
|
| 33 |
+
letters = string.ascii_lowercase
|
| 34 |
+
return ''.join(random.choice(letters) for i in range(length))
|
| 35 |
+
random_string = generate_random_string(20)
|
| 36 |
+
directory_path=random_string
|
| 37 |
+
|
| 38 |
+
st.sidebar.markdown(
|
| 39 |
+
"""
|
| 40 |
+
<style>
|
| 41 |
+
.blue-underline {
|
| 42 |
+
text-decoration: bold;
|
| 43 |
+
color: blue;
|
| 44 |
+
}
|
| 45 |
+
</style>
|
| 46 |
+
""",
|
| 47 |
+
unsafe_allow_html=True
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
st.markdown(
|
| 51 |
+
"""
|
| 52 |
+
<style>
|
| 53 |
+
[data-testid=stSidebar] [data-testid=stImage]{
|
| 54 |
+
text-align: center;
|
| 55 |
+
display: block;
|
| 56 |
+
margin-left: auto;
|
| 57 |
+
margin-right: auto;
|
| 58 |
+
width: 50%;
|
| 59 |
+
}
|
| 60 |
+
</style>
|
| 61 |
+
""", unsafe_allow_html=True
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
question = st.text_input("Enter your query here:")
|
| 65 |
+
display_output_text = st.checkbox("Check AI Repsonse", key="key_checkbox", help="Check me to get AI Response.")
|
| 66 |
+
|
| 67 |
+
with st.sidebar:
|
| 68 |
+
pdf_files = st.file_uploader("Upload file and start AI Doc-Chat.", type=['pdf'], accept_multiple_files=True)
|
| 69 |
+
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.")
|
| 70 |
+
st.sidebar.markdown("Contact: [[email protected]](mailto:[email protected])")
|
| 71 |
+
st.sidebar.markdown('WeChat: <span class="blue-underline">pat2win</span>, or scan the code below.', unsafe_allow_html=True)
|
| 72 |
+
st.image(wechat_image)
|
| 73 |
+
st.sidebar.markdown('<span class="blue-underline">Life Enhancing with AI.</span>', unsafe_allow_html=True)
|
| 74 |
+
st.subheader("Enjoy chatting!")
|
| 75 |
+
if pdf_files:
|
| 76 |
+
os.makedirs(directory_path)
|
| 77 |
+
for pdf_file in pdf_files:
|
| 78 |
+
file_path = os.path.join(directory_path, pdf_file.name)
|
| 79 |
+
with open(file_path, 'wb') as f:
|
| 80 |
+
f.write(pdf_file.read())
|
| 81 |
+
st.success(f"File '{pdf_file.name}' saved successfully.")
|
| 82 |
+
documents = SimpleDirectoryReader(directory_path).load_data()
|
| 83 |
+
else:
|
| 84 |
+
print("waiting for path creation.")
|
| 85 |
+
sys.exit()
|
| 86 |
+
|
| 87 |
+
embed_model = LangchainEmbedding(HuggingFaceEmbeddings(model_name=model_name))
|
| 88 |
+
|
| 89 |
+
llm = HuggingFaceHub(repo_id=repo_id,
|
| 90 |
+
model_kwargs={"min_length":1024,
|
| 91 |
+
"max_new_tokens":5632, "do_sample":True,
|
| 92 |
+
"temperature":0.1,
|
| 93 |
+
"top_k":50,
|
| 94 |
+
"top_p":0.95, "eos_token_id":49155})
|
| 95 |
+
|
| 96 |
+
llm_predictor = LLMPredictor(llm)
|
| 97 |
+
|
| 98 |
+
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, embed_model=embed_model)
|
| 99 |
+
|
| 100 |
+
new_index = VectorStoreIndex.from_documents(
|
| 101 |
+
documents,
|
| 102 |
+
service_context=service_context,
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
if question !="" and not question.strip().isspace() and not question == "" and not question.strip() == "" and not question.isspace():
|
| 106 |
+
if display_output_text==True:
|
| 107 |
+
with st.spinner("AI Thinking...Please wait a while to Cheers!"):
|
| 108 |
+
new_index.storage_context.persist("directory_path")
|
| 109 |
+
storage_context = StorageContext.from_defaults(persist_dir="directory_path")
|
| 110 |
+
loadedindex = load_index_from_storage(storage_context=storage_context, service_context=service_context)
|
| 111 |
+
query_engine = loadedindex.as_query_engine()
|
| 112 |
+
initial_response = query_engine.query(question)
|
| 113 |
+
#temp_ai_response=str(initial_response)
|
| 114 |
+
#final_ai_response=temp_ai_response.partition('<|end|>')[0]
|
| 115 |
+
st.write("AI Response:\n\n"+str(initial_response))
|
| 116 |
+
else:
|
| 117 |
+
print("Check the Checkbox to get AI Response.")
|
| 118 |
+
sys.exit()
|
| 119 |
+
else:
|
| 120 |
+
print("Please enter your question first.")
|
| 121 |
+
st.stop()
|