JoFrost commited on
Commit
c03941e
1 Parent(s): adabb02

feat: UI polish again, improve search in doc features & document list is now more explicit

Browse files
Files changed (4) hide show
  1. app.py +44 -31
  2. email.json +0 -0
  3. email_200.parquet +3 -0
  4. requirements.txt +8 -1
app.py CHANGED
@@ -1,20 +1,49 @@
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 = []
15
- for doc in index.docstore.docs:
16
- docs_arr.append(doc)
17
- dat_fr = pd.DataFrame({"Documents loaded": docs_arr})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  def respond_upload(btn_upload, message, chat_history):
20
  time.sleep(2)
@@ -47,48 +76,32 @@ def respond(message, chat_history):
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
  """)
74
  dat = gr.Dataframe(
75
- value=dat_fr
 
 
 
76
  )
 
77
  gr.Markdown(
78
  """
79
  ## Chatbot
80
  """)
81
  chatbot = gr.Chatbot().style(height=400)
82
  with gr.Row():
83
- with gr.Column(scale=0.70):
84
  msg = gr.Textbox(
85
  show_label=False,
86
  placeholder="Enter text and press enter, or click on Send.",
87
  ).style(container=False)
88
  with gr.Column(scale=0.15, min_width=0):
89
  btn_send = gr.Button("Send your query")
90
- with gr.Column(scale=0.15, min_width=0):
91
- btn_upload = gr.UploadButton("Upload a new document...", file_types=["text"])
92
  with gr.Row():
93
  gr.Markdown(
94
  """
@@ -121,7 +134,7 @@ with gr.Blocks(title=title) as demo:
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()
 
1
  import time
2
  import uuid
3
+ import openai
4
+ import os
5
  import pandas as pd
6
+ import numpy as np
7
  import gradio as gr
8
+ from llama_index import GPTSimpleVectorIndex
9
+ from gpt_index.indices.struct_store.pandas import GPTPandasIndex
10
+ from openai.embeddings_utils import get_embedding, cosine_similarity
11
+
12
+ openai.api_key = os.getenv("OPENAI_API_KEY")
13
 
14
  title = "Confidential forensics tool with ChatGPT"
15
  examples = ["Who is Phillip Allen?", "What the project in Austin is about?", "Give me more details about the real estate project"]
16
 
 
 
 
17
  index = GPTSimpleVectorIndex.load_from_disk('email.json')
18
+
19
+ dat_fr = pd.DataFrame({"Documents loaded": ["email.json", "email_200.parquet"]})
20
+
21
+ df = pd.read_parquet("email_200.parquet")
22
+ # df["embedding"] = [get_embedding(x) forW x in df.body.values]
23
+ # df.to_parquet("email_50.parquet")
24
+
25
+ # df = pd.read_csv("email_ok.csv", nrows=50)
26
+ # df["embedding"] = [get_embedding(x) for x in df.body.values]
27
+ # df.to_parquet("email_50.parquet")
28
+
29
+ def search_emails(opt, message, n=3):
30
+ "Outputs the top n emails that match the most the pattern"
31
+ if len(message.strip()) < 1:
32
+ message = "Oops, it looks like your query was not valid. Please make sure you typed something in your text box and then try again."
33
+ else:
34
+ try:
35
+ embedding = get_embedding(message)
36
+ message = ""
37
+ df['similarities'] = df.embedding.apply(lambda x: cosine_similarity(x, embedding))
38
+
39
+ message_tmp = df.sort_values('similarities', ascending=False).head(n)
40
+ message_tmp = [(row.file, row.body, row.similarities) for index, row in message_tmp.iterrows()]
41
+ for msg in message_tmp:
42
+ message += f"{msg[0]}\nContent: {msg[1].strip()}\n{msg[2]}\n\n"
43
+ except Exception as e:
44
+ message = "An error occured when handling your query, please try again."
45
+ print(e)
46
+ return message, ""
47
 
48
  def respond_upload(btn_upload, message, chat_history):
49
  time.sleep(2)
 
76
  chat_history.append((message, bot_message))
77
  return "", chat_history
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  with gr.Blocks(title=title) as demo:
80
  gr.Markdown(
81
  """
82
 
83
  # """ + title + """
 
84
  """)
85
  dat = gr.Dataframe(
86
+ value=dat_fr,
87
+ max_cols=1,
88
+ max_rows=4,
89
+ overflow_row_behaviour="paginate",
90
  )
91
+ btn_upload = gr.UploadButton("Upload a new document...", file_types=["text"])
92
  gr.Markdown(
93
  """
94
  ## Chatbot
95
  """)
96
  chatbot = gr.Chatbot().style(height=400)
97
  with gr.Row():
98
+ with gr.Column(scale=0.85):
99
  msg = gr.Textbox(
100
  show_label=False,
101
  placeholder="Enter text and press enter, or click on Send.",
102
  ).style(container=False)
103
  with gr.Column(scale=0.15, min_width=0):
104
  btn_send = gr.Button("Send your query")
 
 
105
  with gr.Row():
106
  gr.Markdown(
107
  """
 
134
  with gr.Column(scale=0.15, min_width=0):
135
  btn_send2 = gr.Button("Send your query")
136
 
137
+ btn_send2.click(search_emails, [opt, msg2], [opt, msg2])
138
 
139
  if __name__ == "__main__":
140
  demo.launch()
email.json CHANGED
The diff for this file is too large to render. See raw diff
 
email_200.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:689f185923970ee7a8da61eb63ae2116d75af0a473dd2735c0848bc87b505135
3
+ size 8670196
requirements.txt CHANGED
@@ -1,3 +1,10 @@
1
  gradio
2
  llama_index
3
- pandas
 
 
 
 
 
 
 
 
1
  gradio
2
  llama_index
3
+ pandas
4
+ gpt_index
5
+ numpy
6
+ openai
7
+ plotly
8
+ scipy
9
+ scikit-learn
10
+ fastparquet