File size: 3,707 Bytes
1d0768f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
---
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.