CoolFace
Apppublic

uxoxo/eb2ab

sourceHugging Faceapache-2.0updated 11mo agoView on Hugging Face
0likes
README_LLM_PROCESSOR.md378 linesDownload Raw Back to root
1# LLM Ebook Processor2 3**Transform ebooks with AI, then convert to audiobooks**4 5A complete pipeline for preprocessing ebooks with Large Language Models (LLMs) and converting the results to high-quality audiobooks.6 7---8 9## What It Does10 111. **Upload** an ebook (EPUB, PDF, TXT, HTML)122. **Configure** how to transform the text (e.g., "modernize Victorian English")133. **Estimate** costs and time before processing144. **Process** with AI (OpenAI GPT, Anthropic Claude, or local Ollama)155. **Convert** to audiobook with TTS166. **Download** your transformed ebook and audiobook17 18## Key Features19 20- ๐Ÿ“š **Multi-format Support** - EPUB, PDF, TXT, HTML21- ๐Ÿค– **Multiple LLM Providers** - OpenAI, Anthropic, Ollama22- ๐Ÿ’ฐ **Cost Transparency** - See exact costs before processing23- โฑ๏ธ **Time Estimates** - Know how long it will take24- ๐ŸŽ™๏ธ **TTS Integration** - Convert processed text to audiobook25- ๐Ÿ“Š **Live Progress** - Real-time updates during processing26- โœ… **Comprehensive Error Handling** - User-friendly error messages27- ๐Ÿ”„ **Smart Chunking** - 3 strategies for optimal results28 29## Use Cases30 31### Language Modernization32Transform Victorian-era novels to modern English:33```34Before: "It was the best of times, it was the worst of times..."35After:  "It was both the best and worst period in history..."36```37 38### Text Simplification39Make complex texts accessible:40```41Instruction: "Rewrite for a middle school reading level"42Result: Complex academic text โ†’ Easy-to-read version43```44 45### Translation46Translate while preserving tone:47```48Instruction: "Translate to Spanish, maintaining formality"49Result: English book โ†’ Spanish audiobook50```51 52### Tone Conversion53Change writing style:54```55Instruction: "Convert to casual, conversational tone"56Result: Formal text โ†’ Podcast-style audio57```58 59## Quick Start60 61### 1. Install62```bash63pip install -r requirements.txt64```65 66### 2. Set API Key67```bash68export ANTHROPIC_API_KEY=your-key-here69# or70export OPENAI_API_KEY=your-key-here71```72 73### 3. Launch74```bash75python app_llm_processor.py76```77 78Visit: http://localhost:786179 80See [QUICKSTART_LLM_PROCESSOR.md](QUICKSTART_LLM_PROCESSOR.md) for detailed usage.81 82## Cost Examples83 84### 200-Page Novel85- **LLM**: $0.15 - $2.50 (depending on provider)86- **TTS**: ~$12.5087- **Total**: ~$13-1588- **Time**: 30-40 minutes89- **Output**: ~8 hours of audio90 91### 50-Page Book92- **LLM**: $0.05 - $0.2593- **TTS**: ~$3.0094- **Total**: ~$3-3.2595- **Time**: 8-12 minutes96- **Output**: ~2 hours of audio97 98## Providers Comparison99 100| Provider | Speed | Cost (per book) | Quality | Use For |101|----------|-------|-----------------|---------|---------|102| Claude Haiku | โšกโšกโšก | $ | โญโญโญ | Testing, simple tasks |103| Claude Sonnet | โšกโšก | $$ | โญโญโญโญ | Balanced quality/cost |104| GPT-4o | โšกโšก | $$$ | โญโญโญโญโญ | Best quality |105| GPT-3.5 | โšกโšกโšก | $ | โญโญโญ | Fast and cheap |106| Ollama | โšก | FREE | โญโญ | Local, no API costs |107 108## Architecture109 110### Core Components111 112```113โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”114โ”‚           Gradio UI (app_llm_processor.py)      โ”‚115โ”‚  Upload โ†’ Configure โ†’ Estimate โ†’ Process โ†’ Download โ”‚116โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜117                        โ†“118โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”119โ”‚        Event Handlers (app_llm_processor_handlers.py)     โ”‚120โ”‚  โ€ข File validation    โ€ข Cost estimation          โ”‚121โ”‚  โ€ข API key checks     โ€ข Progress tracking        โ”‚122โ”‚  โ€ข Error handling     โ€ข Download generation      โ”‚123โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜124                        โ†“125โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”126โ”‚   Ebook      โ”‚    Text      โ”‚  Estimator   โ”‚    LLM      โ”‚127โ”‚  Extractor   โ”‚   Chunker    โ”‚              โ”‚  Processor  โ”‚128โ”‚              โ”‚              โ”‚              โ”‚             โ”‚129โ”‚ EPUB/PDF/TXT โ”‚ 3 Strategies โ”‚ Cost & Time  โ”‚ OpenAI/     โ”‚130โ”‚ โ†’ Chapters   โ”‚ โ†’ Chunks     โ”‚ Estimates    โ”‚ Anthropic   โ”‚131โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜132                        โ†“133โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”134โ”‚           TTS Integration + Downloads            โ”‚135โ”‚  ebook2audiobook TTS โ†’ M4B/MP3/WAV files        โ”‚136โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜137```138 139### File Structure140 141```142hf-eb2ab/143โ”œโ”€โ”€ app_llm_processor.py              # Main Gradio UI144โ”œโ”€โ”€ app_llm_processor_handlers.py     # Event handlers145โ”œโ”€โ”€ lib/146โ”‚   โ”œโ”€โ”€ ebook_extractor.py            # Text extraction147โ”‚   โ”œโ”€โ”€ text_chunker.py               # Chunking strategies148โ”‚   โ”œโ”€โ”€ estimator.py                  # Cost/time estimation149โ”‚   โ”œโ”€โ”€ llm_processor.py              # LLM processing150โ”‚   โ”œโ”€โ”€ tts_integration.py            # TTS integration151โ”‚   โ””โ”€โ”€ download_manager.py           # Download handling152โ”œโ”€โ”€ test_llm_processor_foundation.py  # Foundation tests153โ”œโ”€โ”€ test_llm_processor_ui.py          # UI/error tests154โ”œโ”€โ”€ QUICKSTART_LLM_PROCESSOR.md       # Quick start guide155โ”œโ”€โ”€ LLM_PROCESSOR_COMPLETE.md         # Implementation details156โ””โ”€โ”€ LLM_PROCESSOR_STATUS.md           # Development status157```158 159## Error Handling160 161Every operation has comprehensive error handling:162 163```python164# Example: File validation165if not ebook_file:166    return """167    ### โŒ Error: No File Selected168 169    Please upload an ebook file to continue.170 171    **Supported formats:** EPUB, PDF, TXT, HTML172    """173 174# Example: API key check175if not os.environ.get("ANTHROPIC_API_KEY"):176    return """177    ### โŒ Error: Missing API Key178 179    Anthropic API key not found.180 181    **To fix:**182    1. Get an API key from https://console.anthropic.com183    2. Set environment variable: ANTHROPIC_API_KEY=your-key-here184    3. Restart the application185    """186```187 188All errors include:189- User-friendly explanation190- Possible causes191- How to fix192- Debug information193 194## Testing195 196### Run Foundation Tests197```bash198python test_llm_processor_foundation.py199```200 201Tests:202- Ebook extraction (all formats)203- Text chunking (3 strategies)204- Cost estimation205- Provider comparison206 207### Run UI Tests208```bash209python test_llm_processor_ui.py210```211 212Tests:213- Error handling (10 scenarios)214- Validation logic215- Prerequisite checks216- API key validation217 218## API Keys219 220### Anthropic (Claude)2211. Sign up: https://console.anthropic.com2222. Create API key: https://console.anthropic.com/settings/keys2233. Set environment variable:224   ```bash225   export ANTHROPIC_API_KEY=sk-ant-xxxxx226   ```227 228### OpenAI (GPT)2291. Sign up: https://platform.openai.com2302. Create API key: https://platform.openai.com/api-keys2313. Set environment variable:232   ```bash233   export OPENAI_API_KEY=sk-xxxxx234   ```235 236### Cost Management237- Start with small test files238- Use Claude Haiku for testing (cheapest)239- Review estimates before processing240- Set billing alerts in API dashboards241 242## Chunking Strategies243 244### By Chapter (Semantic)245- **Best for**: Preserving book structure246- **Pros**: Natural breaks, maintains context247- **Cons**: Variable chunk sizes248- **Use when**: Processing fiction or structured content249 250### Fixed Size with Overlap251- **Best for**: Consistent processing252- **Pros**: Predictable chunk sizes, maintains context with overlap253- **Cons**: May break mid-sentence254- **Use when**: Long chapters or technical content255 256### Sentence Boundaries257- **Best for**: Natural language processing258- **Pros**: Clean breaks, no mid-sentence cuts259- **Cons**: Variable sizes260- **Use when**: Short stories or articles261 262## Progress Tracking263 264### LLM Processing265- Current chunk (e.g., "Processing 15/28")266- Time elapsed and remaining267- Cost so far vs estimated268- Chunks per minute269 270### TTS Conversion271- Current chapter272- Audio hours generated273- Time remaining274- Total cost tracking275 276## Development Status277 278- โœ… Core implementation (100%)279- โœ… Error handling (100%)280- โœ… UI integration (100%)281- โœ… Foundation testing (100%)282- โœ… Documentation (100%)283- โณ Full workflow testing (pending API keys)284- โณ TTS integration testing (pending service)285 286See [LLM_PROCESSOR_COMPLETE.md](LLM_PROCESSOR_COMPLETE.md) for detailed status.287 288## Requirements289 290### Python Packages291```292gradio>=5.42.0293openai>=1.0.0294anthropic>=0.25.0295tiktoken>=0.5.0296beautifulsoup4297ebooklib298lxml>=4.9.0299```300 301### Optional (for full TTS)302```303pymupdf4llm304coqui-tts305```306 307### API Keys308- Anthropic API key (for Claude)309- OR OpenAI API key (for GPT)310- OR Ollama running locally (free)311 312## Deployment313 314### Local315```bash316python app_llm_processor.py317```318 319### HuggingFace Spaces3201. Push to Spaces repository3212. Set secrets: `ANTHROPIC_API_KEY`, `OPENAI_API_KEY`3223. Configure `app_llm_processor.py` as app file3234. Set SDK: gradio 5.42.0324 325## Roadmap326 327### Completed328- [x] Multi-format ebook support329- [x] 3 chunking strategies330- [x] Cost/time estimation331- [x] OpenAI + Anthropic integration332- [x] Comprehensive error handling333- [x] Live progress tracking334- [x] TTS integration335- [x] Download management336 337### In Progress338- [ ] Full workflow testing with API keys339- [ ] TTS service integration testing340 341### Future342- [ ] Ollama local model support343- [ ] Batch processing344- [ ] Custom voice support345- [ ] REST API endpoints346- [ ] Template saving347- [ ] Processing history348 349## Support & Feedback350 351### For Errors352Check error message โ†’ It includes:353- What went wrong354- Why it might have happened355- How to fix it356- Debug information357 358### For Questions359See documentation:360- [QUICKSTART_LLM_PROCESSOR.md](QUICKSTART_LLM_PROCESSOR.md) - Usage guide361- [LLM_PROCESSOR_COMPLETE.md](LLM_PROCESSOR_COMPLETE.md) - Implementation details362- [LLM_PROCESSOR_STATUS.md](LLM_PROCESSOR_STATUS.md) - Development status363 364## License365 366Part of ebook2audiobook project.367 368## Author369 370Built with Claude Code (Anthropic)371Date: 2025-10-17372 373---374 375**Ready to transform your ebooks with AI!**376 377Start with `python app_llm_processor.py` and upload your first book.378