first push
Browse files- Dockerfile +15 -24
- README.md +22 -17
- README_SPACE.md +3 -2
- TROUBLESHOOTING.md +69 -0
- app/main.py +6 -0
- requirements.txt +0 -1
- setup.py +96 -0
- ุงุฎุทุงุก.txt +0 -0
Dockerfile
CHANGED
|
@@ -4,7 +4,7 @@ FROM python:3.11-slim
|
|
| 4 |
# Set working directory
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
-
# Install system dependencies
|
| 8 |
RUN apt-get update && apt-get install -y \
|
| 9 |
libreoffice \
|
| 10 |
libreoffice-writer \
|
|
@@ -13,42 +13,33 @@ RUN apt-get update && apt-get install -y \
|
|
| 13 |
fonts-liberation \
|
| 14 |
fonts-dejavu-core \
|
| 15 |
curl \
|
| 16 |
-
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
| 17 |
|
| 18 |
-
# Create
|
| 19 |
-
RUN useradd -m -u 1000 user
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
USER user
|
| 21 |
|
| 22 |
# Set environment variables
|
| 23 |
ENV HOME=/home/user \
|
| 24 |
PATH=/home/user/.local/bin:$PATH \
|
| 25 |
-
PYTHONPATH
|
| 26 |
-
PYTHONUNBUFFERED=1
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
GRADIO_THEME=huggingface \
|
| 31 |
-
SYSTEM=spaces
|
| 32 |
-
|
| 33 |
-
# Copy requirements first for better caching
|
| 34 |
-
COPY --chown=user requirements.txt .
|
| 35 |
-
|
| 36 |
-
# Install Python dependencies
|
| 37 |
RUN pip install --no-cache-dir --upgrade pip && \
|
| 38 |
pip install --no-cache-dir -r requirements.txt
|
| 39 |
|
| 40 |
# Copy application code
|
| 41 |
-
COPY --chown=user . .
|
| 42 |
-
|
| 43 |
-
# Create necessary directories
|
| 44 |
-
RUN mkdir -p uploads web
|
| 45 |
|
| 46 |
# Expose port
|
| 47 |
EXPOSE 7860
|
| 48 |
|
| 49 |
-
# Health check
|
| 50 |
-
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
| 51 |
-
CMD curl -f http://localhost:7860/health || exit 1
|
| 52 |
-
|
| 53 |
# Run the application
|
| 54 |
CMD ["python", "app/main.py"]
|
|
|
|
| 4 |
# Set working directory
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
+
# Install system dependencies including LibreOffice
|
| 8 |
RUN apt-get update && apt-get install -y \
|
| 9 |
libreoffice \
|
| 10 |
libreoffice-writer \
|
|
|
|
| 13 |
fonts-liberation \
|
| 14 |
fonts-dejavu-core \
|
| 15 |
curl \
|
| 16 |
+
&& rm -rf /var/lib/apt/lists/* \
|
| 17 |
+
&& apt-get clean
|
| 18 |
|
| 19 |
+
# Create user and set permissions
|
| 20 |
+
RUN useradd -m -u 1000 user && \
|
| 21 |
+
mkdir -p /app/uploads /app/web && \
|
| 22 |
+
chown -R user:user /app
|
| 23 |
+
|
| 24 |
+
# Switch to non-root user
|
| 25 |
USER user
|
| 26 |
|
| 27 |
# Set environment variables
|
| 28 |
ENV HOME=/home/user \
|
| 29 |
PATH=/home/user/.local/bin:$PATH \
|
| 30 |
+
PYTHONPATH=/app \
|
| 31 |
+
PYTHONUNBUFFERED=1
|
| 32 |
+
|
| 33 |
+
# Copy requirements and install Python dependencies
|
| 34 |
+
COPY --chown=user:user requirements.txt .
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
RUN pip install --no-cache-dir --upgrade pip && \
|
| 36 |
pip install --no-cache-dir -r requirements.txt
|
| 37 |
|
| 38 |
# Copy application code
|
| 39 |
+
COPY --chown=user:user . .
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# Expose port
|
| 42 |
EXPOSE 7860
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
# Run the application
|
| 45 |
CMD ["python", "app/main.py"]
|
README.md
CHANGED
|
@@ -12,6 +12,19 @@ A powerful document translation service that converts PDF and DOCX files using A
|
|
| 12 |
- **Download Options**: Get both original and translated files
|
| 13 |
- **Translation Reports**: Detailed statistics on processed content
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
## ๐ Quick Start
|
| 16 |
|
| 17 |
### Environment Variables
|
|
@@ -23,31 +36,23 @@ export OPENROUTER_API_KEY="your_api_key_here"
|
|
| 23 |
|
| 24 |
### Local Development
|
| 25 |
|
| 26 |
-
1. **
|
| 27 |
```bash
|
| 28 |
-
|
| 29 |
```
|
| 30 |
|
| 31 |
-
2. **
|
| 32 |
```bash
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
```
|
| 42 |
|
| 43 |
-
3. **Run the Application**:
|
| 44 |
-
```bash
|
| 45 |
-
python app/main.py
|
| 46 |
-
```
|
| 47 |
-
|
| 48 |
-
4. **Access the Interface**:
|
| 49 |
-
Open your browser to `http://localhost:7860`
|
| 50 |
-
|
| 51 |
### Docker Deployment
|
| 52 |
|
| 53 |
1. **Build the Image**:
|
|
|
|
| 12 |
- **Download Options**: Get both original and translated files
|
| 13 |
- **Translation Reports**: Detailed statistics on processed content
|
| 14 |
|
| 15 |
+
## ๐ง Fixed Issues
|
| 16 |
+
|
| 17 |
+
### Docker Build Permissions
|
| 18 |
+
The original Docker build was failing due to permission issues when creating directories as a non-root user. This has been fixed by:
|
| 19 |
+
- Creating directories before switching to non-root user
|
| 20 |
+
- Properly setting ownership of the `/app` directory
|
| 21 |
+
- Simplified Dockerfile structure for better reliability
|
| 22 |
+
|
| 23 |
+
### Python Dependencies
|
| 24 |
+
- Removed conflicting `pathlib` dependency (built into Python 3.11)
|
| 25 |
+
- Updated requirements.txt with compatible versions
|
| 26 |
+
- Added proper Python path handling in main.py
|
| 27 |
+
|
| 28 |
## ๐ Quick Start
|
| 29 |
|
| 30 |
### Environment Variables
|
|
|
|
| 36 |
|
| 37 |
### Local Development
|
| 38 |
|
| 39 |
+
1. **Quick Setup** (recommended):
|
| 40 |
```bash
|
| 41 |
+
python setup.py
|
| 42 |
```
|
| 43 |
|
| 44 |
+
2. **Manual Setup**:
|
| 45 |
```bash
|
| 46 |
+
# Set your API key
|
| 47 |
+
export OPENROUTER_API_KEY="your_key_here"
|
| 48 |
|
| 49 |
+
# Install dependencies
|
| 50 |
+
pip install -r requirements.txt
|
| 51 |
|
| 52 |
+
# Run the app
|
| 53 |
+
python app.py
|
| 54 |
```
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
### Docker Deployment
|
| 57 |
|
| 58 |
1. **Build the Image**:
|
README_SPACE.md
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
|
|
| 1 |
title: Document Translator
|
| 2 |
emoji: ๐
|
| 3 |
colorFrom: blue
|
| 4 |
colorTo: purple
|
| 5 |
sdk: docker
|
| 6 |
app_port: 7860
|
| 7 |
-
app_file: app.py
|
| 8 |
pinned: false
|
| 9 |
license: mit
|
| 10 |
-
short_description: Translate PDF and DOCX documents using
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
title: Document Translator
|
| 3 |
emoji: ๐
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
| 6 |
sdk: docker
|
| 7 |
app_port: 7860
|
|
|
|
| 8 |
pinned: false
|
| 9 |
license: mit
|
| 10 |
+
short_description: Translate PDF and DOCX documents using Google Gemini 2.5 Pro from OpenRouter
|
| 11 |
+
---
|
TROUBLESHOOTING.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Troubleshooting Guide
|
| 2 |
+
|
| 3 |
+
## Common Issues and Solutions
|
| 4 |
+
|
| 5 |
+
### 1. Docker Build Permission Error
|
| 6 |
+
**Error**: `mkdir: cannot create directory 'uploads': Permission denied`
|
| 7 |
+
|
| 8 |
+
**Solution**: This has been fixed in the updated Dockerfile. Make sure you're using the latest version.
|
| 9 |
+
|
| 10 |
+
### 2. LibreOffice Not Found
|
| 11 |
+
**Error**: `libreoffice: command not found`
|
| 12 |
+
|
| 13 |
+
**Solution**:
|
| 14 |
+
- **Ubuntu/Debian**: `sudo apt-get install libreoffice`
|
| 15 |
+
- **macOS**: `brew install --cask libreoffice`
|
| 16 |
+
- **Windows**: Download from https://www.libreoffice.org/
|
| 17 |
+
|
| 18 |
+
### 3. OpenRouter API Key Missing
|
| 19 |
+
**Error**: Translation fails or returns original text
|
| 20 |
+
|
| 21 |
+
**Solution**: Set your API key:
|
| 22 |
+
```bash
|
| 23 |
+
export OPENROUTER_API_KEY="your_key_here"
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
### 4. Python Import Errors
|
| 27 |
+
**Error**: `ModuleNotFoundError: No module named 'translator'`
|
| 28 |
+
|
| 29 |
+
**Solution**: Run from the project root directory:
|
| 30 |
+
```bash
|
| 31 |
+
cd /path/to/tr\ 0.1
|
| 32 |
+
python app.py
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
### 5. Port Already in Use
|
| 36 |
+
**Error**: `OSError: [Errno 98] Address already in use`
|
| 37 |
+
|
| 38 |
+
**Solution**: Change the port in `app.py`:
|
| 39 |
+
```python
|
| 40 |
+
uvicorn.run(app, host="0.0.0.0", port=8000) # Changed from 7860
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
### 6. File Upload Fails
|
| 44 |
+
**Error**: File size or type errors
|
| 45 |
+
|
| 46 |
+
**Solution**: Check file constraints:
|
| 47 |
+
- Max size: 50MB
|
| 48 |
+
- Supported formats: PDF, DOCX only
|
| 49 |
+
|
| 50 |
+
### 7. Translation Returns Original Text
|
| 51 |
+
**Possible causes**:
|
| 52 |
+
- Invalid API key
|
| 53 |
+
- Network connectivity issues
|
| 54 |
+
- API rate limits exceeded
|
| 55 |
+
|
| 56 |
+
**Solution**: Check logs and verify API key is working.
|
| 57 |
+
|
| 58 |
+
## Getting Help
|
| 59 |
+
|
| 60 |
+
1. Run the setup script: `python setup.py`
|
| 61 |
+
2. Check the logs for detailed error messages
|
| 62 |
+
3. Verify all requirements are installed: `pip list`
|
| 63 |
+
4. Test LibreOffice: `libreoffice --version`
|
| 64 |
+
|
| 65 |
+
## For Hugging Face Spaces
|
| 66 |
+
|
| 67 |
+
Make sure to set the `OPENROUTER_API_KEY` in your Space settings:
|
| 68 |
+
1. Go to Settings โ Repository secrets
|
| 69 |
+
2. Add `OPENROUTER_API_KEY` with your API key value
|
app/main.py
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
| 3 |
from fastapi.responses import FileResponse, JSONResponse
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# Add the parent directory to the Python path
|
| 5 |
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 6 |
+
|
| 7 |
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 8 |
from fastapi.staticfiles import StaticFiles
|
| 9 |
from fastapi.responses import FileResponse, JSONResponse
|
requirements.txt
CHANGED
|
@@ -6,5 +6,4 @@ aiohttp==3.9.1
|
|
| 6 |
python-docx==1.1.0
|
| 7 |
requests==2.31.0
|
| 8 |
Pillow==10.1.0
|
| 9 |
-
pathlib==1.0.1
|
| 10 |
typing-extensions==4.8.0
|
|
|
|
| 6 |
python-docx==1.1.0
|
| 7 |
requests==2.31.0
|
| 8 |
Pillow==10.1.0
|
|
|
|
| 9 |
typing-extensions==4.8.0
|
setup.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Setup script for Document Translator
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import sys
|
| 8 |
+
import subprocess
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
def install_requirements():
|
| 12 |
+
"""Install Python requirements"""
|
| 13 |
+
print("๐ฆ Installing Python requirements...")
|
| 14 |
+
try:
|
| 15 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
|
| 16 |
+
print("โ
Requirements installed successfully")
|
| 17 |
+
except subprocess.CalledProcessError as e:
|
| 18 |
+
print(f"โ Failed to install requirements: {e}")
|
| 19 |
+
return False
|
| 20 |
+
return True
|
| 21 |
+
|
| 22 |
+
def check_libreoffice():
|
| 23 |
+
"""Check if LibreOffice is installed"""
|
| 24 |
+
print("๐ Checking LibreOffice installation...")
|
| 25 |
+
try:
|
| 26 |
+
result = subprocess.run(["libreoffice", "--version"],
|
| 27 |
+
capture_output=True, text=True, timeout=10)
|
| 28 |
+
if result.returncode == 0:
|
| 29 |
+
print(f"โ
LibreOffice found: {result.stdout.strip()}")
|
| 30 |
+
return True
|
| 31 |
+
else:
|
| 32 |
+
print("โ LibreOffice not working properly")
|
| 33 |
+
return False
|
| 34 |
+
except FileNotFoundError:
|
| 35 |
+
print("โ LibreOffice not found")
|
| 36 |
+
print("Please install LibreOffice:")
|
| 37 |
+
print(" - Windows: Download from https://www.libreoffice.org/")
|
| 38 |
+
print(" - macOS: brew install --cask libreoffice")
|
| 39 |
+
print(" - Ubuntu/Debian: sudo apt-get install libreoffice")
|
| 40 |
+
return False
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"โ Error checking LibreOffice: {e}")
|
| 43 |
+
return False
|
| 44 |
+
|
| 45 |
+
def check_api_key():
|
| 46 |
+
"""Check if OpenRouter API key is set"""
|
| 47 |
+
print("๐ Checking OpenRouter API key...")
|
| 48 |
+
api_key = os.getenv('OPENROUTER_API_KEY')
|
| 49 |
+
if api_key:
|
| 50 |
+
print(f"โ
API key found (length: {len(api_key)})")
|
| 51 |
+
return True
|
| 52 |
+
else:
|
| 53 |
+
print("โ ๏ธ OpenRouter API key not found")
|
| 54 |
+
print("Please set it: export OPENROUTER_API_KEY='your_key_here'")
|
| 55 |
+
return False
|
| 56 |
+
|
| 57 |
+
def create_directories():
|
| 58 |
+
"""Create necessary directories"""
|
| 59 |
+
print("๐ Creating directories...")
|
| 60 |
+
directories = ['uploads', 'web']
|
| 61 |
+
for dir_name in directories:
|
| 62 |
+
Path(dir_name).mkdir(exist_ok=True)
|
| 63 |
+
print(f"โ
Created/verified {dir_name} directory")
|
| 64 |
+
|
| 65 |
+
def main():
|
| 66 |
+
"""Main setup function"""
|
| 67 |
+
print("๐ Setting up Document Translator...")
|
| 68 |
+
print("=" * 50)
|
| 69 |
+
|
| 70 |
+
success = True
|
| 71 |
+
|
| 72 |
+
# Create directories
|
| 73 |
+
create_directories()
|
| 74 |
+
|
| 75 |
+
# Install requirements
|
| 76 |
+
if not install_requirements():
|
| 77 |
+
success = False
|
| 78 |
+
|
| 79 |
+
# Check LibreOffice
|
| 80 |
+
if not check_libreoffice():
|
| 81 |
+
success = False
|
| 82 |
+
|
| 83 |
+
# Check API key
|
| 84 |
+
check_api_key() # Warning only, not failure
|
| 85 |
+
|
| 86 |
+
print("=" * 50)
|
| 87 |
+
if success:
|
| 88 |
+
print("๐ Setup completed successfully!")
|
| 89 |
+
print("Run: python app.py")
|
| 90 |
+
print("Then open: http://localhost:7860")
|
| 91 |
+
else:
|
| 92 |
+
print("โ Setup failed. Please fix the issues above.")
|
| 93 |
+
sys.exit(1)
|
| 94 |
+
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
main()
|
ุงุฎุทุงุก.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|