CoolFace
Apppublic

Gungz/quiz-generator-baidu-ernie

sourceHugging Facemitupdated 9mo agoView on Hugging Face
0likes
App README

๐ŸŽ“ Baidu ERNIE AI Quiz Generator

An intelligent, AI-powered tool that automatically generates multiple-choice quizzes from text content. Built with Gradio and powered by Baidu's ERNIE-5.0-Thinking-Preview model (accessed via OpenAI-compatible API).

Python Gradio Baidu ERNIE

โœจ Features

  • โ€”๐Ÿ“„ Multi-Format Input: Extract text from PDF, TXT files, or live URLs.
  • โ€”๐Ÿง  AI-Powered Generation: Uses Baidu's advanced ERNIE 5.0 model to generate context-aware questions.
  • โ€”๐ŸŽฏ Adjustable Difficulty: Select between Easy, Medium, and Hard levels.
  • โ€”๐Ÿ“Š Customizable Length: Generate between 1 to 20 questions per session.
  • โ€”๐Ÿ’พ Export Options: Download generated quizzes as CSV (for Excel/LMS) or TXT (for printing).
  • โ€”๐ŸŽจ Interactive UI: Built with Gradio for a smooth, real-time user experience with progress tracking.

๐Ÿ› ๏ธ Prerequisites

Before running the application, ensure you have Python 3.8+ installed.

Required API Key

This project uses the Baidu AI Studio API. You must have a Baidu account and create an API key:

  1. 1.Go to Baidu AI Studio.
  2. 2.Navigate to the API section to get your Access Token.

๐Ÿ“ฆ Installation

  1. 1.Clone the repository (or download app.py):
bash
   git clone <your-repo-url>
   cd quiz-generator-baidu-ernie
  1. 1.Install dependencies:
bash
   pip install gradio openai pypdf pandas requests beautifulsoup4
  1. 1.Set Environment Variable: You must set your Baidu Access Token as an environment variable.

Linux/Mac:

bash
   export BAIDU_ACCESS_TOKEN="your_baidu_access_token_here"

Windows (CMD):

cmd
   set BAIDU_ACCESS_TOKEN="your_baidu_access_token_here"

Windows (PowerShell):

powershell
   $env:BAIDU_ACCESS_TOKEN="your_baidu_access_token_here"

๐Ÿš€ Usage

Run the application using the following command:

bash
python app.py

Once the server starts, open the local URL provided in the terminal (usually http://127.0.0.1:7860).

How to Use the Interface

  1. 1.Select Source Type: Choose between "File Upload" (PDF/TXT) or "URL".
  2. 2.Upload/Paste:
  3. 3.If using File Upload: Drag and drop your document.
  4. 4.If using URL: Paste the website link.
  5. 5.Configure Quiz:
  6. 6.Number of Questions: Use the slider (1-20).
  7. 7.Difficulty: Select Easy, Medium, or Hard.
  8. 8.Generate: Click "โœจ Generate Quiz".
  9. 9.Review & Download:
  10. 10.View the quiz in the Markdown preview pane.
  11. 11.Click "๐Ÿ“ฅ CSV" or "๐Ÿ“ฅ Text" to download the results.

& Configuration

You can modify the following constants at the top of app.py:

ConstantDescriptionDefault
BASE_URLThe API endpoint for Baidu AI Studiohttps://aistudio.baidu.com/llm/lmapi/v3
MODEL_NAMEThe specific model version to useernie-5.0-thinking-preview

๐Ÿง  Technical Details

  • โ€”LLM Integration: The code uses the OpenAI Python client library configured to point to Baidu's API endpoint, making it compatible with standard OpenAI SDK patterns while using Baidu's infrastructure.
  • โ€”Text Extraction:
  • โ€”PDFs: Uses PyPDF2.
  • โ€”Web Scraping: Uses Requests and BeautifulSoup to strip navigation/scripts for clean text.
  • โ€”Output Structure: The LLM is prompted to return strict JSON formatted as:
json
  [
    {
      "question": "...",
      "options": ["A) ...", "B) ...", "C) ...", "D) ..."],
      "answer": "A",
      "explanation": "..."
    }
  ]

๐Ÿ“ Project Structure

.
โ”œโ”€โ”€ app.py              # Main application logic and Gradio UI
โ”œโ”€โ”€ requirements.txt    # Python dependencies
โ””โ”€โ”€ README.md           # This file

โš ๏ธ Troubleshooting

  • โ€”Error reading PDF: Ensure the PDF is text-based (not a scanned image) and not encrypted.
  • โ€”Authentication Error: Check if BAIDU_ACCESS_TOKEN is set correctly and has not expired.
  • โ€”Empty Text from URL: Some websites block scrapers. The tool uses a standard User-Agent, but complex JavaScript-heavy sites may not render fully.

๐Ÿ“ License

This project is open-source and available for educational purposes.