Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -26,7 +26,7 @@ PINECONE_ENV = os.environ.get("PINECONE_ENV", "asia-northeast1-gcp")
|
|
26 |
PINECONE_INDEX = os.environ.get("PINECONE_INDEX", "3gpp")
|
27 |
|
28 |
PINECONE_LINK = "[Pinecone](https://www.pinecone.io)"
|
29 |
-
LANGCHAIN_LINK
|
30 |
|
31 |
EMBEDDING_MODEL = os.environ.get("PINECONE_INDEX", "sentence-transformers/all-mpnet-base-v2")
|
32 |
|
@@ -35,10 +35,13 @@ TOP_K_DEFAULT = 10
|
|
35 |
TOP_K_MAX = 25
|
36 |
|
37 |
|
38 |
-
BUTTON_MIN_WIDTH =
|
39 |
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
42 |
|
43 |
FORK_BADGE = "Fork-HuggingFace Space-9cf"
|
44 |
|
@@ -46,10 +49,10 @@ FORK_BADGE = "Fork-HuggingFace Space-9cf"
|
|
46 |
def get_logo(inputs, logo) -> str:
|
47 |
return f"""https://img.shields.io/badge/{inputs}?style=flat&logo={logo}&logoColor=white"""
|
48 |
|
49 |
-
def get_status(inputs) -> str:
|
50 |
return f"""<img
|
51 |
-
src = "{get_logo(inputs,
|
52 |
-
style = "margin: 0 auto;"
|
53 |
>"""
|
54 |
|
55 |
|
@@ -57,13 +60,17 @@ KEY_INIT = "Initialize Model"
|
|
57 |
KEY_SUBMIT = "Submit"
|
58 |
KEY_CLEAR = "Clear"
|
59 |
|
60 |
-
MODEL_NULL = get_status(
|
61 |
-
MODEL_DONE = get_status(
|
62 |
-
|
63 |
|
64 |
-
|
|
|
65 |
|
66 |
TAB_1 = "Chatbot"
|
|
|
|
|
|
|
|
|
67 |
|
68 |
FAVICON = './icon.svg'
|
69 |
|
@@ -73,8 +80,17 @@ LLM_LIST = ["gpt-3.5-turbo", "text-davinci-003"]
|
|
73 |
DOC_1 = '3GPP'
|
74 |
DOC_2 = 'HTTP2'
|
75 |
|
76 |
-
DOC_SUPPORTED = [DOC_1
|
77 |
-
DOC_DEFAULT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
webui_title = """
|
80 |
# OpenAI Chatbot Based on Vector Database
|
@@ -96,51 +112,37 @@ init_message = f"""This demonstration website is based on \
|
|
96 |
|
97 |
def init_model(api_key, emb_name, db_api_key, db_env, db_index):
|
98 |
try:
|
99 |
-
if (api_key and api_key.startswith("sk-") and len(api_key) > 50)
|
100 |
-
|
101 |
-
|
102 |
-
embeddings = HuggingFaceEmbeddings(model_name=emb_name)
|
103 |
-
|
104 |
-
pinecone.init(api_key = db_api_key,
|
105 |
-
environment = db_env)
|
106 |
|
107 |
-
#llm = OpenAI(temperature=OPENAI_TEMP, model_name="gpt-3.5-turbo-0301")
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
llm_dict[llm_name] = ChatOpenAI(model_name=llm_name,
|
114 |
-
temperature = OPENAI_TEMP,
|
115 |
-
openai_api_key = api_key)
|
116 |
-
else:
|
117 |
-
llm_dict[llm_name] = OpenAI(model_name=llm_name,
|
118 |
temperature = OPENAI_TEMP,
|
119 |
openai_api_key = api_key)
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
126 |
|
127 |
-
|
128 |
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
'''
|
134 |
-
|
135 |
-
db = Pinecone.from_existing_index(index_name = db_index,
|
136 |
-
embedding = embeddings)
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
return None,MODEL_NULL,None,None,None,None
|
141 |
except Exception as e:
|
142 |
print(e)
|
143 |
-
return None,MODEL_NULL,None,None,None,None
|
144 |
|
145 |
|
146 |
def get_chat_history(inputs) -> str:
|
@@ -176,7 +178,7 @@ def bot(box_message, ref_message,
|
|
176 |
question = box_message[-1][0]
|
177 |
history = box_message[:-1]
|
178 |
|
179 |
-
if (not llm_dict)
|
180 |
box_message[-1][1] = MODEL_WARNING
|
181 |
return box_message, "", ""
|
182 |
|
@@ -190,6 +192,10 @@ def bot(box_message, ref_message,
|
|
190 |
llm = llm_dict[llm_dropdown]
|
191 |
|
192 |
if DOC_1 in doc_list:
|
|
|
|
|
|
|
|
|
193 |
chain = load_qa_chain(llm, chain_type="stuff")
|
194 |
docs = doc_similarity(ref_message, db, top_k)
|
195 |
delta_top_k = top_k - len(docs)
|
@@ -251,7 +257,7 @@ with gr.Blocks(
|
|
251 |
with gr.Column(scale=1, min_width=BUTTON_MIN_WIDTH):
|
252 |
|
253 |
init = gr.Button(KEY_INIT) #.style(full_width=False)
|
254 |
-
model_statusbox = gr.HTML(MODEL_NULL)
|
255 |
|
256 |
with gr.Tab(TAB_1):
|
257 |
with gr.Row():
|
@@ -261,7 +267,7 @@ with gr.Blocks(
|
|
261 |
with gr.Column(scale=1, min_width=BUTTON_MIN_WIDTH):
|
262 |
doc_check = gr.CheckboxGroup(choices = DOC_SUPPORTED,
|
263 |
value = DOC_DEFAULT,
|
264 |
-
label =
|
265 |
interactive=True)
|
266 |
llm_dropdown = gr.Dropdown(LLM_LIST,
|
267 |
value=LLM_LIST[0],
|
@@ -281,7 +287,7 @@ with gr.Blocks(
|
|
281 |
submit = gr.Button(KEY_SUBMIT,variant="primary")
|
282 |
|
283 |
|
284 |
-
with gr.Tab(
|
285 |
top_k = gr.Slider(1,
|
286 |
TOP_K_MAX,
|
287 |
value=TOP_K_DEFAULT,
|
@@ -290,7 +296,7 @@ with gr.Blocks(
|
|
290 |
interactive=True)
|
291 |
detail_panel = gr.Chatbot(label="Related Docs")
|
292 |
|
293 |
-
with gr.Tab(
|
294 |
with gr.Row():
|
295 |
emb_textbox = gr.Textbox(
|
296 |
label = "Embedding Model",
|
|
|
26 |
PINECONE_INDEX = os.environ.get("PINECONE_INDEX", "3gpp")
|
27 |
|
28 |
PINECONE_LINK = "[Pinecone](https://www.pinecone.io)"
|
29 |
+
LANGCHAIN_LINK = "[LangChain](https://python.langchain.com/en/latest/index.html)"
|
30 |
|
31 |
EMBEDDING_MODEL = os.environ.get("PINECONE_INDEX", "sentence-transformers/all-mpnet-base-v2")
|
32 |
|
|
|
35 |
TOP_K_MAX = 25
|
36 |
|
37 |
|
38 |
+
BUTTON_MIN_WIDTH = 215
|
39 |
|
40 |
+
LLM_NULL = "LLM-UNLOAD-critical"
|
41 |
+
LLM_DONE = "LLM-LOADED-9cf"
|
42 |
+
|
43 |
+
DB_NULL = "DB-UNLOAD-critical"
|
44 |
+
DB_DONE = "DB-LOADED-9cf"
|
45 |
|
46 |
FORK_BADGE = "Fork-HuggingFace Space-9cf"
|
47 |
|
|
|
49 |
def get_logo(inputs, logo) -> str:
|
50 |
return f"""https://img.shields.io/badge/{inputs}?style=flat&logo={logo}&logoColor=white"""
|
51 |
|
52 |
+
def get_status(inputs, logo, pos) -> str:
|
53 |
return f"""<img
|
54 |
+
src = "{get_logo(inputs, logo)}";
|
55 |
+
style = "margin: 0 auto; float:{pos}"
|
56 |
>"""
|
57 |
|
58 |
|
|
|
60 |
KEY_SUBMIT = "Submit"
|
61 |
KEY_CLEAR = "Clear"
|
62 |
|
63 |
+
MODEL_NULL = get_status(LLM_NULL, "openai", "left")
|
64 |
+
MODEL_DONE = get_status(LLM_DONE, "openai", "left")
|
|
|
65 |
|
66 |
+
DOCS_NULL = get_status(DB_NULL, "processingfoundation", "right")
|
67 |
+
DOCS_DONE = get_status(DB_DONE, "processingfoundation", "right")
|
68 |
|
69 |
TAB_1 = "Chatbot"
|
70 |
+
TAB_2 = "Details"
|
71 |
+
TAB_3 = "Database"
|
72 |
+
|
73 |
+
|
74 |
|
75 |
FAVICON = './icon.svg'
|
76 |
|
|
|
80 |
DOC_1 = '3GPP'
|
81 |
DOC_2 = 'HTTP2'
|
82 |
|
83 |
+
DOC_SUPPORTED = [DOC_1]
|
84 |
+
DOC_DEFAULT = [DOC_1]
|
85 |
+
DOC_LABEL = "Reference Docs"
|
86 |
+
|
87 |
+
|
88 |
+
MODEL_WARNING = f"Please paste your **{OPENAI_API_LINK}** and then **{KEY_INIT}**"
|
89 |
+
|
90 |
+
DOCS_WARNING = f"""Database Unloaded
|
91 |
+
Please check your **{TAB_3}** config and then **{KEY_INIT}**
|
92 |
+
Or you could uncheck **{DOC_LABEL}** to ask LLM directly"""
|
93 |
+
|
94 |
|
95 |
webui_title = """
|
96 |
# OpenAI Chatbot Based on Vector Database
|
|
|
112 |
|
113 |
def init_model(api_key, emb_name, db_api_key, db_env, db_index):
|
114 |
try:
|
115 |
+
if not (api_key and api_key.startswith("sk-") and len(api_key) > 50):
|
116 |
+
return None,MODEL_NULL+DOCS_NULL,None,None,None,None
|
117 |
+
|
|
|
|
|
|
|
|
|
118 |
|
|
|
119 |
|
120 |
+
llm_dict = {}
|
121 |
+
for llm_name in LLM_LIST:
|
122 |
+
if llm_name == "gpt-3.5-turbo":
|
123 |
+
llm_dict[llm_name] = ChatOpenAI(model_name=llm_name,
|
|
|
|
|
|
|
|
|
|
|
124 |
temperature = OPENAI_TEMP,
|
125 |
openai_api_key = api_key)
|
126 |
+
else:
|
127 |
+
llm_dict[llm_name] = OpenAI(model_name=llm_name,
|
128 |
+
temperature = OPENAI_TEMP,
|
129 |
+
openai_api_key = api_key)
|
130 |
+
|
131 |
+
if not (emb_name and db_api_key and db_env and db_index):
|
132 |
+
return api_key,MODEL_DONE+DOCS_NULL,llm_dict,None,None,None
|
133 |
|
134 |
+
embeddings = HuggingFaceEmbeddings(model_name=emb_name)
|
135 |
|
136 |
+
pinecone.init(api_key = db_api_key,
|
137 |
+
environment = db_env)
|
138 |
+
db = Pinecone.from_existing_index(index_name = db_index,
|
139 |
+
embedding = embeddings)
|
|
|
|
|
|
|
|
|
140 |
|
141 |
+
return api_key, MODEL_DONE+DOCS_DONE, llm_dict, None, db, None
|
142 |
+
|
|
|
143 |
except Exception as e:
|
144 |
print(e)
|
145 |
+
return None,MODEL_NULL+DOCS_NULL,None,None,None,None
|
146 |
|
147 |
|
148 |
def get_chat_history(inputs) -> str:
|
|
|
178 |
question = box_message[-1][0]
|
179 |
history = box_message[:-1]
|
180 |
|
181 |
+
if (not llm_dict):
|
182 |
box_message[-1][1] = MODEL_WARNING
|
183 |
return box_message, "", ""
|
184 |
|
|
|
192 |
llm = llm_dict[llm_dropdown]
|
193 |
|
194 |
if DOC_1 in doc_list:
|
195 |
+
if (not db):
|
196 |
+
box_message[-1][1] = DOCS_WARNING
|
197 |
+
return box_message, "", ""
|
198 |
+
|
199 |
chain = load_qa_chain(llm, chain_type="stuff")
|
200 |
docs = doc_similarity(ref_message, db, top_k)
|
201 |
delta_top_k = top_k - len(docs)
|
|
|
257 |
with gr.Column(scale=1, min_width=BUTTON_MIN_WIDTH):
|
258 |
|
259 |
init = gr.Button(KEY_INIT) #.style(full_width=False)
|
260 |
+
model_statusbox = gr.HTML(MODEL_NULL+DOCS_NULL)
|
261 |
|
262 |
with gr.Tab(TAB_1):
|
263 |
with gr.Row():
|
|
|
267 |
with gr.Column(scale=1, min_width=BUTTON_MIN_WIDTH):
|
268 |
doc_check = gr.CheckboxGroup(choices = DOC_SUPPORTED,
|
269 |
value = DOC_DEFAULT,
|
270 |
+
label = DOC_LABEL,
|
271 |
interactive=True)
|
272 |
llm_dropdown = gr.Dropdown(LLM_LIST,
|
273 |
value=LLM_LIST[0],
|
|
|
287 |
submit = gr.Button(KEY_SUBMIT,variant="primary")
|
288 |
|
289 |
|
290 |
+
with gr.Tab(TAB_2):
|
291 |
top_k = gr.Slider(1,
|
292 |
TOP_K_MAX,
|
293 |
value=TOP_K_DEFAULT,
|
|
|
296 |
interactive=True)
|
297 |
detail_panel = gr.Chatbot(label="Related Docs")
|
298 |
|
299 |
+
with gr.Tab(TAB_3):
|
300 |
with gr.Row():
|
301 |
emb_textbox = gr.Textbox(
|
302 |
label = "Embedding Model",
|