Spaces:
Sleeping
Sleeping
HUANG-Stephanie
commited on
Commit
•
814b6ba
1
Parent(s):
5e4e7d3
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,8 @@ import os
|
|
3 |
import sys
|
4 |
|
5 |
from fastapi import FastAPI, File, UploadFile
|
|
|
|
|
6 |
from typing import List
|
7 |
import torch
|
8 |
from pdf2image import convert_from_path
|
@@ -80,6 +82,47 @@ async def search(query: str, k: int):
|
|
80 |
|
81 |
return {"results": results}
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
if __name__ == "__main__":
|
84 |
-
|
85 |
-
uvicorn.run(app, host="0.0.0.0", port=8082, reload=True)
|
|
|
3 |
import sys
|
4 |
|
5 |
from fastapi import FastAPI, File, UploadFile
|
6 |
+
import gradio as gr
|
7 |
+
import requests
|
8 |
from typing import List
|
9 |
import torch
|
10 |
from pdf2image import convert_from_path
|
|
|
82 |
|
83 |
return {"results": results}
|
84 |
|
85 |
+
def index_gradio(file, ds):
|
86 |
+
"""Upload PDFs and get embeddings."""
|
87 |
+
url = "http://localhost:8082/index"
|
88 |
+
files = [("files", (f.name, f.file)) for f in file]
|
89 |
+
response = requests.post(url, files=files)
|
90 |
+
result = response.json()
|
91 |
+
return result['message'], ds, []
|
92 |
+
|
93 |
+
def search_gradio(query: str, ds, images, k):
|
94 |
+
"""Send a search query and get results."""
|
95 |
+
url = "http://localhost:8082/search"
|
96 |
+
payload = {'query': query, 'k': k}
|
97 |
+
response = requests.post(url, json=payload)
|
98 |
+
result = response.json()
|
99 |
+
results = result['results']
|
100 |
+
return results
|
101 |
+
|
102 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
103 |
+
gr.Markdown("# ColPali: Efficient Document Retrieval with Vision Language Models 📚")
|
104 |
+
|
105 |
+
with gr.Row():
|
106 |
+
with gr.Column(scale=2):
|
107 |
+
gr.Markdown("## 1️⃣ Upload PDFs")
|
108 |
+
file = gr.File(file_types=["pdf"], file_count="multiple", label="Upload PDFs")
|
109 |
+
|
110 |
+
convert_button = gr.Button("🔄 Index documents")
|
111 |
+
message = gr.Textbox("Files not yet uploaded", label="Status")
|
112 |
+
embeds = gr.State(value=[])
|
113 |
+
imgs = gr.State(value=[])
|
114 |
+
|
115 |
+
with gr.Column(scale=3):
|
116 |
+
gr.Markdown("## 2️⃣ Search")
|
117 |
+
query = gr.Textbox(placeholder="Enter your query here", label="Query")
|
118 |
+
k = gr.Slider(minimum=1, maximum=10, step=1, label="Number of results", value=1)
|
119 |
+
|
120 |
+
# Define the actions
|
121 |
+
search_button = gr.Button("🔍 Search", variant="primary")
|
122 |
+
output_gallery = gr.Gallery(label="Retrieved Documents", height=600, show_label=True)
|
123 |
+
|
124 |
+
convert_button.click(index_gradio, inputs=[file, embeds], outputs=[message, embeds, imgs])
|
125 |
+
search_button.click(search_gradio, inputs=[query, embeds, imgs, k], outputs=[output_gallery])
|
126 |
+
|
127 |
if __name__ == "__main__":
|
128 |
+
demo.queue(max_size=10).launch(debug=True)
|
|