Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flask endpoint in transcriptions folder #11

Merged
merged 3 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added transcription/.gitignore
Empty file.
40 changes: 40 additions & 0 deletions transcription/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from flask import Flask, request, jsonify
from PIL import Image
from transformers import AutoProcessor, AutoModelForCausalLM
import torch

app = Flask(__name__)

# load model and processor once during init
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True)

@app.route("/transcribe", methods=["POST"])
def transcribe():
if "image" not in request.files:
return jsonify({"error": "No image file provided"}), 400

image_file = request.files["image"]
try:
# open and preprocess image
image = Image.open(image_file).convert("RGB")
prompt = "<OCR>"
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
generated_ids = model.generate(
input_ids=inputs["input_ids"],
pixel_values=inputs["pixel_values"],
max_new_tokens=1024,
num_beams=3,
do_sample=False
)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]

return jsonify({"transcription": generated_text})
except Exception as e:
return jsonify({"error": str(e)}), 500

if __name__ == "__main__":
app.run(debug=True)
2 changes: 2 additions & 0 deletions transcription/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ torch==2.5.1
transformers==4.46.1
einops
timm
flask
flask-cors
2 changes: 1 addition & 1 deletion transcription/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def florence():

prompt = "<OCR>"

url = "../assets/kkl.jpg"
url = "../assets/Filled_Logbook_page-0001.jpg"
image = Image.open(url).convert("RGB")

inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
Expand Down
Loading