Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -156,19 +156,46 @@ if __name__ == "__main__":
|
|
| 156 |
categories = sorted(set(Path(p).parts[0] for p in glob.glob('*/*.txt')) | set(recommended.keys()) - {"all"})
|
| 157 |
|
| 158 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
selected_category = gr.State("all")
|
|
|
|
|
|
|
| 160 |
with gr.Row():
|
| 161 |
banner_path = Path(__file__).resolve().parent / "bannerhr.png"
|
| 162 |
banner_value = str(banner_path) if banner_path.exists() else None
|
| 163 |
-
gr.Image(value=banner_value, show_label=False, show_download_button=False)
|
| 164 |
-
|
| 165 |
|
|
|
|
| 166 |
with gr.Row():
|
| 167 |
category_buttons = []
|
| 168 |
for cat in ["all"] + categories:
|
| 169 |
-
btn = gr.Button(cat)
|
| 170 |
category_buttons.append(btn)
|
| 171 |
|
|
|
|
| 172 |
with gr.Row():
|
| 173 |
with gr.Column():
|
| 174 |
examples = gr.Dropdown(
|
|
@@ -177,18 +204,25 @@ if __name__ == "__main__":
|
|
| 177 |
value=recommended["all"][0]
|
| 178 |
)
|
| 179 |
with gr.Column():
|
| 180 |
-
query = gr.Textbox(label="Ask a question")
|
| 181 |
submit = gr.Button("Submit")
|
| 182 |
answer = gr.Textbox(label="Answer")
|
| 183 |
|
|
|
|
| 184 |
def set_category(cat):
|
| 185 |
-
return cat, gr.update(
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
for btn in category_buttons:
|
| 188 |
-
btn.click(lambda _, cat=btn.value: set_category(cat),
|
|
|
|
| 189 |
|
|
|
|
| 190 |
examples.change(lambda q: q, inputs=examples, outputs=query)
|
| 191 |
|
|
|
|
| 192 |
submit.click(lambda q, cat: answer_question(q, None if cat == "all" else cat),
|
| 193 |
inputs=[query, selected_category], outputs=answer)
|
| 194 |
|
|
|
|
| 156 |
categories = sorted(set(Path(p).parts[0] for p in glob.glob('*/*.txt')) | set(recommended.keys()) - {"all"})
|
| 157 |
|
| 158 |
with gr.Blocks() as demo:
|
| 159 |
+
# Custom CSS for banner and buttons
|
| 160 |
+
gr.HTML("""
|
| 161 |
+
<style>
|
| 162 |
+
#banner-img img {
|
| 163 |
+
width: 100%;
|
| 164 |
+
height: auto;
|
| 165 |
+
border-radius: 10px;
|
| 166 |
+
}
|
| 167 |
+
.category-button button {
|
| 168 |
+
background: linear-gradient(to right, #36d1dc, #5b86e5);
|
| 169 |
+
color: white;
|
| 170 |
+
border: none;
|
| 171 |
+
border-radius: 8px;
|
| 172 |
+
padding: 8px 16px;
|
| 173 |
+
font-weight: bold;
|
| 174 |
+
transition: 0.3s;
|
| 175 |
+
}
|
| 176 |
+
.category-button button:hover {
|
| 177 |
+
background: linear-gradient(to right, #5b86e5, #36d1dc);
|
| 178 |
+
transform: scale(1.05);
|
| 179 |
+
}
|
| 180 |
+
</style>
|
| 181 |
+
""")
|
| 182 |
+
|
| 183 |
selected_category = gr.State("all")
|
| 184 |
+
|
| 185 |
+
# β
Banner Image
|
| 186 |
with gr.Row():
|
| 187 |
banner_path = Path(__file__).resolve().parent / "bannerhr.png"
|
| 188 |
banner_value = str(banner_path) if banner_path.exists() else None
|
| 189 |
+
gr.Image(value=banner_value, show_label=False, show_download_button=False, elem_id="banner-img")
|
|
|
|
| 190 |
|
| 191 |
+
# β
Category Buttons with color
|
| 192 |
with gr.Row():
|
| 193 |
category_buttons = []
|
| 194 |
for cat in ["all"] + categories:
|
| 195 |
+
btn = gr.Button(cat, elem_classes=["category-button"])
|
| 196 |
category_buttons.append(btn)
|
| 197 |
|
| 198 |
+
# β
Question selection and input
|
| 199 |
with gr.Row():
|
| 200 |
with gr.Column():
|
| 201 |
examples = gr.Dropdown(
|
|
|
|
| 204 |
value=recommended["all"][0]
|
| 205 |
)
|
| 206 |
with gr.Column():
|
| 207 |
+
query = gr.Textbox(label="Ask a question", value=recommended["all"][0]) # β
Pre-filled to avoid error
|
| 208 |
submit = gr.Button("Submit")
|
| 209 |
answer = gr.Textbox(label="Answer")
|
| 210 |
|
| 211 |
+
# β
Category change logic
|
| 212 |
def set_category(cat):
|
| 213 |
+
return cat, gr.update(
|
| 214 |
+
choices=recommended.get(cat, recommended["all"]),
|
| 215 |
+
value=recommended.get(cat, recommended["all"])[0]
|
| 216 |
+
)
|
| 217 |
|
| 218 |
for btn in category_buttons:
|
| 219 |
+
btn.click(lambda _, cat=btn.value: set_category(cat),
|
| 220 |
+
inputs=None, outputs=[selected_category, examples])
|
| 221 |
|
| 222 |
+
# β
Sync dropdown to textbox
|
| 223 |
examples.change(lambda q: q, inputs=examples, outputs=query)
|
| 224 |
|
| 225 |
+
# β
Submit logic
|
| 226 |
submit.click(lambda q, cat: answer_question(q, None if cat == "all" else cat),
|
| 227 |
inputs=[query, selected_category], outputs=answer)
|
| 228 |
|