TextCorrectionModel / README.md
MdSourav76046's picture
Upload 5 files
1d0768f verified
---
title: Text Correction API
emoji: πŸ”§
colorFrom: blue
colorTo: purple
sdk: docker
sdk_version: 1.0.0
app_file: app.py
pinned: false
---
# Text Correction API Server
This is the server-side API for text correction using your trained model.
## πŸ“ License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## πŸš€ Setup
### 1. Install Dependencies
```bash
pip install -r requirements.txt
```
### 2. Set Model Path
Make sure your trained model is in the `gpu_base_model2` directory, or set the `MODEL_PATH` environment variable:
```bash
export MODEL_PATH="./gpu_base_model2"
```
### 3. Run the Server
#### Local Development:
```bash
python main.py
```
Or using uvicorn directly:
```bash
uvicorn main:app --reload --host 0.0.0.0 --port 8000
```
The API will be available at: `http://localhost:8000`
### 4. Test the API
```bash
# Health check
curl http://localhost:8000/health
# Correct text
curl -X POST http://localhost:8000/correct \
-H "Content-Type: application/json" \
-d '{"text": "helo wrld this is a test"}'
```
## πŸ“‘ API Endpoints
### GET `/health`
Check if the API and model are ready.
**Response:**
```json
{
"status": "healthy",
"model_loaded": true,
"device": "cuda"
}
```
### POST `/correct`
Correct text using the trained model.
**Request:**
```json
{
"text": "helo wrld this is a test"
}
```
**Response:**
```json
{
"corrected_text": "hello world this is a test",
"processing_time": 0.45
}
```
## 🌐 Deployment Options
### Option 1: Hugging Face Spaces (Free) - Recommended
1. **Create a new Space** at https://huggingface.co/new-space
- Name: `your-username-text-correction`
- SDK: Docker
- License: **MIT** (or Apache 2.0)
- Click "Create Space"
2. **Upload files:**
- Upload all files from this directory
- Upload your `gpu_base_model2/` folder
3. **Your API will be live at:**
```
https://your-username-text-correction.hf.space/correct
```
### Option 2: Render (Free tier available)
1. Create a new Web Service
2. Connect your GitHub repository
3. Set build command: `pip install -r requirements.txt`
4. Set start command: `uvicorn main:app --host 0.0.0.0 --port $PORT`
5. Deploy
### Option 3: Railway (Free tier available)
1. Create a new project
2. Add a service from GitHub
3. Railway will auto-detect the Python app
4. Set environment variable `MODEL_PATH` if needed
5. Deploy
### Option 4: AWS/GCP/Azure
For production deployments with more control.
## βš™οΈ Environment Variables
- `MODEL_PATH`: Path to your trained model (default: `./gpu_base_model2`)
- `PORT`: Server port (default: `8000`)
## πŸ”’ Security Notes
⚠️ **Important for Production:**
1. Add authentication to your API endpoints
2. Set proper CORS origins (not `*`)
3. Add rate limiting
4. Use HTTPS
5. Keep your API key secure
## πŸ› Troubleshooting
### Model not loading
- Check that `gpu_base_model2` directory exists
- Verify all model files are present
- Check console logs for specific errors
### Out of memory
- Reduce `max_length` in the generate function
- Use smaller batch sizes
- Consider using CPU instead of GPU
### Slow inference
- Use GPU if available
- Reduce `num_beams` parameter
- Use quantization for faster inference
## πŸ“Š Usage
This API is designed to be called from an iOS app for correcting OCR text. The typical flow is:
1. User takes/selects an image
2. OCR extracts text from the image
3. Extracted text is sent to this API
4. API corrects the text using the trained model
5. Corrected text is returned to the app
## 🀝 Contributing
This is a private project for text correction. For questions or issues, please contact the project owner.