mad-professor

repository·main·Indexed 23 days ago

https://github.com/lyihub/mad-professor-public

A Python-based desktop application for researchers to analyze academic papers. It features a 'grumpy professor' AI persona, voice-enabled interaction, RAG-enhanced analysis, PDF-to-Markdown processing, and bilingual support. Built with PyQt6, it integrates DeepSeek for LLM and MiniMax for TTS, requiring Python 3.10+, CUDA support, and 6GB+ VRAM.

Tokens
2.5K
Snippets
8
Records
10
Agent score
82%

What's inside mad-professor

  1. Project Structure Overview

    main

    The project is organized into several functional modules:

    • Core Modules (核心模块/): Contains the main logic including AI_manager.py (AI orchestration), AI_professor_chat.py (chat logic), rag_retriever.py (RAG system), and TTS_manager.py (voice synthesis).
    • UI Components (ui/): PyQt6 components like chat_widget.py, markdown_view.py, and sidebar_widget.py.
    • Processors (processor/): Handles the data pipeline, including pdf_processor.py (PDF to MD), translate_processor.py (translation), and rag_processor.py (vector indexing).
    • Prompts (prompt/): Contains text files defining AI personalities and task-specific instructions (e.g., ai_explain_prompt.txt).
    • Data & Output: data/ stores source PDFs, and output/ stores processed results.
  2. Setup KaTeX with a starter template

    main

    To use KaTeX in a web project, you must use the HTML5 doctype. You need to include the KaTeX CSS and the KaTeX JavaScript files. For automatic rendering of math within text elements, include the auto-render extension. The following template demonstrates how to load KaTeX via CDN with deferred loading for better performance and an automatic render call on the document.body.

    <!DOCTYPE html>
    <!-- KaTeX requires the use of the HTML5 doctype. Without it, KaTeX may not render properly -->
    <html>
      <head>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.21/dist/katex.min.css" integrity="sha384-zh0CIslj+VczCZtlzBcjt5ppRcsAmDnRem7ESsYwWwg3m/OaJ2l4x7YBZl9Kxxib" crossorigin="anonymous">
    
        <!-- The loading of KaTeX is deferred to speed up page rendering -->
        <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.21/dist/katex.min.js" integrity="sha384-Rma6DA2IPUwhNxmrB/7S3Tno0YY7sFu9WSYMCuulLhIqYSGZ2gKCJWIqhBWqMQfh" crossorigin="anonymous"></script>
    
        <!-- To automatically render math in text elements, include the auto-render extension: -->
        <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.21/dist/contrib/auto-render.min.js" integrity="sha384-hCXGrW6PitJEwbkoStFjeJxv+fSOOQKOPbJxSfM6G5sWZjAyWhXiTIIAmQqnlLlh" crossorigin="anonymous"
            onload="renderMathInElement(document.body);"></script>
      </head>
      ...
    </html>
  3. Use the mad-professor application

    main

    Launch the application

    Run the following command from the project root:

    python main.py

    Importing Papers

    1. Click the "导入论文" (Import Paper) button in the sidebar.
    2. Select a PDF file.
    3. Click "继续" (Continue) and wait for processing (translation and indexing).
    4. Note: PDFs are stored in the data/ folder. Adding multiple PDFs to this folder allows for batch processing.

    Reading and Interaction

    • Reading: Select a processed paper from the sidebar. Use the toggle in the top-right corner to switch between Chinese and English views.
    • Voice Interaction: Select your input device in the chat window. Click the microphone button; wait for the indicator to turn green before speaking.
    • Tip: Use headphones to prevent the AI's voice from being re-recorded as user input.
    python main.py
  4. Install mad-professor

    main

    Follow these steps to set up the environment for the mad-professor application.

    Prerequisites

    • Python 3.10 or higher
    • CUDA support
    • 6GB+ VRAM

    Installation Steps

    1. Create and activate a conda environment:

      conda create -n mad-professor python=3.10.16
      conda activate mad-professor
    2. Install MinerU dependencies:

      pip install -U magic-pdf[full]==1.3.3 -i https://mirrors.aliyun.com/pypi/simple
    3. Install project requirements:

      pip install -r requirements.txt
    4. Install CUDA-compatible torch and numpy (Example for CUDA 12.4; adjust based on your hardware):

      pip install --force-reinstall torch torchvision torchaudio "numpy<=2.1.1" --index-url https://download.pytorch.org/whl/cu124
    5. Install FAISS GPU via conda:

      conda install -c conda-forge faiss-gpu
    6. Download models:

      python download_models.py

      This script downloads model files and configures the magic-pdf.json file located in your user directory (C:\Users\<username> on Windows or /home/<username> on Linux).

    conda create -n mad-professor python=3.10.16
    conda activate mad-professor
    pip install -U magic-pdf[full]==1.3.3 -i https://mirrors.aliyun.com/pypi/simple
    pip install -r requirements.txt
    pip install --force-reinstall torch torchvision torchaudio "numpy<=2.1.1" --index-url https://download.pytorch.org/whl/cu124
    conda install -c conda-forge faiss-gpu
    python download_models.py
  5. Customize the AI Professor's personality and voice

    main

    Modify AI Personality (Prompt)

    1. Create a new text file in the prompt/ directory named ai_character_prompt_[your_name].txt.
    2. In AI_professor_chat.py, update the AI_CHARACTER_PROMPT_PATH variable to point to your new file:
    AI_CHARACTER_PROMPT_PATH = "prompt/ai_character_prompt_[your_name].txt"

    Modify AI Voice

    To change the voice, you must use a valid voice_id from MiniMax. Update the voice_id parameter within the build_tts_stream_body method in the TTSManager class inside TTS_manager.py:

    body = json.dumps({
        "model": "speech-02-turbo",
        "text": text,
        "stream": True,
        "voice_setting": {
            "voice_id": "YOUR_NEW_VOICE_ID",
            "speed": 1,
            "vol": 1,
            "pitch": 0,
            "emotion": mapped_emotion
        },
        "audio_setting": {
            "sample_rate": 32000,
            "bitrate": 128000,
            "format": "pcm",
            "channel": 1
        }
    })
    AI_CHARACTER_PROMPT_PATH = "prompt/ai_character_prompt_[your_name].txt"
    
    # In TTS_manager.py
    "voice_setting": {
        "voice_id": "YOUR_NEW_VOICE_ID",
        "speed": 1,
        "vol": 1,
        "pitch": 0,
        "emotion": mapped_emotion
    }
  6. Configure API keys and device mode

    main

    Enable CUDA

    To enable CUDA support, locate the magic-pdf.json file in your user directory and set device-mode to "cuda":

    {
        "device-mode":"cuda"
    }

    Configure LLM and TTS APIs

    Edit config.py to provide your API credentials for DeepSeek (LLM) and MiniMax (TTS):

    # DeepSeek Configuration
    API_BASE_URL = "YOUR_API_URL"
    API_KEY = "YOUR_API_KEY"   
    
    # MiniMax Configuration
    TTS_GROUP_ID = "YOUR_MINIMAX_GROUP_ID"
    TTS_API_KEY = "YOUR_MINIMAX_API_KEY"
    {
        "device-mode":"cuda"
    }
    
    # In config.py
    API_BASE_URL = "YOUR_API_URL"
    API_KEY = "YOUR_API_KEY"   
    TTS_GROUP_ID = "YOUR_MINIMAX_GROUP_ID"
    TTS_API_KEY = "YOUR_MINIMAX_API_KEY"
  7. Launch the Mad Professor application

    main

    The application is a PyQt6-based desktop application. To start the application, run the main.py script. The entrypoint initializes the QApplication with the 'Fusion' style, sets up custom fonts (Source Han Sans and Source Han Serif), configures a modern color palette, and launches the AIProfessorUI window.

    Note: On Windows, the application sets a specific AppUserModelID (ai.professor.paperassistant.1.0) to ensure the taskbar icon is handled correctly.

  8. Render math using katex.render()

    main

    Use katex.render to render a TeX expression directly into a specific DOM element.

    Note: Ensure you have included the KaTeX CSS and font files in your project. You can use the throwOnError: false option to render invalid TeX as the original source code in red with the error message provided as hover text.

    katex.render("c = \\pm\\sqrt{a^2 + b^2}", element, {
        throwOnError: false
    });
  9. Generate math HTML strings with katex.renderToString()

    main

    Use katex.renderToString to generate an HTML string of the rendered math. This is primarily used for server-side rendering (SSR) where you want to pre-render expressions using Node.js and send them as plain HTML to the client.

    Note: If you are performing all rendering on the server, you do not need to include the KaTeX JavaScript on the client side, but you must still include the CSS.

    var html = katex.renderToString("c = \\pm\\sqrt{a^2 + b^2}", {
        throwOnError: false
    });
    // '<span class="katex">...</span>'
  10. Generate the application icon with generate_app_icon()

    main

    The generate_app_icon() function programmatically creates a QIcon using QPainter. The icon features a circular gradient background (deep blue/indigo), a book pattern with white pages and spine lines, and a simplified "AI" text overlay in the center.

    from main import generate_app_icon
    from PyQt6.QtWidgets import QApplication
    import sys
    
    app = QApplication(sys.argv)
    app_icon = generate_app_icon()
    app.setWindowIcon(app_icon)