Spaces:
Running
Running
ok
Browse files- paper_chat_tab.py +90 -15
paper_chat_tab.py
CHANGED
|
@@ -257,7 +257,7 @@ def create_chat_interface(provider_dropdown, model_dropdown, paper_content, hf_t
|
|
| 257 |
except Exception as ex:
|
| 258 |
yield f"Unexpected error: {ex}"
|
| 259 |
|
| 260 |
-
chatbot = gr.Chatbot(label="Chatbot", scale=1, height=
|
| 261 |
chat_interface = gr.ChatInterface(
|
| 262 |
fn=get_fn,
|
| 263 |
chatbot=chatbot,
|
|
@@ -268,13 +268,15 @@ def create_chat_interface(provider_dropdown, model_dropdown, paper_content, hf_t
|
|
| 268 |
|
| 269 |
|
| 270 |
def paper_chat_tab(paper_id, paper_from, paper_central_df):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
# First row with two columns
|
| 272 |
with gr.Row():
|
| 273 |
# Left column: Paper selection and display
|
| 274 |
with gr.Column(scale=1):
|
| 275 |
-
gr.Markdown("### Select a Paper")
|
| 276 |
todays_date = datetime.today().strftime('%Y-%m-%d')
|
| 277 |
-
|
| 278 |
# Filter papers for today's date and having a paper_page
|
| 279 |
selectable_papers = paper_central_df.df_prettified
|
| 280 |
selectable_papers = selectable_papers[
|
|
@@ -290,10 +292,17 @@ def paper_chat_tab(paper_id, paper_from, paper_central_df):
|
|
| 290 |
paper_choices = [("No available papers for today", "")]
|
| 291 |
|
| 292 |
paper_select = gr.Dropdown(
|
| 293 |
-
label="Select a paper to chat with:",
|
| 294 |
choices=[p[0] for p in paper_choices],
|
| 295 |
value=paper_choices[0][0] if paper_choices else None
|
| 296 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
select_paper_button = gr.Button("Load this paper")
|
| 298 |
|
| 299 |
# Paper info display
|
|
@@ -345,7 +354,6 @@ def paper_chat_tab(paper_id, paper_from, paper_central_df):
|
|
| 345 |
provider_info = PROVIDERS[selected_provider]
|
| 346 |
models = provider_info['models']
|
| 347 |
logo_url = provider_info['logo']
|
| 348 |
-
chatbot_message_type = provider_info['type']
|
| 349 |
max_total_tokens = provider_info['max_total_tokens']
|
| 350 |
|
| 351 |
model_dropdown_choices = gr.update(choices=models, value=models[0])
|
|
@@ -357,7 +365,8 @@ def paper_chat_tab(paper_id, paper_from, paper_central_df):
|
|
| 357 |
placeholder=f"Enter your {selected_provider} API token to avoid rate limits"
|
| 358 |
)
|
| 359 |
chatbot_reset = []
|
| 360 |
-
return model_dropdown_choices, logo_html_update, note_markdown_update, hf_token_input_update,
|
|
|
|
| 361 |
|
| 362 |
provider_dropdown.change(
|
| 363 |
fn=update_provider,
|
|
@@ -394,19 +403,44 @@ def paper_chat_tab(paper_id, paper_from, paper_central_df):
|
|
| 394 |
|
| 395 |
return gr.update(value=card_html), text, []
|
| 396 |
|
| 397 |
-
def select_paper(paper_title):
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
|
| 403 |
select_paper_button.click(
|
| 404 |
fn=select_paper,
|
| 405 |
-
inputs=[paper_select],
|
| 406 |
outputs=[paper_id, paper_from]
|
| 407 |
)
|
| 408 |
|
| 409 |
-
paper_id
|
|
|
|
| 410 |
fn=update_paper_info,
|
| 411 |
inputs=[paper_id, paper_from, model_dropdown, content],
|
| 412 |
outputs=[content, paper_content, chatbot]
|
|
@@ -418,20 +452,60 @@ def paper_chat_tab(paper_id, paper_from, paper_central_df):
|
|
| 418 |
else:
|
| 419 |
return gr.update(visible=False)
|
| 420 |
|
| 421 |
-
# Toggle provider section visibility
|
| 422 |
paper_id.change(
|
| 423 |
fn=toggle_provider_visibility,
|
| 424 |
inputs=[paper_id],
|
| 425 |
outputs=[provider_section]
|
| 426 |
)
|
| 427 |
|
| 428 |
-
# Toggle chat row visibility
|
| 429 |
paper_id.change(
|
| 430 |
fn=toggle_provider_visibility,
|
| 431 |
inputs=[paper_id],
|
| 432 |
outputs=[chat_row]
|
| 433 |
)
|
| 434 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
|
| 436 |
def main():
|
| 437 |
with gr.Blocks(css_paths="style.css") as demo:
|
|
@@ -448,6 +522,7 @@ def main():
|
|
| 448 |
data = {
|
| 449 |
'date': [datetime.today().strftime('%Y-%m-%d')],
|
| 450 |
'paper_page': ['1234.56789'],
|
|
|
|
| 451 |
'title': ['An Example Paper']
|
| 452 |
}
|
| 453 |
self.df_prettified = pd.DataFrame(data)
|
|
|
|
| 257 |
except Exception as ex:
|
| 258 |
yield f"Unexpected error: {ex}"
|
| 259 |
|
| 260 |
+
chatbot = gr.Chatbot(label="Chatbot", scale=1, height=800, autoscroll=True)
|
| 261 |
chat_interface = gr.ChatInterface(
|
| 262 |
fn=get_fn,
|
| 263 |
chatbot=chatbot,
|
|
|
|
| 268 |
|
| 269 |
|
| 270 |
def paper_chat_tab(paper_id, paper_from, paper_central_df):
|
| 271 |
+
# A top-level button to "Chat with another paper" (visible only if paper_id is set)
|
| 272 |
+
# We'll place it above everything
|
| 273 |
+
chat_another_button = gr.Button("Chat with another paper", variant="primary", visible=False)
|
| 274 |
+
|
| 275 |
# First row with two columns
|
| 276 |
with gr.Row():
|
| 277 |
# Left column: Paper selection and display
|
| 278 |
with gr.Column(scale=1):
|
|
|
|
| 279 |
todays_date = datetime.today().strftime('%Y-%m-%d')
|
|
|
|
| 280 |
# Filter papers for today's date and having a paper_page
|
| 281 |
selectable_papers = paper_central_df.df_prettified
|
| 282 |
selectable_papers = selectable_papers[
|
|
|
|
| 292 |
paper_choices = [("No available papers for today", "")]
|
| 293 |
|
| 294 |
paper_select = gr.Dropdown(
|
| 295 |
+
label="Select a paper to chat with: (from today's 🤗 hugging face paper page)",
|
| 296 |
choices=[p[0] for p in paper_choices],
|
| 297 |
value=paper_choices[0][0] if paper_choices else None
|
| 298 |
)
|
| 299 |
+
|
| 300 |
+
# Add a textbox for user to enter a paper_id (arxiv_id)
|
| 301 |
+
paper_id_input = gr.Textbox(
|
| 302 |
+
label="Or enter a 🤗 paper_id (arxiv_id) directly",
|
| 303 |
+
placeholder="e.g. 1234.56789"
|
| 304 |
+
)
|
| 305 |
+
|
| 306 |
select_paper_button = gr.Button("Load this paper")
|
| 307 |
|
| 308 |
# Paper info display
|
|
|
|
| 354 |
provider_info = PROVIDERS[selected_provider]
|
| 355 |
models = provider_info['models']
|
| 356 |
logo_url = provider_info['logo']
|
|
|
|
| 357 |
max_total_tokens = provider_info['max_total_tokens']
|
| 358 |
|
| 359 |
model_dropdown_choices = gr.update(choices=models, value=models[0])
|
|
|
|
| 365 |
placeholder=f"Enter your {selected_provider} API token to avoid rate limits"
|
| 366 |
)
|
| 367 |
chatbot_reset = []
|
| 368 |
+
return model_dropdown_choices, logo_html_update, note_markdown_update, hf_token_input_update, provider_info[
|
| 369 |
+
'type'], max_total_tokens, chatbot_reset
|
| 370 |
|
| 371 |
provider_dropdown.change(
|
| 372 |
fn=update_provider,
|
|
|
|
| 403 |
|
| 404 |
return gr.update(value=card_html), text, []
|
| 405 |
|
| 406 |
+
def select_paper(paper_title, paper_id_val):
|
| 407 |
+
# If user provided a paper_id_val (arxiv_id), use that
|
| 408 |
+
if paper_id_val and paper_id_val.strip():
|
| 409 |
+
# Check if it exists in df as a paper with paper_page not None
|
| 410 |
+
df = paper_central_df.df_raw
|
| 411 |
+
# We assume `arxiv_id` column exists in df (the user requested checking arxiv_id)
|
| 412 |
+
# If not present, you must ensure `paper_central_df` has `arxiv_id` column.
|
| 413 |
+
if 'arxiv_id' not in df.columns:
|
| 414 |
+
return gr.update(value="<div>arxiv_id column not found in dataset</div>"), None
|
| 415 |
+
|
| 416 |
+
found = df[
|
| 417 |
+
(df['arxiv_id'] == paper_id_val.strip()) &
|
| 418 |
+
df['paper_page'].notna() & (df['paper_page'] != "")
|
| 419 |
+
]
|
| 420 |
+
|
| 421 |
+
if len(found) > 0:
|
| 422 |
+
# We found a matching paper
|
| 423 |
+
return paper_id_val.strip(), "paper_page"
|
| 424 |
+
else:
|
| 425 |
+
# Not found, show error in content
|
| 426 |
+
# We can't directly show error from here. We'll return something that doesn't update states and rely on error message
|
| 427 |
+
# Let's return empty paper_id and paper_from but we must also show error in content after this call
|
| 428 |
+
return "", ""
|
| 429 |
+
else:
|
| 430 |
+
# fallback to dropdown selection
|
| 431 |
+
for t, ppage in paper_choices:
|
| 432 |
+
if t == paper_title:
|
| 433 |
+
return ppage, "paper_page"
|
| 434 |
+
return "", ""
|
| 435 |
|
| 436 |
select_paper_button.click(
|
| 437 |
fn=select_paper,
|
| 438 |
+
inputs=[paper_select, paper_id_input],
|
| 439 |
outputs=[paper_id, paper_from]
|
| 440 |
)
|
| 441 |
|
| 442 |
+
# After the paper_id/paper_from are set, we update paper info
|
| 443 |
+
paper_id_update = paper_id.change(
|
| 444 |
fn=update_paper_info,
|
| 445 |
inputs=[paper_id, paper_from, model_dropdown, content],
|
| 446 |
outputs=[content, paper_content, chatbot]
|
|
|
|
| 452 |
else:
|
| 453 |
return gr.update(visible=False)
|
| 454 |
|
|
|
|
| 455 |
paper_id.change(
|
| 456 |
fn=toggle_provider_visibility,
|
| 457 |
inputs=[paper_id],
|
| 458 |
outputs=[provider_section]
|
| 459 |
)
|
| 460 |
|
|
|
|
| 461 |
paper_id.change(
|
| 462 |
fn=toggle_provider_visibility,
|
| 463 |
inputs=[paper_id],
|
| 464 |
outputs=[chat_row]
|
| 465 |
)
|
| 466 |
|
| 467 |
+
# Show/hide the "Chat with another paper" button
|
| 468 |
+
# If paper_id is set, show it. If not, hide it.
|
| 469 |
+
def toggle_chat_another_button(paper_id_value):
|
| 470 |
+
if paper_id_value and paper_id_value.strip():
|
| 471 |
+
return gr.update(visible=True)
|
| 472 |
+
else:
|
| 473 |
+
return gr.update(visible=False)
|
| 474 |
+
|
| 475 |
+
paper_id.change(
|
| 476 |
+
fn=toggle_chat_another_button,
|
| 477 |
+
inputs=[paper_id],
|
| 478 |
+
outputs=[chat_another_button]
|
| 479 |
+
)
|
| 480 |
+
|
| 481 |
+
# Button action to reset paper_id to None
|
| 482 |
+
def reset_paper_id():
|
| 483 |
+
# reset paper_id to ""
|
| 484 |
+
return "", "neurips", gr.update(value="<div></div>")
|
| 485 |
+
|
| 486 |
+
# When this button is clicked, we reset the paper_id and content
|
| 487 |
+
chat_another_button.click(
|
| 488 |
+
fn=reset_paper_id,
|
| 489 |
+
outputs=[paper_id, paper_from, content]
|
| 490 |
+
)
|
| 491 |
+
|
| 492 |
+
# If user tried an invalid paper_id_input, no error was shown yet:
|
| 493 |
+
# Actually we can show error message if no paper selected by updating after select_paper_button
|
| 494 |
+
# The select_paper returns paper_id/paper_from. If empty means error:
|
| 495 |
+
def check_paper_id_error(p_id, p_from):
|
| 496 |
+
# If p_id is empty after clicking load, show error message
|
| 497 |
+
if not p_id:
|
| 498 |
+
return gr.update(value="<div style='color:red;'>No valid paper found for the given input.</div>")
|
| 499 |
+
else:
|
| 500 |
+
return gr.update()
|
| 501 |
+
|
| 502 |
+
select_paper_button.click(
|
| 503 |
+
fn=check_paper_id_error,
|
| 504 |
+
inputs=[paper_id, paper_from],
|
| 505 |
+
outputs=[content],
|
| 506 |
+
queue=False
|
| 507 |
+
)
|
| 508 |
+
|
| 509 |
|
| 510 |
def main():
|
| 511 |
with gr.Blocks(css_paths="style.css") as demo:
|
|
|
|
| 522 |
data = {
|
| 523 |
'date': [datetime.today().strftime('%Y-%m-%d')],
|
| 524 |
'paper_page': ['1234.56789'],
|
| 525 |
+
'arxiv_id': ['1234.56789'], # adding arxiv_id column as user requested
|
| 526 |
'title': ['An Example Paper']
|
| 527 |
}
|
| 528 |
self.df_prettified = pd.DataFrame(data)
|