How to Add Custom Shortcuts/Buttons to the GPT-Academic Interface

You can add custom shortcuts to GPT-Academic either by increasing the placeholder count in config.py or by implementing dedicated callback functions in main.py and core_functional.py.

GPT-Academic is an open-source academic paper processing tool built with Gradio. Adding custom shortcuts/buttons to the GPT-Academic interface allows you to streamline repetitive tasks by creating one-click prompts or specialized functions that pre-process user input before sending it to the LLM pipeline.

Understanding the GPT-Academic UI Architecture

The interface is divided into three logical layers that handle button creation, user customization, and event persistence:

Layer Purpose Main Source File
Core UI layout Defines panels, basic controls, and custom button placeholders main.py (lines 96‑136)
Custom-button editor Floating panel for renaming buttons and setting prompt prefix/suffix themes/gui_floating_menu.py (lines 20‑42)
Persistence & click handling Stores configuration in cookies and wires clicks to the predict pipeline shared_utils/cookie_manager.pyassign_btn__fn_builder (lines 28‑52)

By default, the program creates 4 hidden placeholders labeled “自定义按钮 1‑4”. They become visible when the user checks “自定义菜单” in the toolbar (see themes/gui_toolbar.py, line 28).

Method 1: Increase the Number of Custom Button Placeholders (No Code Change)

The simplest way to add more shortcuts is to increase the global placeholder count. This requires no changes to the logic—only a configuration update.

  1. Open config.py.
  2. Locate the constant NUM_CUSTOM_BASIC_BTN (default = 4) and change it to your desired count:

# config.py (line 46)

NUM_CUSTOM_BASIC_BTN = 8   # Creates 8 hidden custom buttons
  1. Restart the server (python main.py). The UI will now display up to 8 placeholders in the 自定义按钮 dropdown inside the floating menu.

Note: The floating menu (themes/gui_floating_menu.py) automatically builds the dropdown from the keys of the customize_btns dictionary (line 24). No further code changes are needed for the UI to recognize the additional placeholders.

Method 2: Add a Fully Custom Button with a Dedicated Callback

If you need a button that executes specific logic (e.g., “Generate Chart” that automatically wraps user input in a Mermaid diagram prompt), you must modify the source code.

Step 1: Create the Button Placeholder in main.py

Inside the 基础功能区 construction block (around lines 131‑137), instantiate a gr.Button and register it in the customize_btns dictionary:


# main.py – inside the 基础功能区 loop

my_btn = gr.Button("生成图表", variant="secondary", info_str='基础功能区: 生成图表')
my_btn.style(size="sm")
customize_btns["生成图表"] = my_btn

Step 2: Define the Callback Function

Create a function that accepts the same arguments as the standard predict pipeline and returns the standard outputs (chatbot, history, status). The recommended location is core_functional.py:


# core_functional.py

def generate_chart(request, *inputs):
    """
    Custom callback that prepends a Mermaid diagram instruction.
    inputs[3] corresponds to the primary user input textbox.
    """
    user_txt = inputs[3]
    extra_prompt = "请把以下文字转换成 mermaid 流程图:\n" + user_txt
    # Re-use the generic wrapper to call the LLM pipeline

    return ArgsGeneralWrapper(predict)(request, *inputs, extra_prompt=extra_prompt)

Step 3: Register the Click Handler

Wire the button to your callback using the same pattern as the built-in functional buttons (see main.py, lines 236‑242):


# main.py – after the button creation

click_handle = my_btn.click(fn=generate_chart,
                           inputs=[*input_combo, gr.State(True)],
                           outputs=output_combo)
cancel_handles.append(click_handle)  # Ensures the "停止" button can cancel this operation

Step 4: Persist Visibility Settings

If you want the button visible by default, ensure visible=True when creating it (as shown in Step 1). The button’s state will also be saved to the browser cookie when the user interacts with the “自定义菜单” toggle, thanks to the assign_btn__fn_builder function in shared_utils/cookie_manager.py (lines 28‑52). This function updates the web_cookie_cache with the button’s Title, Prefix, and Suffix.

How the Floating Menu Edits Button Properties

The floating menu (themes/gui_floating_menu.py) provides a no-code way to customize any placeholder button:

  1. Dropdown: Selects the target button from the customize_btns keys.
  2. Title field: Updates the button’s visible label.
  3. Prefix / Suffix fields: Define text automatically added before or after the user’s query when the button is clicked.
  4. Confirm button: Triggers assign_btn__fn_builder in shared_utils/cookie_manager.py, which serializes these values into the web_cookie_cache cookie.

This mechanism allows non-technical users to repurpose the placeholder buttons without touching Python code.

Summary

  • Increase placeholders: Edit NUM_CUSTOM_BASIC_BTN in config.py (line 46) to create more empty custom button slots.
  • Add dedicated buttons: Modify main.py to instantiate gr.Button, register it in customize_btns, and wire a click handler using ArgsGeneralWrapper(predict).
  • Implement logic: Place custom callback functions in core_functional.py to pre-process inputs or modify prompts before calling the LLM.
  • Persist settings: The floating menu (themes/gui_floating_menu.py) and cookie manager (shared_utils/cookie_manager.py) handle saving button labels and prompt prefixes without extra code.

Frequently Asked Questions

How many custom buttons can I add to GPT-Academic?

You can add as many as needed by increasing NUM_CUSTOM_BASIC_BTN in config.py. The default is 4, but you can set it to any integer (e.g., 8, 12, or 20). The UI will automatically generate the corresponding placeholders in the floating menu dropdown.

Where are custom button settings saved?

Settings are stored in the browser’s web_cookie_cache cookie via the assign_btn__fn_builder function in shared_utils/cookie_manager.py (lines 28‑52). This includes the button title, prefix, and suffix. The cookie is loaded on startup in main.py (lines 328‑330), restoring your configuration across sessions.

Can I add a custom button without modifying source code?

Yes, partially. You can repurpose the existing placeholder buttons (自定义按钮 1‑4) using the floating menu (“自定义菜单”) to change their labels and set prompt prefixes/suffixes. However, to add a button with completely custom logic (like calling a specific preprocessing function), you must modify main.py and create a callback function.

How do I make a custom button visible by default?

When creating the button in main.py, ensure you do not set visible=False. The button will appear immediately in the 基础功能区 panel. Additionally, if the user checks the “自定义菜单” checkbox in the toolbar (themes/gui_toolbar.py, line 28), the floating editor becomes visible, allowing further customization of the button’s behavior.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →