Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -50,11 +50,14 @@ webui_title = """
|
|
| 50 |
|
| 51 |
"""
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
"""
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
def init_model(api_key):
|
| 60 |
try:
|
|
@@ -89,6 +92,20 @@ def get_chat_history(inputs) -> str:
|
|
| 89 |
res.append(f"Human: {human}\nAI: {ai}")
|
| 90 |
return "\n".join(res)
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
def user(user_message, history):
|
| 93 |
return "", history+[[user_message, None]]
|
| 94 |
|
|
@@ -108,10 +125,15 @@ def bot(box_message, ref_message, chain, db, top_k):
|
|
| 108 |
details = f"Q: {question}"
|
| 109 |
else:
|
| 110 |
details = f"Q: {question}\nR: {ref_message}"
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
docs =
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
all_output = chain({"input_documents": docs,
|
| 117 |
"question": question,
|
|
@@ -130,7 +152,6 @@ def bot(box_message, ref_message, chain, db, top_k):
|
|
| 130 |
box_message[-1][1] = bot_message
|
| 131 |
return box_message, "", [[details, source]]
|
| 132 |
|
| 133 |
-
|
| 134 |
with gr.Blocks(css=""".bigbox {
|
| 135 |
min-height:200px;
|
| 136 |
}""") as demo:
|
|
@@ -150,7 +171,7 @@ with gr.Blocks(css=""".bigbox {
|
|
| 150 |
|
| 151 |
with gr.Column(scale=1, min_width=BUTTON_MIN_WIDTH):
|
| 152 |
|
| 153 |
-
init = gr.Button(
|
| 154 |
model_statusbox = gr.HTML(MODEL_NULL)
|
| 155 |
|
| 156 |
with gr.Tab("3GPP-Chatbot"):
|
|
@@ -173,8 +194,8 @@ with gr.Blocks(css=""".bigbox {
|
|
| 173 |
lines=2)
|
| 174 |
ref = gr.Textbox(label="Reference(optional):")
|
| 175 |
with gr.Column(scale=1, min_width=BUTTON_MIN_WIDTH):
|
| 176 |
-
clear = gr.Button(
|
| 177 |
-
submit = gr.Button(
|
| 178 |
|
| 179 |
|
| 180 |
with gr.Tab("Details"):
|
|
@@ -207,4 +228,4 @@ with gr.Blocks(css=""".bigbox {
|
|
| 207 |
|
| 208 |
if __name__ == "__main__":
|
| 209 |
demo.launch(share=False, inbrowser=True)
|
| 210 |
-
|
|
|
|
| 50 |
|
| 51 |
"""
|
| 52 |
|
| 53 |
+
KEY_INIT = "Initialize Model"
|
| 54 |
+
KEY_SUBMIT = "Submit"
|
| 55 |
+
KEY_CLEAR = "Clear"
|
|
|
|
| 56 |
|
| 57 |
+
init_message = f"""Welcome to use 3GPP Chatbot, this demo toolkit is based on OpenAI with LangChain and Pinecone
|
| 58 |
+
1. Insert your OpenAI API key and click `{KEY_INIT}`
|
| 59 |
+
2. Insert your Question and click `{KEY_SUBMIT}`
|
| 60 |
+
"""
|
| 61 |
|
| 62 |
def init_model(api_key):
|
| 63 |
try:
|
|
|
|
| 92 |
res.append(f"Human: {human}\nAI: {ai}")
|
| 93 |
return "\n".join(res)
|
| 94 |
|
| 95 |
+
def remove_duplicates(documents):
|
| 96 |
+
seen_content = set()
|
| 97 |
+
unique_documents = []
|
| 98 |
+
for doc in documents:
|
| 99 |
+
if doc.page_content not in seen_content:
|
| 100 |
+
seen_content.add(doc.page_content)
|
| 101 |
+
unique_documents.append(doc)
|
| 102 |
+
return unique_documents
|
| 103 |
+
|
| 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]]
|
| 111 |
|
|
|
|
| 125 |
details = f"Q: {question}"
|
| 126 |
else:
|
| 127 |
details = f"Q: {question}\nR: {ref_message}"
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
docs = doc_similarity(ref_message, db, top_k)
|
| 131 |
+
|
| 132 |
+
delta_top_k = top_k - len(docs)
|
| 133 |
+
|
| 134 |
+
if delta_top_k > 0:
|
| 135 |
+
docs = doc_similarity(ref_message, db, top_k+delta_top_k)
|
| 136 |
+
print(docs)
|
| 137 |
|
| 138 |
all_output = chain({"input_documents": docs,
|
| 139 |
"question": question,
|
|
|
|
| 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:
|
|
|
|
| 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"):
|
|
|
|
| 194 |
lines=2)
|
| 195 |
ref = gr.Textbox(label="Reference(optional):")
|
| 196 |
with gr.Column(scale=1, min_width=BUTTON_MIN_WIDTH):
|
| 197 |
+
clear = gr.Button(KEY_CLEAR)
|
| 198 |
+
submit = gr.Button(KEY_SUBMIT,variant="primary")
|
| 199 |
|
| 200 |
|
| 201 |
with gr.Tab("Details"):
|
|
|
|
| 228 |
|
| 229 |
if __name__ == "__main__":
|
| 230 |
demo.launch(share=False, inbrowser=True)
|
| 231 |
+
|