Hung Bui zhtet commited on
Commit
7cd6032
·
0 Parent(s):

Duplicate from zhtet/RegBotBeta

Browse files

Co-authored-by: zwea htet <zhtet@users.noreply.huggingface.co>

.gitattributes ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ .*pdf filter=lfs diff=lfs merge=lfs -text
36
+ assets/pdf/calregs.pdf filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ venv
2
+ data/__pycache__
3
+ models/__pycache__
4
+ .env
5
+ __pycache__
6
+ vectorStores
7
+ .vscode
Dockerfile ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /docker
4
+
5
+ ADD . /docker
6
+
7
+ COPY requirements.txt requirements.txt
8
+ RUN pip install --upgrade pip
9
+ RUN pip install --no-cache-dir -r requirements.txt
10
+
11
+ COPY . .
12
+
13
+ # Set up a new user named "user" with user ID 1000
14
+ RUN useradd -m -u 1000 user
15
+
16
+ # Switch to the "user" user
17
+ USER user
18
+
19
+ # Set home to the user's home directory
20
+ ENV HOME=/home/user \
21
+ PATH=/home/user/.local/bin:$PATH
22
+
23
+ # Set the working directory to the user's home directory
24
+ WORKDIR $HOME/app
25
+
26
+ # Set the working directory to the user's home directory
27
+ COPY --chown=user . $HOME/app
28
+
29
+ EXPOSE 8501
30
+
31
+ HEALTHCHECK CMD --fail http://localhost:8501/_stcore/health
32
+
33
+ ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
34
+ # CMD [ "streamlit" , "run", "app.py"]
35
+ # CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: RegBotBeta
3
+ emoji: 😻
4
+ colorFrom: green
5
+ colorTo: red
6
+ sdk: docker
7
+ app_file: app.py
8
+ app_port: 8501
9
+ pinned: false
10
+ duplicated_from: zhtet/RegBotBeta
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps
2
+
3
+ import os
4
+
5
+ import openai
6
+ import requests
7
+ import streamlit as st
8
+
9
+ from utils.util import *
10
+
11
+ st.set_page_config(page_title="RegBotBeta", page_icon="📜🤖")
12
+
13
+ st.title("Welcome to RegBotBeta2.0")
14
+ st.header("Powered by `LlamaIndex🦙`, `Langchain🦜🔗 ` and `OpenAI API`")
15
+
16
+ api_key = st.text_input("Enter your OpenAI API key here:", type="password")
17
+
18
+ if api_key:
19
+ resp = validate(api_key)
20
+ if "error" in resp.json():
21
+ st.info("Invalid Token! Try again.")
22
+ else:
23
+ st.info("Success")
24
+ os.environ["OPENAI_API_KEY"] = api_key
25
+ openai.api_key = api_key
26
+
27
+ if "openai_api_key" not in st.session_state:
28
+ st.session_state.openai_api_key = ""
29
+
30
+ st.session_state.openai_api_key = api_key
assets/pdf/calregs.pdf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:00f86bb345111e60d8e8f90619499f83673371efa08c29b86c2d5df27f3d9bfa
3
+ size 2434316
assets/regItems.json ADDED
The diff for this file is too large to render. See raw diff
 
assets/updated_calregs.txt ADDED
The diff for this file is too large to render. See raw diff
 
