Spaces:
Sleeping
Sleeping
Kaung Myat Htet
commited on
Commit
•
7979fcd
1
Parent(s):
bb4914c
initialize project
Browse files- app.py +76 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import time
|
4 |
+
|
5 |
+
import boto3
|
6 |
+
|
7 |
+
from langchain_aws import BedrockLLM
|
8 |
+
from langchain.embeddings import BedrockEmbeddings
|
9 |
+
from langchain.vectorstores import FAISS
|
10 |
+
from langchain_core.prompts import ChatPromptTemplate
|
11 |
+
from langchain_core.output_parsers import StrOutputParser
|
12 |
+
from langchain_core.runnables import RunnablePassthrough
|
13 |
+
import gradio as gr
|
14 |
+
|
15 |
+
module_path = ".."
|
16 |
+
sys.path.append(os.path.abspath(module_path))
|
17 |
+
bedrock_client = boto3.client('bedrock-runtime',region_name=os.environ.get("AWS_DEFAULT_REGION", "us-west-2"))
|
18 |
+
|
19 |
+
modelId = 'meta.llama3-1-70b-instruct-v1:0'
|
20 |
+
|
21 |
+
llm = BedrockLLM(
|
22 |
+
model_id=modelId,
|
23 |
+
client=bedrock_client
|
24 |
+
)
|
25 |
+
|
26 |
+
br_embeddings = BedrockEmbeddings(model_id="cohere.embed-multilingual-v3", client=bedrock_client)
|
27 |
+
|
28 |
+
db = FAISS.load_local('faiss_index', embeddings=br_embeddings, allow_dangerous_deserialization=True)
|
29 |
+
retriever = db.as_retriever(k=5)
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
prompt = ChatPromptTemplate.from_messages([
|
34 |
+
('system',
|
35 |
+
"Answer the questions witht the provided context. Do not include based on the context or based on the documents in your answer."
|
36 |
+
"Please say you do not know if you do not know or cannot find the information needed."
|
37 |
+
"\n Question: {question} \nContext: {context}"),
|
38 |
+
('user', "{question}")
|
39 |
+
])
|
40 |
+
|
41 |
+
|
42 |
+
chat_history = []
|
43 |
+
|
44 |
+
def format_docs(docs):
|
45 |
+
return "\n\n".join(doc.page_content for doc in docs)
|
46 |
+
|
47 |
+
rag_chain = (
|
48 |
+
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
49 |
+
| prompt
|
50 |
+
| llm
|
51 |
+
| StrOutputParser()
|
52 |
+
)
|
53 |
+
|
54 |
+
response = rag_chain.invoke("Who are the board of directors in KCE company?")
|
55 |
+
|
56 |
+
|
57 |
+
def chat_gen(message, history):
|
58 |
+
response = rag_chain.invoke(message)
|
59 |
+
partial_message = ""
|
60 |
+
for token in response:
|
61 |
+
partial_message = partial_message + token
|
62 |
+
time.sleep(0.05)
|
63 |
+
yield partial_message
|
64 |
+
|
65 |
+
|
66 |
+
initial_msg = "Hello! I am KCE assistant. You can ask me anything about KCE. I am happy to assist you."
|
67 |
+
chatbot = gr.Chatbot(value = [[None, initial_msg]])
|
68 |
+
demo = gr.ChatInterface(chat_gen, chatbot=chatbot).queue()
|
69 |
+
|
70 |
+
try:
|
71 |
+
demo.launch(debug=True, share=False, show_api=False)
|
72 |
+
demo.close()
|
73 |
+
except Exception as e:
|
74 |
+
demo.close()
|
75 |
+
print(e)
|
76 |
+
raise e
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
langchain-aws
|
3 |
+
langchain_community
|
4 |
+
faiss-cpu
|
5 |
+
boto3
|