wjbmattingly commited on
Commit
462a9b2
·
1 Parent(s): 0d4066e

using api and gpu

Browse files
Files changed (1) hide show
  1. app.py +14 -9
app.py CHANGED
@@ -3,10 +3,12 @@ import torch
3
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
4
  import subprocess
5
  import json
 
6
  from PIL import Image, ImageDraw
7
  import os
8
  import tempfile
9
  import numpy as np
 
10
 
11
  # Dictionary of model names and their corresponding HuggingFace model IDs
12
  MODEL_OPTIONS = {
@@ -35,19 +37,21 @@ def load_model(model_name):
35
  return processor, model
36
 
37
  def detect_lines(image_path):
 
 
38
  # Run Kraken for line detection
39
  lines_json_path = "lines.json"
40
- kraken_command = f"kraken -i {image_path} {lines_json_path} segment -bl"
41
- subprocess.run(kraken_command, shell=True, check=True)
42
 
43
- # Load the lines from the JSON file
44
- with open(lines_json_path, 'r') as f:
45
- lines_data = json.load(f)
46
 
47
- # Clean up temporary file
48
- os.unlink(lines_json_path)
 
49
 
50
- return lines_data['lines']
51
 
52
  def extract_line_images(image, lines):
53
  line_images = []
@@ -88,6 +92,7 @@ def visualize_lines(image, lines):
88
  draw.polygon(polygon, outline="red")
89
  return output_image
90
 
 
91
  def transcribe_lines(line_images, model_name):
92
  processor, model = load_model(model_name)
93
 
@@ -152,4 +157,4 @@ with gr.Blocks() as iface:
152
  outputs=[output_image, output_text]
153
  )
154
 
155
- iface.launch()
 
3
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
4
  import subprocess
5
  import json
6
+ import spaces
7
  from PIL import Image, ImageDraw
8
  import os
9
  import tempfile
10
  import numpy as np
11
+ import requests
12
 
13
  # Dictionary of model names and their corresponding HuggingFace model IDs
14
  MODEL_OPTIONS = {
 
37
  return processor, model
38
 
39
  def detect_lines(image_path):
40
+ # API endpoint
41
+ url = "https://wjbmattingly-kraken-api.hf.space/detect_lines"
42
  # Run Kraken for line detection
43
  lines_json_path = "lines.json"
44
+ # Prepare the file for upload
45
+ files = {'file': ('ms.jpg', open(image_path, 'rb'), 'image/jpeg')}
46
 
47
+ # Specify the model to use
48
+ data = {'model_name': 'catmus-medieval.mlmodel'}
 
49
 
50
+ # Send the POST request
51
+ response = requests.post(url, files=files, data=data)
52
+ result = response.json()["result"]["lines"]
53
 
54
+ return result
55
 
56
  def extract_line_images(image, lines):
57
  line_images = []
 
92
  draw.polygon(polygon, outline="red")
93
  return output_image
94
 
95
+ @spaces.GPU
96
  def transcribe_lines(line_images, model_name):
97
  processor, model = load_model(model_name)
98
 
 
157
  outputs=[output_image, output_text]
158
  )
159
 
160
+ iface.launch(debug=True)