bokesyo commited on
Commit
9a6b755
·
1 Parent(s): d3da251
Files changed (1) hide show
  1. app.py +61 -10
app.py CHANGED
@@ -40,7 +40,7 @@ def calculate_md5_from_binary(binary_data):
40
  hash_md5.update(binary_data)
41
  return hash_md5.hexdigest()
42
 
43
- @spaces.GPU(duration=90)
44
  def add_pdf_gradio(pdf_file_binary, progress=gr.Progress()):
45
  global model, tokenizer
46
 
@@ -84,6 +84,7 @@ def add_pdf_gradio(pdf_file_binary, progress=gr.Progress()):
84
 
85
  return knowledge_base_name
86
 
 
87
  def retrieve_gradio(knowledge_base: str, query: str, topk: int):
88
  global model, tokenizer
89
 
@@ -104,12 +105,6 @@ def retrieve_gradio(knowledge_base: str, query: str, topk: int):
104
  query_rep = model(text=[query_with_instruction], image=[None], tokenizer=tokenizer).reps.squeeze(0).cpu()
105
 
106
  query_md5 = hashlib.md5(query.encode()).hexdigest()
107
- with open(os.path.join(target_cache_dir, f"q-{query_md5}.json"), 'w') as f:
108
- f.write(json.dumps(
109
- {
110
- "query": query
111
- }, indent=4, ensure_ascii=False
112
- ))
113
 
114
  doc_reps_cat = torch.stack([torch.Tensor(i) for i in doc_reps], dim=0)
115
 
@@ -125,9 +120,57 @@ def retrieve_gradio(knowledge_base: str, query: str, topk: int):
125
 
126
  images_topk = [Image.open(os.path.join(target_cache_dir, f"{md5s[idx]}.png")) for idx in topk_doc_ids_np]
127
 
 
 
 
 
 
 
 
 
 
128
  return images_topk
129
 
130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  device = 'cuda'
132
  model_path = 'RhapsodyAI/minicpm-visual-embedding-v0' # replace with your local model path
133
  tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
@@ -136,9 +179,10 @@ model.to(device)
136
 
137
 
138
  with gr.Blocks() as app:
139
- gr.Markdown("# Memex: OCR-free Visual Document Retrieval @RhapsodyAI [RhapsodyAI/minicpm-visual-embedding-v0](https://huggingface.co/RhapsodyAI/minicpm-visual-embedding-v0)")
140
 
141
- gr.Markdown("Only PDF file is available now. You are suggested to uplaod <= 50 pages per PDF, otherwise you will reach GPU time limit.")
 
142
 
143
  with gr.Row():
144
  file_input = gr.File(type="binary", label="Upload PDF")
@@ -148,16 +192,23 @@ with gr.Blocks() as app:
148
  process_button.click(add_pdf_gradio, inputs=[file_input], outputs=file_result)
149
 
150
  with gr.Row():
151
- kb_id_input = gr.Text(label="Your Knowledge Base ID")
152
  query_input = gr.Text(label="Your Queston")
153
  topk_input = inputs=gr.Number(value=1, minimum=1, maximum=5, step=1, label="Top K")
154
  retrieve_button = gr.Button("Retrieve")
155
 
 
 
 
 
156
  with gr.Row():
157
  images_output = gr.Gallery(label="Retrieved Pages")
158
 
159
  retrieve_button.click(retrieve_gradio, inputs=[kb_id_input, query_input, topk_input], outputs=images_output)
160
 
 
 
 
161
  gr.Markdown("By using this demo, you agree to share your use data with us for research purpose, to help improve user experience.")
162
 
163
  app.launch()
 
40
  hash_md5.update(binary_data)
41
  return hash_md5.hexdigest()
42
 
43
+ @spaces.GPU(duration=100)
44
  def add_pdf_gradio(pdf_file_binary, progress=gr.Progress()):
45
  global model, tokenizer
46
 
 
84
 
85
  return knowledge_base_name
86
 
87
+ # @spaces.GPU
88
  def retrieve_gradio(knowledge_base: str, query: str, topk: int):
89
  global model, tokenizer
90
 
 
105
  query_rep = model(text=[query_with_instruction], image=[None], tokenizer=tokenizer).reps.squeeze(0).cpu()
106
 
107
  query_md5 = hashlib.md5(query.encode()).hexdigest()
 
 
 
 
 
 
108
 
109
  doc_reps_cat = torch.stack([torch.Tensor(i) for i in doc_reps], dim=0)
