Spaces:
Sleeping
Sleeping
HonestAnnie
commited on
Commit
•
e59705e
1
Parent(s):
92e9dc6
coppied changes from german version
Browse files
app.py
CHANGED
@@ -20,7 +20,7 @@ def get_embeddings(queries, task):
|
|
20 |
def query_chroma(collection, embedding, authors):
|
21 |
results = collection.query(
|
22 |
query_embeddings=[embedding.tolist()],
|
23 |
-
n_results=
|
24 |
where={"author": {"$in": authors}} if authors else {},
|
25 |
include=["documents", "metadatas", "distances"]
|
26 |
)
|
@@ -34,10 +34,10 @@ def query_chroma(collection, embedding, authors):
|
|
34 |
for id_, metadata, document_text, distance in zip(ids, metadatas, documents, distances):
|
35 |
result_dict = {
|
36 |
"id": id_,
|
37 |
-
"author": metadata.get('author', '
|
38 |
-
"book": metadata.get('book', '
|
39 |
-
"section": metadata.get('section', '
|
40 |
-
"title": metadata.get('title', '
|
41 |
"text": document_text,
|
42 |
"distance": distance
|
43 |
}
|
@@ -45,12 +45,88 @@ def query_chroma(collection, embedding, authors):
|
|
45 |
|
46 |
return formatted_results
|
47 |
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
#database_inp = gr.Dropdown(label="Database", choices=["German", "English"], value="German")
|
51 |
author_inp = gr.Dropdown(label="Authors", choices=authors_list_en, multiselect=True)
|
52 |
-
inp = gr.Textbox(label="Query", placeholder="How can I live a healthy life?; How can I improve my ability to focus?; What is the meaning of life?; ...")
|
53 |
btn = gr.Button("Search")
|
|
|
54 |
results = gr.State()
|
55 |
|
56 |
#def update_authors(database):
|
@@ -72,24 +148,43 @@ with gr.Blocks(css=".custom-markdown { border: 1px solid #ccc; padding: 10px; bo
|
|
72 |
for query, embedding in zip(queries, embeddings):
|
73 |
res = query_chroma(collection, embedding, authors)
|
74 |
results_data.append((query, res))
|
75 |
-
return results_data
|
76 |
|
77 |
btn.click(
|
|
|
|
|
|
|
|
|
|
|
78 |
perform_query,
|
79 |
inputs=[inp, author_inp],
|
80 |
-
outputs=[results]
|
81 |
)
|
82 |
|
83 |
@gr.render(inputs=[results])
|
84 |
def display_accordion(data):
|
85 |
for query, res in data:
|
86 |
-
with gr.Accordion(query, open=False) as acc:
|
87 |
for result in res:
|
88 |
with gr.Column():
|
89 |
-
author = result.get('author', '
|
90 |
-
book = result.get('book', '
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
gr.Markdown(value=markdown_contents, elem_classes="custom-markdown")
|
94 |
|
95 |
-
demo.launch()
|
|
|
20 |
def query_chroma(collection, embedding, authors):
|
21 |
results = collection.query(
|
22 |
query_embeddings=[embedding.tolist()],
|
23 |
+
n_results=20,
|
24 |
where={"author": {"$in": authors}} if authors else {},
|
25 |
include=["documents", "metadatas", "distances"]
|
26 |
)
|
|
|
34 |
for id_, metadata, document_text, distance in zip(ids, metadatas, documents, distances):
|
35 |
result_dict = {
|
36 |
"id": id_,
|
37 |
+
"author": metadata.get('author', ''),
|
38 |
+
"book": metadata.get('book', ''),
|
39 |
+
"section": metadata.get('section', ''),
|
40 |
+
"title": metadata.get('title', ''),
|
41 |
"text": document_text,
|
42 |
"distance": distance
|
43 |
}
|
|
|
45 |
|
46 |
return formatted_results
|
47 |
|
48 |
+
theme = gr.themes.Soft(
|
49 |
+
primary_hue="indigo",
|
50 |
+
secondary_hue="slate",
|
51 |
+
neutral_hue="slate",
|
52 |
+
spacing_size="lg",
|
53 |
+
radius_size="lg",
|
54 |
+
text_size="lg",
|
55 |
+
font=["Helvetica", "sans-serif"],
|
56 |
+
font_mono=["Courier", "monospace"],
|
57 |
+
).set(
|
58 |
+
body_text_color="*neutral_800",
|
59 |
+
block_background_fill="*neutral_50",
|
60 |
+
block_border_width="0px",
|
61 |
+
button_primary_background_fill="*primary_600",
|
62 |
+
button_primary_background_fill_hover="*primary_700",
|
63 |
+
button_primary_text_color="white",
|
64 |
+
input_background_fill="white",
|
65 |
+
input_border_color="*neutral_200",
|
66 |
+
input_border_width="1px",
|
67 |
+
checkbox_background_color_selected="*primary_600",
|
68 |
+
checkbox_border_color_selected="*primary_600",
|
69 |
+
)
|
70 |
+
|
71 |
+
custom_css = """
|
72 |
+
/* Remove outer padding, margins, and borders */
|
73 |
+
gradio-app,
|
74 |
+
gradio-app > div,
|
75 |
+
gradio-app .gradio-container {
|
76 |
+
padding: 0 !important;
|
77 |
+
margin: 0 !important;
|
78 |
+
border: none !important;
|
79 |
+
}
|
80 |
+
|
81 |
+
/* Remove any potential outlines */
|
82 |
+
gradio-app:focus,
|
83 |
+
gradio-app > div:focus,
|
84 |
+
gradio-app .gradio-container:focus {
|
85 |
+
outline: none !important;
|
86 |
+
}
|
87 |
+
|
88 |
+
/* Ensure full width */
|
89 |
+
gradio-app {
|
90 |
+
width: 100% !important;
|
91 |
+
display: block !important;
|
92 |
+
}
|
93 |
+
|
94 |
+
.custom-markdown {
|
95 |
+
border: 1px solid var(--neutral-200);
|
96 |
+
padding: 10px;
|
97 |
+
border-radius: var(--radius-lg);
|
98 |
+
background-color: var(--color-background-primary);
|
99 |
+
margin-bottom: 15px;
|
100 |
+
}
|
101 |
+
.custom-markdown p {
|
102 |
+
margin-bottom: 10px;
|
103 |
+
line-height: 1.6;
|
104 |
+
}
|
105 |
+
|
106 |
+
@media (max-width: 768px) {
|
107 |
+
gradio-app,
|
108 |
+
gradio-app > div,
|
109 |
+
gradio-app .gradio-container {
|
110 |
+
padding-left: 1px !important;
|
111 |
+
padding-right: 1px !important;
|
112 |
+
}
|
113 |
+
.custom-markdown {
|
114 |
+
padding: 5px;
|
115 |
+
}
|
116 |
+
.accordion {
|
117 |
+
margin-left: -10px;
|
118 |
+
margin-right: -10px;
|
119 |
+
}
|
120 |
+
}
|
121 |
+
"""
|
122 |
+
|
123 |
+
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
124 |
+
gr.Markdown("Enter one or more queries, divide them with semicola; filter authors (default is all), click **Search** to search.")
|
125 |
#database_inp = gr.Dropdown(label="Database", choices=["German", "English"], value="German")
|
126 |
author_inp = gr.Dropdown(label="Authors", choices=authors_list_en, multiselect=True)
|
127 |
+
inp = gr.Textbox(label="Query", lines=3, placeholder="How can I live a healthy life?; How can I improve my ability to focus?; What is the meaning of life?; ...")
|
128 |
btn = gr.Button("Search")
|
129 |
+
loading_indicator = gr.Markdown(visible=False, elem_id="loading-indicator")
|
130 |
results = gr.State()
|
131 |
|
132 |
#def update_authors(database):
|
|
|
148 |
for query, embedding in zip(queries, embeddings):
|
149 |
res = query_chroma(collection, embedding, authors)
|
150 |
results_data.append((query, res))
|
151 |
+
return results_data, ""
|
152 |
|
153 |
btn.click(
|
154 |
+
fn=lambda: ("", gr.update(visible=True)),
|
155 |
+
inputs=None,
|
156 |
+
outputs=[loading_indicator, loading_indicator],
|
157 |
+
queue=False
|
158 |
+
).then(
|
159 |
perform_query,
|
160 |
inputs=[inp, author_inp],
|
161 |
+
outputs=[results, loading_indicator]
|
162 |
)
|
163 |
|
164 |
@gr.render(inputs=[results])
|
165 |
def display_accordion(data):
|
166 |
for query, res in data:
|
167 |
+
with gr.Accordion(query, open=False, elem_classes="accordion") as acc:
|
168 |
for result in res:
|
169 |
with gr.Column():
|
170 |
+
author = str(result.get('author', ''))
|
171 |
+
book = str(result.get('book', ''))
|
172 |
+
section = str(result.get('section', ''))
|
173 |
+
title = str(result.get('title', ''))
|
174 |
+
text = str(result.get('text', ''))
|
175 |
+
|
176 |
+
header_parts = []
|
177 |
+
if author and author != "Unknown":
|
178 |
+
header_parts.append(author)
|
179 |
+
if book and book != "Unknown":
|
180 |
+
header_parts.append(book)
|
181 |
+
if section and section != "Unknown":
|
182 |
+
header_parts.append(section)
|
183 |
+
if title and title != "Unknown":
|
184 |
+
header_parts.append(title)
|
185 |
+
|
186 |
+
header = ", ".join(header_parts)
|
187 |
+
markdown_contents = f"**{header}**\n\n{text}"
|
188 |
gr.Markdown(value=markdown_contents, elem_classes="custom-markdown")
|
189 |
|
190 |
+
demo.launch(inline=False)
|