Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- app.py +106 -104
- app_config.py +13 -19
- faiss_index/index.faiss +0 -0
- faiss_index/index.pkl +3 -0
- requirements.txt +11 -11
app.py
CHANGED
|
@@ -1,104 +1,106 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
-
from langchain.
|
| 8 |
-
from
|
| 9 |
-
from
|
| 10 |
-
from
|
| 11 |
-
from
|
| 12 |
-
import
|
| 13 |
-
from
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
with container.chat_message("assistant"):
|
| 97 |
-
response = response_generator(prompt
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import base64
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from app_config import SYSTEM_PROMPT,MODEL,MAX_TOKENS,TRANSFORMER_MODEL
|
| 7 |
+
from langchain.memory import ConversationSummaryBufferMemory
|
| 8 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 9 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 10 |
+
from langchain_groq import ChatGroq
|
| 11 |
+
from streamlit_pdf_viewer import pdf_viewer
|
| 12 |
+
from pydantic import BaseModel
|
| 13 |
+
from langchain.chains import LLMChain
|
| 14 |
+
from langchain.prompts import ChatPromptTemplate
|
| 15 |
+
from langchain_community.vectorstores import FAISS
|
| 16 |
+
from sentence_transformers import SentenceTransformer
|
| 17 |
+
from typing import Any
|
| 18 |
+
|
| 19 |
+
st.title("Hitachi Support Bot")
|
| 20 |
+
|
| 21 |
+
class Element(BaseModel):
|
| 22 |
+
type: str
|
| 23 |
+
text: Any
|
| 24 |
+
|
| 25 |
+
# llm = ChatGoogleGenerativeAI(
|
| 26 |
+
# model=MODEL,
|
| 27 |
+
# max_tokens=MAX_TOKENS
|
| 28 |
+
# )
|
| 29 |
+
llm = ChatGroq(model=MODEL,api_key='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')
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
prompt = ChatPromptTemplate.from_template(SYSTEM_PROMPT)
|
| 33 |
+
qa_chain = LLMChain(llm=llm,prompt=prompt)
|
| 34 |
+
embeddings = HuggingFaceEmbeddings(model_name=TRANSFORMER_MODEL)
|
| 35 |
+
db = FAISS.load_local("faiss_index",embeddings,allow_dangerous_deserialization=True)
|
| 36 |
+
|
| 37 |
+
st.markdown(
|
| 38 |
+
"""
|
| 39 |
+
<style>
|
| 40 |
+
.st-emotion-cache-janbn0 {
|
| 41 |
+
flex-direction: row-reverse;
|
| 42 |
+
text-align: right;
|
| 43 |
+
}
|
| 44 |
+
</style>
|
| 45 |
+
""",
|
| 46 |
+
unsafe_allow_html=True,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
def response_generator(question):
|
| 50 |
+
relevant_docs = db.similarity_search_with_relevance_scores(question,k=5)
|
| 51 |
+
context = ""
|
| 52 |
+
relevant_images = []
|
| 53 |
+
for d,score in relevant_docs:
|
| 54 |
+
if score > 0:
|
| 55 |
+
if d.metadata['type'] == 'text':
|
| 56 |
+
context += str(d.metadata['original_content'])
|
| 57 |
+
elif d.metadata['type'] == 'table':
|
| 58 |
+
context += str(d.metadata['original_content'])
|
| 59 |
+
elif d.metadata['type'] == 'image':
|
| 60 |
+
context += d.page_content
|
| 61 |
+
relevant_images.append(d.metadata['original_content'])
|
| 62 |
+
result = qa_chain.run({'context':context,"question":question})
|
| 63 |
+
return result,relevant_images
|
| 64 |
+
|
| 65 |
+
with st.sidebar:
|
| 66 |
+
st.header("Hitachi Support Bot")
|
| 67 |
+
button = st.toggle("View Doc file.")
|
| 68 |
+
|
| 69 |
+
if button:
|
| 70 |
+
pdf_viewer("GPT OUTPUT.pdf")
|
| 71 |
+
else:
|
| 72 |
+
if "messages" not in st.session_state:
|
| 73 |
+
st.session_state.messages=[{"role": "system", "content": SYSTEM_PROMPT}]
|
| 74 |
+
|
| 75 |
+
if "llm" not in st.session_state:
|
| 76 |
+
st.session_state.llm = llm
|
| 77 |
+
if "rag_memory" not in st.session_state:
|
| 78 |
+
st.session_state.rag_memory = ConversationSummaryBufferMemory(llm=st.session_state.llm, max_token_limit= 5000)
|
| 79 |
+
|
| 80 |
+
container = st.container(height=700)
|
| 81 |
+
for message in st.session_state.messages:
|
| 82 |
+
if message["role"] != "system":
|
| 83 |
+
if message["role"] == "user":
|
| 84 |
+
with container.chat_message(message["role"]):
|
| 85 |
+
st.write(message["content"])
|
| 86 |
+
if message["role"] == "assistant":
|
| 87 |
+
with container.chat_message(message["role"]):
|
| 88 |
+
st.write(message["content"])
|
| 89 |
+
for i in range(len(message["images"])):
|
| 90 |
+
st.image(Image.open(BytesIO(base64.b64decode(message["images"][i].encode('utf-8')))))
|
| 91 |
+
|
| 92 |
+
if prompt := st.chat_input("Enter your query here... "):
|
| 93 |
+
with container.chat_message("user"):
|
| 94 |
+
st.write(prompt)
|
| 95 |
+
st.session_state.messages.append({"role":"user" , "content":prompt})
|
| 96 |
+
with container.chat_message("assistant"):
|
| 97 |
+
response,images = response_generator(prompt)
|
| 98 |
+
st.write(response)
|
| 99 |
+
for i in range(len(images)):
|
| 100 |
+
st.markdown("""---""")
|
| 101 |
+
st.image(Image.open(BytesIO(base64.b64decode(images[i].encode('utf-8')))))
|
| 102 |
+
st.markdown("""---""")
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
st.session_state.rag_memory.save_context({'input': prompt}, {'output': response})
|
| 106 |
+
st.session_state.messages.append({"role":"assistant" , "content":response,'images':images})
|
app_config.py
CHANGED
|
@@ -1,19 +1,13 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
REASONING_MODEL_TEMPERATURE = 0
|
| 15 |
-
NLP_MODEL_TEMPERATURE = 0
|
| 16 |
-
NLP_MODEL_MAX_TOKENS = 5400
|
| 17 |
-
VECTOR_MAX_TOKENS = 6000
|
| 18 |
-
VECTORS_TOKEN_OVERLAP_SIZE = 20
|
| 19 |
-
NUMBER_OF_VECTORS_FOR_RAG = 1
|
|
|
|
| 1 |
+
SYSTEM_PROMPT = """
|
| 2 |
+
You are Support bot for hitachi corporation. You must answer of any user questions using context only.
|
| 3 |
+
You have context. make answer using context only and answer must be concise and also if needed use bullet points and other markups.
|
| 4 |
+
don't metion figure number, table number,.
|
| 5 |
+
context: {context}
|
| 6 |
+
Give the answer of this Question: {question}
|
| 7 |
+
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
# MODEL = "gemini-1.5-flash"
|
| 11 |
+
MODEL = "llama-3.1-70b-versatile"
|
| 12 |
+
MAX_TOKENS = 4000
|
| 13 |
+
TRANSFORMER_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
faiss_index/index.faiss
ADDED
|
Binary file (223 kB). View file
|
|
|
faiss_index/index.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9714a57f85868cb0d2a61778b84593da45764246cfeb039ec0d933f2984edf7f
|
| 3 |
+
size 4279867
|
requirements.txt
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 1 |
+
langchain==0.2.12
|
| 2 |
+
langchain_google_genai==1.0.8
|
| 3 |
+
sentence_transformers==3.0.1
|
| 4 |
+
streamlit==1.37.0
|
| 5 |
+
streamlit_pdf_viewer==0.0.14
|
| 6 |
+
langchain_community==0.2.11
|
| 7 |
+
torch==2.3.1
|
| 8 |
+
torchaudio==2.3.1
|
| 9 |
+
torchvision==0.18.1
|
| 10 |
+
faiss-cpu==1.8.0
|
| 11 |
+
langchain_groq
|