110
 
 
120
 
121
  images_topk = [Image.open(os.path.join(target_cache_dir, f"{md5s[idx]}.png")) for idx in topk_doc_ids_np]
122
 
123
+ with open(os.path.join(target_cache_dir, f"q-{query_md5}.json"), 'w') as f:
124
+ f.write(json.dumps(
125
+ {
126
+ "knowledge_base": knowledge_base,
127
+ "query": query,
128
+ "retrived_docs": [os.path.join(target_cache_dir, f"{md5s[idx]}.png") for idx in topk_doc_ids_np]
129
+ }, indent=4, ensure_ascii=False
130
+ ))
131
+
132
  return images_topk
133
 
134
 
135
+ def upvote(knowledge_base, query):
136
+ global model, tokenizer
137
+
138
+ target_cache_dir = os.path.join(cache_dir, knowledge_base)
139
+
140
+ query_md5 = hashlib.md5(query.encode()).hexdigest()
141
+
142
+ with open(os.path.join(target_cache_dir, f"q-{query_md5}.json"), 'r') as f:
143
+ data = json.loads(f.read())
144
+
145
+ data["user_preference"] = "upvote"
146
+
147
+ with open(os.path.join(target_cache_dir, f"q-{query_md5}-withpref.json"), 'w') as f:
148
+ f.write(json.dumps(data, indent=4, ensure_ascii=False))
149
+
150
+ print("up", os.path.join(target_cache_dir, f"q-{query_md5}-withpref.json"))
151
+
152
+ return
153
+
154
+
155
+ def downvote(knowledge_base, query):
156
+ global model, tokenizer
157
+
158
+ target_cache_dir = os.path.join(cache_dir, knowledge_base)
159
+
160
+ query_md5 = hashlib.md5(query.encode()).hexdigest()
161
+
162
+ with open(os.path.join(target_cache_dir, f"q-{query_md5}.json"), 'r') as f:
163
+ data = json.loads(f.read())
164
+
165
+ data["user_preference"] = "downvote"
166
+
167
+ with open(os.path.join(target_cache_dir, f"q-{query_md5}-withpref.json"), 'w') as f:
168
+ f.write(json.dumps(data, indent=4, ensure_ascii=False))
169
+
170
+ print("down", os.path.join(target_cache_dir, f"q-{query_md5}-withpref.json"))
171
+ return
172
+
173
+
174
  device = 'cuda'
175
  model_path = 'RhapsodyAI/minicpm-visual-embedding-v0' # replace with your local model path
176
  tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
 
179
 
180
 
181
  with gr.Blocks() as app:
182
+ gr.Markdown("# Memex: OCR-free Visual Document Retrieval @RhapsodyAI")
183
 
184
+ gr.Markdown("- We open-sourced our model at [RhapsodyAI/minicpm-visual-embedding-v0](https://huggingface.co/RhapsodyAI/minicpm-visual-embedding-v0)")
185
+ gr.Markdown("- Currently we support PDF document with less than 50 pages, PDF over 50 pages will reach GPU time limit.")
186
 
187
  with gr.Row():
188
  file_input = gr.File(type="binary", label="Upload PDF")
 
192
  process_button.click(add_pdf_gradio, inputs=[file_input], outputs=file_result)
193
 
194
  with gr.Row():
195
+ kb_id_input = gr.Text(label="Your Knowledge Base ID (paste your Knowledge Base ID here:)")
196
  query_input = gr.Text(label="Your Queston")
197
  topk_input = inputs=gr.Number(value=1, minimum=1, maximum=5, step=1, label="Top K")
198
  retrieve_button = gr.Button("Retrieve")
199
 
200
+ with gr.Row():
201
+ downvote_button = gr.Button("🤣Downvote")
202
+ upvote_button = gr.Button("🤗Upvote")
203
+
204
  with gr.Row():
205
  images_output = gr.Gallery(label="Retrieved Pages")
206
 
207
  retrieve_button.click(retrieve_gradio, inputs=[kb_id_input, query_input, topk_input], outputs=images_output)
208
 
209
+ upvote_button.click(upvote, inputs=[kb_id_input, query_input], outputs=None)
210
+ downvote_button.click(downvote, inputs=[kb_id_input, query_input], outputs=None)
211
+
212
  gr.Markdown("By using this demo, you agree to share your use data with us for research purpose, to help improve user experience.")
213
 
214
  app.launch()