models/langOpen.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import openai
4
+ from dotenv import load_dotenv
5
+ from langchain.chains import LLMChain
6
+ from langchain.chat_models import ChatOpenAI
7
+ from langchain.embeddings.openai import OpenAIEmbeddings
8
+ from langchain.prompts import PromptTemplate
9
+ from langchain.vectorstores import FAISS
10
+
11
+ load_dotenv()
12
+
13
+ embeddings = OpenAIEmbeddings()
14
+
15
+ prompt_template = """Answer the question using the given context to the best of your ability.
16
+ If you don't know, answer I don't know.
17
+ Context: {context}
18
+ Topic: {topic}"""
19
+
20
+ PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "topic"])
21
+
22
+
23
+ class LangOpen:
24
+ def __init__(self, model_name: str) -> None:
25
+ self.index = self.initialize_index("langOpen")
26
+ self.llm = ChatOpenAI(temperature=0.3, model=model_name)
27
+ self.chain = LLMChain(llm=self.llm, prompt=PROMPT)
28
+
29
+ def initialize_index(self, index_name):
30
+ path = f"./vectorStores/{index_name}"
31
+ if os.path.exists(path=path):
32
+ return FAISS.load_local(folder_path=path, embeddings=embeddings)
33
+ else:
34
+ faiss = FAISS.from_texts(
35
+ "./assets/updated_calregs.txt", embedding=embeddings
36
+ )
37
+ faiss.save_local(path)
38
+ return faiss
39
+
40
+ def get_response(self, query_str):
41
+ print("query_str: ", query_str)
42
+ print("model_name: ", self.llm.model_name)
43
+ docs = self.index.similarity_search(query_str, k=4)
44
+ inputs = [{"context": doc.page_content, "topic": query_str} for doc in docs]
45
+ result = self.chain.apply(inputs)[0]["text"]
46
+ return result
models/llamaCustom.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pickle
3
+ from json import dumps, loads
4
+ from typing import Any, List, Mapping, Optional
5
+
6
+ import numpy as np
7
+ import openai
8
+ import pandas as pd
9
+ from dotenv import load_dotenv
10
+ from huggingface_hub import HfFileSystem
11
+ from langchain.llms.base import LLM
12
+ from llama_index import (
13
+ Document,
14
+ GPTVectorStoreIndex,
15
+ LLMPredictor,
16
+ PromptHelper,
17
+ ServiceContext,
18
+ SimpleDirectoryReader,
19
+ StorageContext,
20
+ load_index_from_storage,
21
+ )
22
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
23
+
24
+ # from utils.customLLM import CustomLLM
25
+
26
+ load_dotenv()
27
+ # openai.api_key = os.getenv("OPENAI_API_KEY")
28
+ fs = HfFileSystem()
29
+
30
+ # define prompt helper
31
+ # set maximum input size
32
+ CONTEXT_WINDOW = 2048
33
+ # set number of output tokens
34
+ NUM_OUTPUT = 525
35
+ # set maximum chunk overlap
36
+ CHUNK_OVERLAP_RATION = 0.2
37
+
38
+ prompt_helper = PromptHelper(
39
+ context_window=CONTEXT_WINDOW,
40
+ num_output=NUM_OUTPUT,
41
+ chunk_overlap_ratio=CHUNK_OVERLAP_RATION,
42
+ )
43
+
44
+ llm_model_name = "bigscience/bloom-560m"
45
+ tokenizer = AutoTokenizer.from_pretrained(llm_model_name)
46
+ model = AutoModelForCausalLM.from_pretrained(llm_model_name, config="T5Config")
47
+
48
+ model_pipeline = pipeline(
49
+ model=model,
50
+ tokenizer=tokenizer,
51
+ task="text-generation",
52
+ # device=0, # GPU device number
53
+ # max_length=512,
54
+ do_sample=True,
55
+ top_p=0.95,
56
+ top_k=50,
57
+ temperature=0.7,
58
+ )
59
+
60
+
61
+ class CustomLLM(LLM):
62
+ pipeline = model_pipeline
63
+
64
+ def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
65
+ prompt_length = len(prompt)
66
+ response = self.pipeline(prompt, max_new_tokens=525)[0]["generated_text"]
67
+
68
+ # only return newly generated tokens
69
+ return response[prompt_length:]
70
+
71
+ @property
72
+ def _identifying_params(self) -> Mapping[str, Any]:
73
+ return {"name_of_model": self.model_name}
74
+
75
+ @property
76
+ def _llm_type(self) -> str:
77
+ return "custom"
78
+
79
+
80
+ class LlamaCustom:
81
+ # define llm
82
+ llm_predictor = LLMPredictor(llm=CustomLLM())
83
+ service_context = ServiceContext.from_defaults(
84
+ llm_predictor=llm_predictor, prompt_helper=prompt_helper
85
+ )
86
+
87
+ def __init__(self, name: str) -> None:
88
+ self.vector_index = self.initialize_index(index_name=name)
89
+
90
+ def initialize_index(self, index_name):
91
+ file_path = f"./vectorStores/{index_name}"
92
+ if os.path.exists(path=file_path):
93
+ # rebuild storage context
94
+ storage_context = StorageContext.from_defaults(persist_dir=file_path)
95
+
96
+ # local load index access
97
+ index = load_index_from_storage(storage_context)
98
+
99
+ # huggingface repo load access
100
+ # with fs.open(file_path, "r") as file:
101
+ # index = pickle.loads(file.readlines())
102
+ return index
103
+ else:
104
+ # documents = prepare_data(r"./assets/regItems.json")
105
+ documents = SimpleDirectoryReader(input_dir="./assets/pdf").load_data()
106
+
107
+ index = GPTVectorStoreIndex.from_documents(
108
+ documents, service_context=self.service_context
109
+ )
110
+
111
+ # local write access
112
+ index.storage_context.persist(file_path)
113
+
114
+ # huggingface repo write access
115
+ # with fs.open(file_path, "w") as file:
116
+ # file.write(pickle.dumps(index))
117
+ return index
118
+
119
+ def get_response(self, query_str):
120
+ print("query_str: ", query_str)
121
+ query_engine = self.vector_index.as_query_engine()
122
+ response = query_engine.query(query_str)
123
+ return str(response)
pages/langchain_demo.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import openai
4
+ import streamlit as st
5
+
6
+ from models.langOpen import LangOpen
7
+ from utils.chatbox import chatbox
8
+
9
+ st.set_page_config(page_title="Langchain", page_icon="")
10
+
11
+ st.subheader("Langchain with OpenAI Demo")
12
+
13
+ if "messages" not in st.session_state:
14
+ st.session_state.messages = []
15
+
16
+ if "openai_api_key" not in st.session_state:
17
+ st.info("Enter your openai key to access the chatbot.")
18
+ else:
19
+ option = st.selectbox(
20
+ label="Select your model:", options=("gpt-3.5-turbo", "gpt-4"), index=0
21
+ )
22
+
23
+ with st.spinner(f"Initializing {option} ..."):
24
+ langOpen = LangOpen(model_name=option)
25
+
26
+ chatbox("lang_open", langOpen)
pages/llama_custom_demo.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+
4
+ import openai
5
+ import streamlit as st
6
+
7
+ from models.llamaCustom import LlamaCustom
8
+ from utils.chatbox import *
9
+
10
+ st.set_page_config(page_title="Llama", page_icon="🦙")
11
+
12
+ st.subheader("Llama Index with Custom LLM Demo")
13
+
14
+ if "messages" not in st.session_state:
15
+ st.session_state.messages = []
16
+
17
+ if "openai_api_key" not in st.session_state:
18
+ st.info("Enter your openai key to access the chatbot.")
19
+ else:
20
+ with st.spinner("Initializing vector index"):
21
+ model = LlamaCustom(name="llamaCustom")
22
+
23
+ chatbox("llama_custom", model)
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ llama_index
2
+ torch
3
+ transformers
4
+ panda
5
+ numpy
6
+ langchain
7
+ openai
8
+ faiss-cpu
9
+ python-dotenv
10
+ streamlit>=1.24.0
11
+ huggingface_hub
12
+ xformers
13
+ pypdf
utils/__init__.py ADDED
File without changes
utils/chatbox.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+
3
+ import streamlit as st
4
+
5
+ def display_chat_history(model_name: str):
6
+ for message in st.session_state[model_name]:
7
+ with st.chat_message(message["role"]):
8
+ st.markdown(message["content"])
9
+
10
+ def chat_input(model_name: str):
11
+ if prompt := st.chat_input("Say something"):
12
+ # Display user message in chat message container
13
+ st.chat_message("user").markdown(prompt)
14
+
15
+ # Add user message to chat history
16
+ st.session_state[model_name].append({"role": "user", "content": prompt})
17
+
18
+ return prompt
19
+
20
+ def display_bot_msg(model_name: str, bot_response: str):
21
+ # Display assistant response in chat message container
22
+ with st.chat_message("assistant"):
23
+ message_placeholder = st.empty()
24
+ full_response = ""
25
+
26
+ # simulate the chatbot "thinking" before responding
27
+ # (or stream its response)
28
+ for chunk in bot_response.split():
29
+ full_response += chunk + " "
30
+ time.sleep(0.05)
31
+
32
+ # add a blinking cursor to simulate typing
33
+ message_placeholder.markdown(full_response + "▌")
34
+
35
+ message_placeholder.markdown(full_response)
36
+ # st.markdown(response)
37
+
38
+ # Add assistant response to chat history
39
+ st.session_state[model_name].append(
40
+ {"model_name": model_name, "role": "assistant", "content": full_response}
41
+ )
42
+
43
+ # @st.cache_data
44
+ def chatbox(model_name: str, model: None):
45
+ # Display chat messages from history on app rerun
46
+ for message in st.session_state.messages:
47
+ if (message["model_name"] == model_name):
48
+ with st.chat_message(message["role"]):
49
+ st.markdown(message["content"])
50
+
51
+ if prompt := st.chat_input("Say something"):
52
+ # Display user message in chat message container
53
+ st.chat_message("user").markdown(prompt)
54
+
55
+ # Add user message to chat history
56
+ st.session_state.messages.append({"model_name": model_name, "role": "user", "content": prompt})
57
+
58
+ with st.spinner("Processing your query..."):
59
+ bot_response = model.get_response(prompt)
60
+
61
+ print("bot: ", bot_response)
62
+
63
+ # Display assistant response in chat message container
64
+ with st.chat_message("assistant"):
65
+ message_placeholder = st.empty()
66
+ full_response = ""
67
+
68
+ # simulate the chatbot "thinking" before responding
69
+ # (or stream its response)
70
+ for chunk in bot_response.split():
71
+ full_response += chunk + " "
72
+ time.sleep(0.05)
73
+
74
+ # add a blinking cursor to simulate typing
75
+ message_placeholder.markdown(full_response + "▌")
76
+
77
+ message_placeholder.markdown(full_response)
78
+ # st.markdown(response)
79
+
80
+ # Add assistant response to chat history
81
+ st.session_state.messages.append(
82
+ {"model_name": model_name, "role": "assistant", "content": full_response}
83
+ )
84
+
85
+ # Scroll to the bottom of the chat container
86
+ # st.markdown(
87
+ # """
88
+ # <script>
89
+ # const chatContainer = document.getElementsByClassName("css-1n76uvr")[0];
90
+ # chatContainer.scrollTop = chatContainer.scrollHeight;
91
+ # </script>
92
+ # """,
93
+ # unsafe_allow_html=True,
94
+ # )
utils/util.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+
3
+
4
+ def validate(token: str):
5
+ api_endpoint = "https://api.openai.com/v1/chat/completions"
6
+ api_key = token
7
+
8
+ headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"}
9
+
10
+ messages = [{"role": "user", "content": "Say this is a test!"}]
11
+
12
+ data = {"model": "gpt-3.5-turbo", "messages": messages}
13
+
14
+ response = requests.post(api_endpoint, json=data, headers=headers)
15
+ return response