Gungz/quiz-generator-baidu-ernie
0
๐ 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).
โจ 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:
- Go to Baidu AI Studio.
- Navigate to the API section to get your
Access Token.
๐ฆ Installation
- Clone the repository (or download
app.py):
git clone <your-repo-url>
cd quiz-generator-baidu-ernie- Install dependencies:
pip install gradio openai pypdf pandas requests beautifulsoup4- Set Environment Variable: You must set your Baidu Access Token as an environment variable.
Linux/Mac:
export BAIDU_ACCESS_TOKEN="your_baidu_access_token_here"Windows (CMD):
set BAIDU_ACCESS_TOKEN="your_baidu_access_token_here"Windows (PowerShell):
$env:BAIDU_ACCESS_TOKEN="your_baidu_access_token_here"๐ Usage
Run the application using the following command:
python app.pyOnce the server starts, open the local URL provided in the terminal (usually http://127.0.0.1:7860).
How to Use the Interface
- Select Source Type: Choose between "File Upload" (PDF/TXT) or "URL".
- Upload/Paste:
- If using File Upload: Drag and drop your document.
- If using URL: Paste the website link.
- Configure Quiz:
- Number of Questions: Use the slider (1-20).
- Difficulty: Select Easy, Medium, or Hard.
- Generate: Click "โจ Generate Quiz".
- Review & Download:
- View the quiz in the Markdown preview pane.
- Click "๐ฅ CSV" or "๐ฅ Text" to download the results.
& Configuration
You can modify the following constants at the top of app.py:
๐ง Technical Details
- LLM Integration: The code uses the
OpenAIPython 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
RequestsandBeautifulSoupto strip navigation/scripts for clean text. - Output Structure: The LLM is prompted to return strict JSON formatted as:
[
{
"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_TOKENis 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.
