Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
import io
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
# ==========================================
|
| 9 |
+
# Initialize Flask App
|
| 10 |
+
# ==========================================
|
| 11 |
+
app = Flask(__name__)
|
| 12 |
+
|
| 13 |
+
# ==========================================
|
| 14 |
+
# Load Model and Processor
|
| 15 |
+
# ==========================================
|
| 16 |
+
MODEL_NAME = "anuashok/ocr-captcha-v3"
|
| 17 |
+
print("🚀 Loading model...")
|
| 18 |
+
|
| 19 |
+
processor = TrOCRProcessor.from_pretrained(MODEL_NAME)
|
| 20 |
+
model = VisionEncoderDecoderModel.from_pretrained(MODEL_NAME)
|
| 21 |
+
|
| 22 |
+
# Use GPU if available
|
| 23 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
+
model.to(device)
|
| 25 |
+
model.eval()
|
| 26 |
+
|
| 27 |
+
print("✅ Model loaded successfully on", device)
|
| 28 |
+
|
| 29 |
+
# ==========================================
|
| 30 |
+
# API Endpoint (Root)
|
| 31 |
+
# ==========================================
|
| 32 |
+
@app.route("/", methods=["GET", "POST"])
|
| 33 |
+
def root():
|
| 34 |
+
"""
|
| 35 |
+
POST / -> Send image file
|
| 36 |
+
GET /?url=image_url -> Send image URL
|
| 37 |
+
Returns: JSON with recognized text
|
| 38 |
+
"""
|
| 39 |
+
try:
|
| 40 |
+
# ---------------------------
|
| 41 |
+
# Handle GET with ?url=
|
| 42 |
+
# ---------------------------
|
| 43 |
+
if request.method == "GET":
|
| 44 |
+
image_url = request.args.get("url")
|
| 45 |
+
if not image_url:
|
| 46 |
+
return jsonify({
|
| 47 |
+
"message": "OCR Captcha API running. Use POST (file) or GET ?url=image_url."
|
| 48 |
+
})
|
| 49 |
+
try:
|
| 50 |
+
img_data = requests.get(image_url, timeout=10).content
|
| 51 |
+
image = Image.open(io.BytesIO(img_data)).convert("RGB")
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return jsonify({"error": f"Failed to fetch image from URL: {str(e)}"}), 400
|
| 54 |
+
|
| 55 |
+
# ---------------------------
|
| 56 |
+
# Handle POST with file upload
|
| 57 |
+
# ---------------------------
|
| 58 |
+
elif request.method == "POST":
|
| 59 |
+
if "file" not in request.files:
|
| 60 |
+
return jsonify({"error": "No file provided"}), 400
|
| 61 |
+
|
| 62 |
+
file = request.files["file"]
|
| 63 |
+
image = Image.open(io.BytesIO(file.read())).convert("RGB")
|
| 64 |
+
|
| 65 |
+
# ---------------------------
|
| 66 |
+
# Process Image with Model
|
| 67 |
+
# ---------------------------
|
| 68 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values.to(device)
|
| 69 |
+
with torch.no_grad():
|
| 70 |
+
generated_ids = model.generate(pixel_values)
|
| 71 |
+
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 72 |
+
|
| 73 |
+
return jsonify({"text": text})
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return jsonify({"error": str(e)}), 500
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# ==========================================
|
| 80 |
+
# Run Server
|
| 81 |
+
# ==========================================
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
# Host on 0.0.0.0 so Hugging Face Space can reach it
|
| 84 |
+
app.run(host="0.0.0.0", port=7860)
|