Spaces:
Runtime error
Runtime error
Mehdi BESSAA
commited on
Commit
·
adabb02
1
Parent(s):
b73e690
feat: document search through LLM mock mode, UI polish
Browse files
app.py
CHANGED
|
@@ -1,10 +1,14 @@
|
|
| 1 |
import time
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
-
from llama_index import GPTSimpleVectorIndex
|
| 5 |
|
| 6 |
title = "Confidential forensics tool with ChatGPT"
|
| 7 |
-
examples = ["Who is Phillip Allen?", "What
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
index = GPTSimpleVectorIndex.load_from_disk('email.json')
|
| 10 |
docs_arr = []
|
|
@@ -43,9 +47,27 @@ def respond(message, chat_history):
|
|
| 43 |
chat_history.append((message, bot_message))
|
| 44 |
return "", chat_history
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
with gr.Blocks(title=title) as demo:
|
| 47 |
gr.Markdown(
|
| 48 |
"""
|
|
|
|
| 49 |
# """ + title + """
|
| 50 |
...
|
| 51 |
""")
|
|
@@ -72,15 +94,34 @@ with gr.Blocks(title=title) as demo:
|
|
| 72 |
"""
|
| 73 |
Example of queries
|
| 74 |
""")
|
| 75 |
-
i = 0
|
| 76 |
for ex in examples:
|
| 77 |
-
btn = gr.Button(
|
| 78 |
btn.click(respond2, [btn, chatbot, msg], [btn, chatbot, msg])
|
| 79 |
-
i += 1
|
| 80 |
|
| 81 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 82 |
btn_send.click(respond, [msg, chatbot], [msg, chatbot])
|
| 83 |
btn_upload.upload(respond_upload, [btn_upload, msg, chatbot], [btn_upload, msg, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
|
|
|
|
|
|
| 85 |
if __name__ == "__main__":
|
| 86 |
demo.launch()
|
|
|
|
| 1 |
import time
|
| 2 |
+
import uuid
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
| 5 |
+
from llama_index import GPTSimpleVectorIndex, MockLLMPredictor, ServiceContext
|
| 6 |
|
| 7 |
title = "Confidential forensics tool with ChatGPT"
|
| 8 |
+
examples = ["Who is Phillip Allen?", "What the project in Austin is about?", "Give me more details about the real estate project"]
|
| 9 |
+
|
| 10 |
+
llm_predictor = MockLLMPredictor()
|
| 11 |
+
service_context_mock = ServiceContext.from_defaults(llm_predictor=llm_predictor)
|
| 12 |
|
| 13 |
index = GPTSimpleVectorIndex.load_from_disk('email.json')
|
| 14 |
docs_arr = []
|
|
|
|
| 47 |
chat_history.append((message, bot_message))
|
| 48 |
return "", chat_history
|
| 49 |
|
| 50 |
+
def find_doc(opt, msg2):
|
| 51 |
+
message = ""
|
| 52 |
+
if len(msg2.strip()) < 1:
|
| 53 |
+
message = "Oops, it looks like your query was not valid. Please make sure you typed something in your text box and then try again."
|
| 54 |
+
else:
|
| 55 |
+
try:
|
| 56 |
+
resp = index.query(msg2, service_context=service_context_mock)
|
| 57 |
+
for key, item in resp.extra_info.items():
|
| 58 |
+
message += f"Document: {key}\nExtra details:\n"
|
| 59 |
+
for sub_key, sub_item in item.items():
|
| 60 |
+
message += f"---- {sub_key}: {sub_item}"
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
message = "An error occured when handling your query, please try again."
|
| 64 |
+
print(e)
|
| 65 |
+
return message, ""
|
| 66 |
+
|
| 67 |
with gr.Blocks(title=title) as demo:
|
| 68 |
gr.Markdown(
|
| 69 |
"""
|
| 70 |
+
|
| 71 |
# """ + title + """
|
| 72 |
...
|
| 73 |
""")
|
|
|
|
| 94 |
"""
|
| 95 |
Example of queries
|
| 96 |
""")
|
|
|
|
| 97 |
for ex in examples:
|
| 98 |
+
btn = gr.Button(ex)
|
| 99 |
btn.click(respond2, [btn, chatbot, msg], [btn, chatbot, msg])
|
|
|
|
| 100 |
|
| 101 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 102 |
btn_send.click(respond, [msg, chatbot], [msg, chatbot])
|
| 103 |
btn_upload.upload(respond_upload, [btn_upload, msg, chatbot], [btn_upload, msg, chatbot])
|
| 104 |
+
gr.Markdown(
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
## Search the matching document
|
| 108 |
+
""")
|
| 109 |
+
opt = gr.Textbox(
|
| 110 |
+
show_label=False,
|
| 111 |
+
placeholder="The document matching with your query will be shown here.",
|
| 112 |
+
interactive=False,
|
| 113 |
+
lines=8
|
| 114 |
+
)
|
| 115 |
+
with gr.Row():
|
| 116 |
+
with gr.Column(scale=0.85):
|
| 117 |
+
msg2 = gr.Textbox(
|
| 118 |
+
show_label=False,
|
| 119 |
+
placeholder="Enter text and press enter, or click on Send.",
|
| 120 |
+
).style(container=False)
|
| 121 |
+
with gr.Column(scale=0.15, min_width=0):
|
| 122 |
+
btn_send2 = gr.Button("Send your query")
|
| 123 |
|
| 124 |
+
btn_send2.click(find_doc, [opt, msg2], [opt, msg2])
|
| 125 |
+
|
| 126 |
if __name__ == "__main__":
|
| 127 |
demo.launch()
|