Spaces:
Sleeping
Sleeping
Commit
·
7d30953
1
Parent(s):
6ea0b62
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 transformers import HuggingFaceHub
|
| 8 |
+
from langchain import HuggingFaceHub
|
| 9 |
+
from streamlit.components.v1 import html
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from time import sleep
|
| 12 |
+
import random
|
| 13 |
+
import string
|
| 14 |
+
|
| 15 |
+
import os
|
| 16 |
+
from dotenv import load_dotenv
|
| 17 |
+
load_dotenv()
|
| 18 |
+
|
| 19 |
+
st.set_page_config(page_title="Open AI Doc-Chat Assistant", layout="wide")
|
| 20 |
+
st.subheader("Open AI Doc-Chat Assistant: Life Enhancing with AI!")
|
| 21 |
+
|
| 22 |
+
css_file = "main.css"
|
| 23 |
+
with open(css_file) as f:
|
| 24 |
+
st.markdown("<style>{}</style>".format(f.read()), unsafe_allow_html=True)
|
| 25 |
+
|
| 26 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 27 |
+
|
| 28 |
+
documents=[]
|
| 29 |
+
|
| 30 |
+
def generate_random_string(length):
|
| 31 |
+
letters = string.ascii_lowercase
|
| 32 |
+
return ''.join(random.choice(letters) for i in range(length))
|
| 33 |
+
random_string = generate_random_string(20)
|
| 34 |
+
directory_path=random_string
|
| 35 |
+
|
| 36 |
+
with st.sidebar:
|
| 37 |
+
st.subheader("Upload your Documents Here: ")
|
| 38 |
+
pdf_files = st.file_uploader("Choose your PDF Files and Press OK", type=['pdf'], accept_multiple_files=True)
|
| 39 |
+
if pdf_files:
|
| 40 |
+
os.makedirs(directory_path)
|
| 41 |
+
for pdf_file in pdf_files:
|
| 42 |
+
file_path = os.path.join(directory_path, pdf_file.name)
|
| 43 |
+
with open(file_path, 'wb') as f:
|
| 44 |
+
f.write(pdf_file.read())
|
| 45 |
+
st.success(f"File '{pdf_file.name}' saved successfully.")
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
documents = SimpleDirectoryReader(directory_path).load_data()
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print("waiting for path creation.")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# Load documents from a directory
|
| 54 |
+
#documents = SimpleDirectoryReader('data').load_data()
|
| 55 |
+
|
| 56 |
+
embed_model = LangchainEmbedding(HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2'))
|
| 57 |
+
|
| 58 |
+
llm_predictor = LLMPredictor(HuggingFaceHub(repo_id="HuggingFaceH4/starchat-beta", model_kwargs={"min_length":100, "max_new_tokens":1024, "do_sample":True, "temperature":0.2,"top_k":50, "top_p":0.95, "eos_token_id":49155}))
|
| 59 |
+
|
| 60 |
+
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, embed_model=embed_model)
|
| 61 |
+
|
| 62 |
+
new_index = VectorStoreIndex.from_documents(
|
| 63 |
+
documents,
|
| 64 |
+
service_context=service_context,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
new_index.storage_context.persist("directory_path")
|
| 68 |
+
|
| 69 |
+
storage_context = StorageContext.from_defaults(persist_dir="directory_path")
|
| 70 |
+
|
| 71 |
+
loadedindex = load_index_from_storage(storage_context=storage_context, service_context=service_context)
|
| 72 |
+
|
| 73 |
+
query_engine = loadedindex.as_query_engine()
|
| 74 |
+
|
| 75 |
+
user_question = st.text_input("Enter your query here:")
|
| 76 |
+
if user_question !="" and not user_question.strip().isspace() and not user_question == "" and not user_question.strip() == "" and not user_question.isspace():
|
| 77 |
+
initial_response = query_engine.query(user_question)
|
| 78 |
+
temp_ai_response=str(initial_response)
|
| 79 |
+
final_ai_response=temp_ai_response.partition('<|end|>')[0]
|
| 80 |
+
print("AI Response:\n"+final_ai_response)
|
| 81 |
+
st.write("AI Response:\n\n"+final_ai_response)
|