Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- Dockerfile +29 -0
- LICENSE +22 -0
- README.md +175 -0
- app.py +189 -0
- requirements.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
# Set working directory
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Install system dependencies
|
| 7 |
+
RUN apt-get update && apt-get install -y \
|
| 8 |
+
build-essential \
|
| 9 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
+
|
| 11 |
+
# Copy requirements first for better caching
|
| 12 |
+
COPY requirements.txt .
|
| 13 |
+
|
| 14 |
+
# Install Python dependencies
|
| 15 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 16 |
+
|
| 17 |
+
# Copy application files
|
| 18 |
+
COPY app.py .
|
| 19 |
+
|
| 20 |
+
# Expose port
|
| 21 |
+
EXPOSE 8000
|
| 22 |
+
|
| 23 |
+
# Health check
|
| 24 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
|
| 25 |
+
CMD python -c "import requests; requests.get('http://localhost:8000/health')"
|
| 26 |
+
|
| 27 |
+
# Run the application
|
| 28 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
| 29 |
+
|
LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2024 Text Correction App
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
| 22 |
+
|
README.md
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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14 |
+
This is the server-side API for text correction using your trained model.
|
| 15 |
+
|
| 16 |
+
## 📝 License
|
| 17 |
+
|
| 18 |
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
| 19 |
+
|
| 20 |
+
## 🚀 Setup
|
| 21 |
+
|
| 22 |
+
### 1. Install Dependencies
|
| 23 |
+
|
| 24 |
+
```bash
|
| 25 |
+
pip install -r requirements.txt
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
### 2. Set Model Path
|
| 29 |
+
|
| 30 |
+
Make sure your trained model is in the `gpu_base_model2` directory, or set the `MODEL_PATH` environment variable:
|
| 31 |
+
|
| 32 |
+
```bash
|
| 33 |
+
export MODEL_PATH="./gpu_base_model2"
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
### 3. Run the Server
|
| 37 |
+
|
| 38 |
+
#### Local Development:
|
| 39 |
+
```bash
|
| 40 |
+
python main.py
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
Or using uvicorn directly:
|
| 44 |
+
```bash
|
| 45 |
+
uvicorn main:app --reload --host 0.0.0.0 --port 8000
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
The API will be available at: `http://localhost:8000`
|
| 49 |
+
|
| 50 |
+
### 4. Test the API
|
| 51 |
+
|
| 52 |
+
```bash
|
| 53 |
+
# Health check
|
| 54 |
+
curl http://localhost:8000/health
|
| 55 |
+
|
| 56 |
+
# Correct text
|
| 57 |
+
curl -X POST http://localhost:8000/correct \
|
| 58 |
+
-H "Content-Type: application/json" \
|
| 59 |
+
-d '{"text": "helo wrld this is a test"}'
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
## 📡 API Endpoints
|
| 63 |
+
|
| 64 |
+
### GET `/health`
|
| 65 |
+
Check if the API and model are ready.
|
| 66 |
+
|
| 67 |
+
**Response:**
|
| 68 |
+
```json
|
| 69 |
+
{
|
| 70 |
+
"status": "healthy",
|
| 71 |
+
"model_loaded": true,
|
| 72 |
+
"device": "cuda"
|
| 73 |
+
}
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
### POST `/correct`
|
| 77 |
+
Correct text using the trained model.
|
| 78 |
+
|
| 79 |
+
**Request:**
|
| 80 |
+
```json
|
| 81 |
+
{
|
| 82 |
+
"text": "helo wrld this is a test"
|
| 83 |
+
}
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
**Response:**
|
| 87 |
+
```json
|
| 88 |
+
{
|
| 89 |
+
"corrected_text": "hello world this is a test",
|
| 90 |
+
"processing_time": 0.45
|
| 91 |
+
}
|
| 92 |
+
```
|
| 93 |
+
|
| 94 |
+
## 🌐 Deployment Options
|
| 95 |
+
|
| 96 |
+
### Option 1: Hugging Face Spaces (Free) - Recommended
|
| 97 |
+
|
| 98 |
+
1. **Create a new Space** at https://huggingface.co/new-space
|
| 99 |
+
- Name: `your-username-text-correction`
|
| 100 |
+
- SDK: Docker
|
| 101 |
+
- License: **MIT** (or Apache 2.0)
|
| 102 |
+
- Click "Create Space"
|
| 103 |
+
|
| 104 |
+
2. **Upload files:**
|
| 105 |
+
- Upload all files from this directory
|
| 106 |
+
- Upload your `gpu_base_model2/` folder
|
| 107 |
+
|
| 108 |
+
3. **Your API will be live at:**
|
| 109 |
+
```
|
| 110 |
+
https://your-username-text-correction.hf.space/correct
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
### Option 2: Render (Free tier available)
|
| 114 |
+
|
| 115 |
+
1. Create a new Web Service
|
| 116 |
+
2. Connect your GitHub repository
|
| 117 |
+
3. Set build command: `pip install -r requirements.txt`
|
| 118 |
+
4. Set start command: `uvicorn main:app --host 0.0.0.0 --port $PORT`
|
| 119 |
+
5. Deploy
|
| 120 |
+
|
| 121 |
+
### Option 3: Railway (Free tier available)
|
| 122 |
+
|
| 123 |
+
1. Create a new project
|
| 124 |
+
2. Add a service from GitHub
|
| 125 |
+
3. Railway will auto-detect the Python app
|
| 126 |
+
4. Set environment variable `MODEL_PATH` if needed
|
| 127 |
+
5. Deploy
|
| 128 |
+
|
| 129 |
+
### Option 4: AWS/GCP/Azure
|
| 130 |
+
For production deployments with more control.
|
| 131 |
+
|
| 132 |
+
## ⚙️ Environment Variables
|
| 133 |
+
|
| 134 |
+
- `MODEL_PATH`: Path to your trained model (default: `./gpu_base_model2`)
|
| 135 |
+
- `PORT`: Server port (default: `8000`)
|
| 136 |
+
|
| 137 |
+
## 🔒 Security Notes
|
| 138 |
+
|
| 139 |
+
⚠️ **Important for Production:**
|
| 140 |
+
1. Add authentication to your API endpoints
|
| 141 |
+
2. Set proper CORS origins (not `*`)
|
| 142 |
+
3. Add rate limiting
|
| 143 |
+
4. Use HTTPS
|
| 144 |
+
5. Keep your API key secure
|
| 145 |
+
|
| 146 |
+
## 🐛 Troubleshooting
|
| 147 |
+
|
| 148 |
+
### Model not loading
|
| 149 |
+
- Check that `gpu_base_model2` directory exists
|
| 150 |
+
- Verify all model files are present
|
| 151 |
+
- Check console logs for specific errors
|
| 152 |
+
|
| 153 |
+
### Out of memory
|
| 154 |
+
- Reduce `max_length` in the generate function
|
| 155 |
+
- Use smaller batch sizes
|
| 156 |
+
- Consider using CPU instead of GPU
|
| 157 |
+
|
| 158 |
+
### Slow inference
|
| 159 |
+
- Use GPU if available
|
| 160 |
+
- Reduce `num_beams` parameter
|
| 161 |
+
- Use quantization for faster inference
|
| 162 |
+
|
| 163 |
+
## 📊 Usage
|
| 164 |
+
|
| 165 |
+
This API is designed to be called from an iOS app for correcting OCR text. The typical flow is:
|
| 166 |
+
|
| 167 |
+
1. User takes/selects an image
|
| 168 |
+
2. OCR extracts text from the image
|
| 169 |
+
3. Extracted text is sent to this API
|
| 170 |
+
4. API corrects the text using the trained model
|
| 171 |
+
5. Corrected text is returned to the app
|
| 172 |
+
|
| 173 |
+
## 🤝 Contributing
|
| 174 |
+
|
| 175 |
+
This is a private project for text correction. For questions or issues, please contact the project owner.
|
app.py
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
FastAPI Server for Text Correction
|
| 3 |
+
Deploy this to run your text correction model as an API
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from fastapi import FastAPI, HTTPException
|
| 7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
from pydantic import BaseModel
|
| 9 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 10 |
+
import torch
|
| 11 |
+
import os
|
| 12 |
+
from typing import Optional
|
| 13 |
+
|
| 14 |
+
# Initialize FastAPI app
|
| 15 |
+
app = FastAPI(
|
| 16 |
+
title="Text Correction API",
|
| 17 |
+
description="API for correcting OCR text using trained model",
|
| 18 |
+
version="1.0.0"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Add CORS middleware to allow requests from iOS app
|
| 22 |
+
app.add_middleware(
|
| 23 |
+
CORSMiddleware,
|
| 24 |
+
allow_origins=["*"], # In production, specify your iOS app's domain
|
| 25 |
+
allow_credentials=True,
|
| 26 |
+
allow_methods=["*"],
|
| 27 |
+
allow_headers=["*"],
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Global variables for model
|
| 31 |
+
model = None
|
| 32 |
+
tokenizer = None
|
| 33 |
+
device = None
|
| 34 |
+
|
| 35 |
+
# Pydantic models for request/response
|
| 36 |
+
class TextRequest(BaseModel):
|
| 37 |
+
text: str
|
| 38 |
+
|
| 39 |
+
class TextResponse(BaseModel):
|
| 40 |
+
corrected_text: str
|
| 41 |
+
processing_time: float
|
| 42 |
+
|
| 43 |
+
class HealthResponse(BaseModel):
|
| 44 |
+
status: str
|
| 45 |
+
model_loaded: bool
|
| 46 |
+
device: str
|
| 47 |
+
|
| 48 |
+
# Load model at startup
|
| 49 |
+
@app.on_event("startup")
|
| 50 |
+
async def load_model():
|
| 51 |
+
global model, tokenizer, device
|
| 52 |
+
|
| 53 |
+
print("🚀 Starting Text Correction API...")
|
| 54 |
+
|
| 55 |
+
# Determine device
|
| 56 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 57 |
+
print(f"📱 Using device: {device}")
|
| 58 |
+
|
| 59 |
+
# Load model and tokenizer
|
| 60 |
+
try:
|
| 61 |
+
# Try to load from environment variable first
|
| 62 |
+
model_path = os.getenv("MODEL_PATH")
|
| 63 |
+
|
| 64 |
+
# If not set, try to load from local directory
|
| 65 |
+
if not model_path:
|
| 66 |
+
if os.path.exists("./gpu_base_model2"):
|
| 67 |
+
model_path = "./gpu_base_model2"
|
| 68 |
+
else:
|
| 69 |
+
# If model not found locally, download from Hugging Face
|
| 70 |
+
# This is your model repository on Hugging Face
|
| 71 |
+
model_path = os.getenv("HF_MODEL_PATH", "MdSourav76046/TextCorrectionModel2")
|
| 72 |
+
print(f"📥 Model not found locally, will download from: {model_path}")
|
| 73 |
+
print(" This may take a few minutes on first run...")
|
| 74 |
+
|
| 75 |
+
print(f"📦 Loading model from: {model_path}")
|
| 76 |
+
|
| 77 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
|
| 78 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 79 |
+
|
| 80 |
+
# Move model to device
|
| 81 |
+
model.to(device)
|
| 82 |
+
model.eval()
|
| 83 |
+
|
| 84 |
+
print("✅ Model loaded successfully!")
|
| 85 |
+
print(f" - Model type: {type(model).__name__}")
|
| 86 |
+
print(f" - Vocabulary size: {tokenizer.vocab_size}")
|
| 87 |
+
print(f" - Device: {device}")
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"❌ Error loading model: {e}")
|
| 91 |
+
print("⚠️ API will not work until model is loaded")
|
| 92 |
+
|
| 93 |
+
# Health check endpoint
|
| 94 |
+
@app.get("/health", response_model=HealthResponse)
|
| 95 |
+
async def health_check():
|
| 96 |
+
"""Check if the API and model are ready"""
|
| 97 |
+
return HealthResponse(
|
| 98 |
+
status="healthy" if model is not None else "unhealthy",
|
| 99 |
+
model_loaded=model is not None,
|
| 100 |
+
device=device or "unknown"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Text correction endpoint
|
| 104 |
+
@app.post("/correct", response_model=TextResponse)
|
| 105 |
+
async def correct_text(request: TextRequest):
|
| 106 |
+
"""
|
| 107 |
+
Correct text using the trained model
|
| 108 |
+
|
| 109 |
+
Args:
|
| 110 |
+
request: TextRequest containing the text to correct
|
| 111 |
+
|
| 112 |
+
Returns:
|
| 113 |
+
TextResponse with corrected text and processing time
|
| 114 |
+
"""
|
| 115 |
+
import time
|
| 116 |
+
|
| 117 |
+
if model is None or tokenizer is None:
|
| 118 |
+
raise HTTPException(
|
| 119 |
+
status_code=503,
|
| 120 |
+
detail="Model not loaded. Please wait for the model to initialize."
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
if not request.text or not request.text.strip():
|
| 124 |
+
raise HTTPException(
|
| 125 |
+
status_code=400,
|
| 126 |
+
detail="Text cannot be empty"
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
start_time = time.time()
|
| 130 |
+
|
| 131 |
+
try:
|
| 132 |
+
# Tokenize input text
|
| 133 |
+
inputs = tokenizer(
|
| 134 |
+
request.text,
|
| 135 |
+
return_tensors="pt",
|
| 136 |
+
max_length=512,
|
| 137 |
+
truncation=True,
|
| 138 |
+
padding=True
|
| 139 |
+
).to(device)
|
| 140 |
+
|
| 141 |
+
# Generate corrected text
|
| 142 |
+
with torch.no_grad():
|
| 143 |
+
outputs = model.generate(
|
| 144 |
+
inputs.input_ids,
|
| 145 |
+
attention_mask=inputs.attention_mask,
|
| 146 |
+
max_length=512,
|
| 147 |
+
num_beams=5,
|
| 148 |
+
early_stopping=True,
|
| 149 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 150 |
+
eos_token_id=tokenizer.eos_token_id
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
# Decode output
|
| 154 |
+
corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 155 |
+
|
| 156 |
+
processing_time = time.time() - start_time
|
| 157 |
+
|
| 158 |
+
print(f"✅ Text corrected in {processing_time:.2f}s")
|
| 159 |
+
print(f" Input: {request.text[:50]}...")
|
| 160 |
+
print(f" Output: {corrected_text[:50]}...")
|
| 161 |
+
|
| 162 |
+
return TextResponse(
|
| 163 |
+
corrected_text=corrected_text,
|
| 164 |
+
processing_time=round(processing_time, 2)
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
except Exception as e:
|
| 168 |
+
print(f"❌ Error during correction: {e}")
|
| 169 |
+
raise HTTPException(
|
| 170 |
+
status_code=500,
|
| 171 |
+
detail=f"Text correction failed: {str(e)}"
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
# Root endpoint
|
| 175 |
+
@app.get("/")
|
| 176 |
+
async def root():
|
| 177 |
+
return {
|
| 178 |
+
"message": "Text Correction API",
|
| 179 |
+
"version": "1.0.0",
|
| 180 |
+
"endpoints": {
|
| 181 |
+
"health": "/health",
|
| 182 |
+
"correct": "/correct (POST)"
|
| 183 |
+
}
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
if __name__ == "__main__":
|
| 187 |
+
import uvicorn
|
| 188 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 189 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.104.1
|
| 2 |
+
uvicorn[standard]==0.24.0
|
| 3 |
+
transformers==4.35.0
|
| 4 |
+
torch==2.1.0
|
| 5 |
+
pydantic==2.5.0
|
| 6 |
+
python-multipart==0.0.6
|
| 7 |
+
|