Spaces:
Runtime error
Runtime error
feat: documents loaded are now listed, UI polished and example added
Browse files- app.py +77 -11
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,20 +1,86 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from llama_index import GPTSimpleVectorIndex
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
msg = gr.Textbox()
|
7 |
-
clear = gr.Button("Clear")
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
15 |
-
|
|
|
16 |
|
17 |
if __name__ == "__main__":
|
18 |
-
global index
|
19 |
-
index = GPTSimpleVectorIndex.load_from_disk('email.json')
|
20 |
demo.launch()
|
|
|
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 is the project in Austin is about?", "Give me more details about the real estate project"]
|
|
|
|
|
8 |
|
9 |
+
index = GPTSimpleVectorIndex.load_from_disk('email.json')
|
10 |
+
docs_arr = []
|
11 |
+
for doc in index.docstore.docs:
|
12 |
+
docs_arr.append(doc)
|
13 |
+
dat_fr = pd.DataFrame({"Documents loaded": docs_arr})
|
14 |
+
|
15 |
+
def respond_upload(btn_upload, message, chat_history):
|
16 |
+
time.sleep(2)
|
17 |
+
message = "***File uploaded***"
|
18 |
+
bot_message = "Your document has been uploaded and will be accounted for your queries."
|
19 |
+
chat_history.append((message, bot_message))
|
20 |
+
return btn_upload, "", chat_history
|
21 |
+
|
22 |
+
def respond2(message, chat_history, box, btn):
|
23 |
+
if len(message.strip()) < 1:
|
24 |
+
message = "***Empty***"
|
25 |
+
bot_message = "Oops, it looks like your query was not valid. Please make sure you typed something in your text box and then try again."
|
26 |
+
else:
|
27 |
+
try:
|
28 |
+
bot_message = str(index.query(message)).strip()
|
29 |
+
except:
|
30 |
+
bot_message = "An error occured when handling your query, please try again."
|
31 |
+
chat_history.append((message, bot_message))
|
32 |
+
return message, chat_history, box
|
33 |
+
|
34 |
+
def respond(message, chat_history):
|
35 |
+
if len(message.strip()) < 1:
|
36 |
+
message = "***Empty***"
|
37 |
+
bot_message = "Oops, it looks like your query was not valid. Please make sure you typed something in your text box and then try again."
|
38 |
+
else:
|
39 |
+
try:
|
40 |
+
bot_message = str(index.query(message)).strip()
|
41 |
+
except:
|
42 |
+
bot_message = "An error occured when handling your query, please try again."
|
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 |
+
""")
|
52 |
+
dat = gr.Dataframe(
|
53 |
+
value=dat_fr
|
54 |
+
)
|
55 |
+
gr.Markdown(
|
56 |
+
"""
|
57 |
+
## Chatbot
|
58 |
+
""")
|
59 |
+
chatbot = gr.Chatbot().style(height=400)
|
60 |
+
with gr.Row():
|
61 |
+
with gr.Column(scale=0.70):
|
62 |
+
msg = gr.Textbox(
|
63 |
+
show_label=False,
|
64 |
+
placeholder="Enter text and press enter, or click on Send.",
|
65 |
+
).style(container=False)
|
66 |
+
with gr.Column(scale=0.15, min_width=0):
|
67 |
+
btn_send = gr.Button("Send your query")
|
68 |
+
with gr.Column(scale=0.15, min_width=0):
|
69 |
+
btn_upload = gr.UploadButton("Upload a new document...", file_types=["text"])
|
70 |
+
with gr.Row():
|
71 |
+
gr.Markdown(
|
72 |
+
"""
|
73 |
+
Example of queries
|
74 |
+
""")
|
75 |
+
i = 0
|
76 |
+
for ex in examples:
|
77 |
+
btn = gr.Button(examples[i])
|
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()
|
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
gradio
|
2 |
-
llama_index
|
|
|
|
1 |
gradio
|
2 |
+
llama_index
|
3 |
+
pandas
|