MdSourav76046 commited on
Commit
91963b3
·
verified ·
1 Parent(s): b140438

Upload 5 files

Browse files
Files changed (5) hide show
  1. Dockerfile +33 -0
  2. LICENSE +22 -0
  3. README.md +164 -12
  4. app.py +176 -0
  5. requirements.txt +7 -0
Dockerfile ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Copy model directory (make sure gpu_base_model2 exists)
21
+ # You can also mount this as a volume or download it at runtime
22
+ COPY gpu_base_model2 ./gpu_base_model2
23
+
24
+ # Expose port
25
+ EXPOSE 8000
26
+
27
+ # Health check
28
+ HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
29
+ CMD python -c "import requests; requests.get('http://localhost:8000/health')"
30
+
31
+ # Run the application
32
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
33
+
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 CHANGED
@@ -1,12 +1,164 @@
1
- ---
2
- title: TextCorrectionModel
3
- emoji: 🐨
4
- colorFrom: green
5
- colorTo: gray
6
- sdk: docker
7
- pinned: false
8
- license: mit
9
- short_description: This will recieve a text and correct the text and return it.
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Text Correction API Server
2
+
3
+ This is the server-side API for text correction using your trained model.
4
+
5
+ ## 📝 License
6
+
7
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
8
+
9
+ ## 🚀 Setup
10
+
11
+ ### 1. Install Dependencies
12
+
13
+ ```bash
14
+ pip install -r requirements.txt
15
+ ```
16
+
17
+ ### 2. Set Model Path
18
+
19
+ Make sure your trained model is in the `gpu_base_model2` directory, or set the `MODEL_PATH` environment variable:
20
+
21
+ ```bash
22
+ export MODEL_PATH="./gpu_base_model2"
23
+ ```
24
+
25
+ ### 3. Run the Server
26
+
27
+ #### Local Development:
28
+ ```bash
29
+ python main.py
30
+ ```
31
+
32
+ Or using uvicorn directly:
33
+ ```bash
34
+ uvicorn main:app --reload --host 0.0.0.0 --port 8000
35
+ ```
36
+
37
+ The API will be available at: `http://localhost:8000`
38
+
39
+ ### 4. Test the API
40
+
41
+ ```bash
42
+ # Health check
43
+ curl http://localhost:8000/health
44
+
45
+ # Correct text
46
+ curl -X POST http://localhost:8000/correct \
47
+ -H "Content-Type: application/json" \
48
+ -d '{"text": "helo wrld this is a test"}'
49
+ ```
50
+
51
+ ## 📡 API Endpoints
52
+
53
+ ### GET `/health`
54
+ Check if the API and model are ready.
55
+
56
+ **Response:**
57
+ ```json
58
+ {
59
+ "status": "healthy",
60
+ "model_loaded": true,
61
+ "device": "cuda"
62
+ }
63
+ ```
64
+
65
+ ### POST `/correct`
66
+ Correct text using the trained model.
67
+
68
+ **Request:**
69
+ ```json
70
+ {
71
+ "text": "helo wrld this is a test"
72
+ }
73
+ ```
74
+
75
+ **Response:**
76
+ ```json
77
+ {
78
+ "corrected_text": "hello world this is a test",
79
+ "processing_time": 0.45
80
+ }
81
+ ```
82
+
83
+ ## 🌐 Deployment Options
84
+
85
+ ### Option 1: Hugging Face Spaces (Free) - Recommended
86
+
87
+ 1. **Create a new Space** at https://huggingface.co/new-space
88
+ - Name: `your-username-text-correction`
89
+ - SDK: Docker
90
+ - License: **MIT** (or Apache 2.0)
91
+ - Click "Create Space"
92
+
93
+ 2. **Upload files:**
94
+ - Upload all files from this directory
95
+ - Upload your `gpu_base_model2/` folder
96
+
97
+ 3. **Your API will be live at:**
98
+ ```
99
+ https://your-username-text-correction.hf.space/correct
100
+ ```
101
+
102
+ ### Option 2: Render (Free tier available)
103
+
104
+ 1. Create a new Web Service
105
+ 2. Connect your GitHub repository
106
+ 3. Set build command: `pip install -r requirements.txt`
107
+ 4. Set start command: `uvicorn main:app --host 0.0.0.0 --port $PORT`
108
+ 5. Deploy
109
+
110
+ ### Option 3: Railway (Free tier available)
111
+
112
+ 1. Create a new project
113
+ 2. Add a service from GitHub
114
+ 3. Railway will auto-detect the Python app
115
+ 4. Set environment variable `MODEL_PATH` if needed
116
+ 5. Deploy
117
+
118
+ ### Option 4: AWS/GCP/Azure
119
+ For production deployments with more control.
120
+
121
+ ## ⚙️ Environment Variables
122
+
123
+ - `MODEL_PATH`: Path to your trained model (default: `./gpu_base_model2`)
124
+ - `PORT`: Server port (default: `8000`)
125
+
126
+ ## 🔒 Security Notes
127
+
128
+ ⚠️ **Important for Production:**
129
+ 1. Add authentication to your API endpoints
130
+ 2. Set proper CORS origins (not `*`)
131
+ 3. Add rate limiting
132
+ 4. Use HTTPS
133
+ 5. Keep your API key secure
134
+
135
+ ## 🐛 Troubleshooting
136
+
137
+ ### Model not loading
138
+ - Check that `gpu_base_model2` directory exists
139
+ - Verify all model files are present
140
+ - Check console logs for specific errors
141
+
142
+ ### Out of memory
143
+ - Reduce `max_length` in the generate function
144
+ - Use smaller batch sizes
145
+ - Consider using CPU instead of GPU
146
+
147
+ ### Slow inference
148
+ - Use GPU if available
149
+ - Reduce `num_beams` parameter
150
+ - Use quantization for faster inference
151
+
152
+ ## 📊 Usage
153
+
154
+ This API is designed to be called from an iOS app for correcting OCR text. The typical flow is:
155
+
156
+ 1. User takes/selects an image
157
+ 2. OCR extracts text from the image
158
+ 3. Extracted text is sent to this API
159
+ 4. API corrects the text using the trained model
160
+ 5. Corrected text is returned to the app
161
+
162
+ ## 🤝 Contributing
163
+
164
+ This is a private project for text correction. For questions or issues, please contact the project owner.
app.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ model_path = os.getenv("MODEL_PATH", "./gpu_base_model2")
62
+ print(f"📦 Loading model from: {model_path}")
63
+
64
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
65
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
66
+
67
+ # Move model to device
68
+ model.to(device)
69
+ model.eval()
70
+
71
+ print("✅ Model loaded successfully!")
72
+ print(f" - Model type: {type(model).__name__}")
73
+ print(f" - Vocabulary size: {tokenizer.vocab_size}")
74
+ print(f" - Device: {device}")
75
+
76
+ except Exception as e:
77
+ print(f"❌ Error loading model: {e}")
78
+ print("⚠️ API will not work until model is loaded")
79
+
80
+ # Health check endpoint
81
+ @app.get("/health", response_model=HealthResponse)
82
+ async def health_check():
83
+ """Check if the API and model are ready"""
84
+ return HealthResponse(
85
+ status="healthy" if model is not None else "unhealthy",
86
+ model_loaded=model is not None,
87
+ device=device or "unknown"
88
+ )
89
+
90
+ # Text correction endpoint
91
+ @app.post("/correct", response_model=TextResponse)
92
+ async def correct_text(request: TextRequest):
93
+ """
94
+ Correct text using the trained model
95
+
96
+ Args:
97
+ request: TextRequest containing the text to correct
98
+
99
+ Returns:
100
+ TextResponse with corrected text and processing time
101
+ """
102
+ import time
103
+
104
+ if model is None or tokenizer is None:
105
+ raise HTTPException(
106
+ status_code=503,
107
+ detail="Model not loaded. Please wait for the model to initialize."
108
+ )
109
+
110
+ if not request.text or not request.text.strip():
111
+ raise HTTPException(
112
+ status_code=400,
113
+ detail="Text cannot be empty"
114
+ )
115
+
116
+ start_time = time.time()
117
+
118
+ try:
119
+ # Tokenize input text
120
+ inputs = tokenizer(
121
+ request.text,
122
+ return_tensors="pt",
123
+ max_length=512,
124
+ truncation=True,
125
+ padding=True
126
+ ).to(device)
127
+
128
+ # Generate corrected text
129
+ with torch.no_grad():
130
+ outputs = model.generate(
131
+ inputs.input_ids,
132
+ attention_mask=inputs.attention_mask,
133
+ max_length=512,
134
+ num_beams=5,
135
+ early_stopping=True,
136
+ pad_token_id=tokenizer.pad_token_id,
137
+ eos_token_id=tokenizer.eos_token_id
138
+ )
139
+
140
+ # Decode output
141
+ corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
142
+
143
+ processing_time = time.time() - start_time
144
+
145
+ print(f"✅ Text corrected in {processing_time:.2f}s")
146
+ print(f" Input: {request.text[:50]}...")
147
+ print(f" Output: {corrected_text[:50]}...")
148
+
149
+ return TextResponse(
150
+ corrected_text=corrected_text,
151
+ processing_time=round(processing_time, 2)
152
+ )
153
+
154
+ except Exception as e:
155
+ print(f"❌ Error during correction: {e}")
156
+ raise HTTPException(
157
+ status_code=500,
158
+ detail=f"Text correction failed: {str(e)}"
159
+ )
160
+
161
+ # Root endpoint
162
+ @app.get("/")
163
+ async def root():
164
+ return {
165
+ "message": "Text Correction API",
166
+ "version": "1.0.0",
167
+ "endpoints": {
168
+ "health": "/health",
169
+ "correct": "/correct (POST)"
170
+ }
171
+ }
172
+
173
+ if __name__ == "__main__":
174
+ import uvicorn
175
+ uvicorn.run(app, host="0.0.0.0", port=8000)
176
+
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
+