ShawnAI commited on
Commit
2197c6b
·
1 Parent(s): e91fa65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -16
app.py CHANGED
@@ -23,20 +23,21 @@ PINECONE_INDEX = "3gpp"
23
 
24
  EMBEDDING_MODEL = "sentence-transformers/all-mpnet-base-v2"
25
 
26
- # return top-k text chunk from vector store
27
- VECTOR_SEARCH_TOP_K = 10
 
28
 
29
- # LLM input history length
30
- LLM_HISTORY_LEN = 3
31
 
 
32
 
33
- BUTTON_MIN_WIDTH = 150
34
-
35
- STATUS_NOK = "404-MODEL UNREADY-red"
36
- STATUS_OK = "200-MODEL LOADED-brightgreen"
37
 
38
  def get_status(inputs) -> str:
39
- return f"""<img src="https://img.shields.io/badge/{inputs}?style=flat"></a>"""
 
 
 
40
 
41
 
42
  MODEL_NULL = get_status(STATUS_NOK)
@@ -59,6 +60,9 @@ init_message = f"""Welcome to use 3GPP Chatbot, this demo toolkit is based on Op
59
  2. Insert your Question and click `{KEY_SUBMIT}`
60
  """
61
 
 
 
 
62
  def init_model(api_key):
63
  try:
64
  if api_key and api_key.startswith("sk-") and len(api_key) > 50:
@@ -104,7 +108,8 @@ def remove_duplicates(documents):
104
  def doc_similarity(query, db, top_k):
105
  docsearch = db.as_retriever(search_kwargs={'k':top_k})
106
  docs = docsearch.get_relevant_documents(query)
107
- return remove_duplicates(docs)
 
108
 
109
  def user(user_message, history):
110
  return "", history+[[user_message, None]]
@@ -152,18 +157,25 @@ def bot(box_message, ref_message, chain, db, top_k):
152
  box_message[-1][1] = bot_message
153
  return box_message, "", [[details, source]]
154
 
155
- with gr.Blocks(css=""".bigbox {
 
 
 
 
 
156
  min-height:200px;
157
- }""") as demo:
 
158
  llm_chain = gr.State()
159
  vector_db = gr.State()
160
  gr.Markdown(webui_title)
161
  gr.Markdown(init_message)
162
 
163
  with gr.Row():
164
- with gr.Column(scale=9):
165
  api_textbox = gr.Textbox(
166
  label = "OpenAI API Key",
 
167
  value = OPENAI_API_KEY,
168
  placeholder = "Paste Your OpenAI API Key (sk-...) and Hit ENTER",
169
  lines=1,
@@ -171,7 +183,7 @@ with gr.Blocks(css=""".bigbox {
171
 
172
  with gr.Column(scale=1, min_width=BUTTON_MIN_WIDTH):
173
 
174
- init = gr.Button(KEY_INIT).style(full_width=False)
175
  model_statusbox = gr.HTML(MODEL_NULL)
176
 
177
  with gr.Tab("3GPP-Chatbot"):
@@ -200,8 +212,8 @@ with gr.Blocks(css=""".bigbox {
200
 
201
  with gr.Tab("Details"):
202
  top_k = gr.Slider(1,
203
- 20,
204
- value=VECTOR_SEARCH_TOP_K,
205
  step=1,
206
  label="Vector similarity top_k",
207
  interactive=True)
@@ -226,6 +238,9 @@ with gr.Blocks(css=""".bigbox {
226
 
227
  clear.click(lambda: (None,None,None), None, [query, ref, chatbot], queue=False)
228
 
 
 
 
229
  if __name__ == "__main__":
230
  demo.launch(share=False, inbrowser=True)
231
 
 
23
 
24
  EMBEDDING_MODEL = "sentence-transformers/all-mpnet-base-v2"
25
 
26
+ # return top-k text chunks from vector store
27
+ TOP_K_DEFAULT = 10
28
+ TOP_K_MAX = 25
29
 
 
 
30
 
31
+ BUTTON_MIN_WIDTH = 180
32
 
33
+ STATUS_NOK = "404-MODEL UNREADY-critical"
34
+ STATUS_OK = "200-MODEL LOADED-9cf"
 
 
35
 
36
  def get_status(inputs) -> str:
37
+ return f"""<img
38
+ src = "https://img.shields.io/badge/{inputs}?style=flat&logo=openai";
39
+ style = "margin: 0 auto;"
40
+ >"""
41
 
42
 
43
  MODEL_NULL = get_status(STATUS_NOK)
 
60
  2. Insert your Question and click `{KEY_SUBMIT}`
61
  """
62
 
63
+ #----------------------------------------------------------------------------------------------------------
64
+ #----------------------------------------------------------------------------------------------------------
65
+
66
  def init_model(api_key):
67
  try:
68
  if api_key and api_key.startswith("sk-") and len(api_key) > 50:
 
108
  def doc_similarity(query, db, top_k):
109
  docsearch = db.as_retriever(search_kwargs={'k':top_k})
110
  docs = docsearch.get_relevant_documents(query)
111
+ udocs = remove_duplicates(docs)
112
+ return udocs
113
 
114
  def user(user_message, history):
115
  return "", history+[[user_message, None]]
 
157
  box_message[-1][1] = bot_message
158
  return box_message, "", [[details, source]]
159
 
160
+ #----------------------------------------------------------------------------------------------------------
161
+ #----------------------------------------------------------------------------------------------------------
162
+
163
+ with gr.Blocks(
164
+ theme = "Base",
165
+ css = """.bigbox {
166
  min-height:200px;
167
+ }
168
+ """) as demo:
169
  llm_chain = gr.State()
170
  vector_db = gr.State()
171
  gr.Markdown(webui_title)
172
  gr.Markdown(init_message)
173
 
174
  with gr.Row():
175
+ with gr.Column(scale=10):
176
  api_textbox = gr.Textbox(
177
  label = "OpenAI API Key",
178
+ show_label = False,
179
  value = OPENAI_API_KEY,
180
  placeholder = "Paste Your OpenAI API Key (sk-...) and Hit ENTER",
181
  lines=1,
 
183
 
184
  with gr.Column(scale=1, min_width=BUTTON_MIN_WIDTH):
185
 
186
+ init = gr.Button(KEY_INIT) #.style(full_width=False)
187
  model_statusbox = gr.HTML(MODEL_NULL)
188
 
189
  with gr.Tab("3GPP-Chatbot"):
 
212
 
213
  with gr.Tab("Details"):
214
  top_k = gr.Slider(1,
215
+ TOP_K_MAX,
216
+ value=TOP_K_DEFAULT,
217
  step=1,
218
  label="Vector similarity top_k",
219
  interactive=True)
 
238
 
239
  clear.click(lambda: (None,None,None), None, [query, ref, chatbot], queue=False)
240
 
241
+ #----------------------------------------------------------------------------------------------------------
242
+ #----------------------------------------------------------------------------------------------------------
243
+
244
  if __name__ == "__main__":
245
  demo.launch(share=False, inbrowser=True)
246