Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,48 +14,15 @@ import os
|
|
| 14 |
|
| 15 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 16 |
|
| 17 |
-
model_dir = 'medieval-data/florence2-medieval-bbox-line-detection'
|
| 18 |
model_dir = "medieval-data/florence2-medieval-bbox-zone-detection"
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
with patch("transformers.dynamic_module_utils.get_imports", fixed_get_imports):
|
| 29 |
-
# Load the configuration
|
| 30 |
-
config = AutoConfig.from_pretrained(model_dir, trust_remote_code=True)
|
| 31 |
-
|
| 32 |
-
# Modify the vision configuration
|
| 33 |
-
if hasattr(config, 'vision_config'):
|
| 34 |
-
config.vision_config.model_type = 'davit'
|
| 35 |
-
|
| 36 |
-
print("Modified vision configuration:")
|
| 37 |
-
print(config.vision_config)
|
| 38 |
-
|
| 39 |
-
# Try to load the model with the modified configuration
|
| 40 |
-
try:
|
| 41 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 42 |
-
model_dir,
|
| 43 |
-
config=config,
|
| 44 |
-
trust_remote_code=True
|
| 45 |
-
)
|
| 46 |
-
print("Model loaded successfully!")
|
| 47 |
-
except Exception as e:
|
| 48 |
-
print(f"Failed to load model: {str(e)}")
|
| 49 |
-
|
| 50 |
-
# Load the processor without specifying a revision
|
| 51 |
-
try:
|
| 52 |
-
processor = AutoProcessor.from_pretrained(
|
| 53 |
-
model_dir,
|
| 54 |
-
trust_remote_code=True
|
| 55 |
-
)
|
| 56 |
-
print("Processor loaded successfully!")
|
| 57 |
-
except Exception as e:
|
| 58 |
-
print(f"Failed to load processor: {str(e)}")
|
| 59 |
TITLE = "# [Florence-2- Medieval Manuscript Layout Parsing Demo](https://huggingface.co/medieval-data/florence2-medieval-bbox-zone-detection)"
|
| 60 |
DESCRIPTION = "The demo for Florence-2 fine-tuned on CATMuS Segmentation Dataset. This app has two models: one for line detection and one for zone detection."
|
| 61 |
|
|
@@ -63,7 +30,7 @@ DESCRIPTION = "The demo for Florence-2 fine-tuned on CATMuS Segmentation Dataset
|
|
| 63 |
colormap = plt.cm.get_cmap('tab20')
|
| 64 |
|
| 65 |
@spaces.GPU
|
| 66 |
-
def process_image(image
|
| 67 |
max_size = 1000
|
| 68 |
prompt = "<OD>"
|
| 69 |
|
|
@@ -111,14 +78,14 @@ def visualize_bboxes(result, image):
|
|
| 111 |
plt.axis('off')
|
| 112 |
return fig
|
| 113 |
|
| 114 |
-
def run_example(image
|
| 115 |
if isinstance(image, str): # If image is a URL
|
| 116 |
response = requests.get(image)
|
| 117 |
image = Image.open(BytesIO(response.content))
|
| 118 |
elif isinstance(image, np.ndarray): # If image is a numpy array
|
| 119 |
image = Image.fromarray(image)
|
| 120 |
|
| 121 |
-
result, processed_image = process_image(image
|
| 122 |
fig = visualize_bboxes(result, processed_image)
|
| 123 |
|
| 124 |
# Convert matplotlib figure to image
|
|
@@ -128,15 +95,13 @@ def run_example(image, text_input=None):
|
|
| 128 |
output_image = Image.open(img_buf)
|
| 129 |
|
| 130 |
return output_image
|
| 131 |
-
|
| 132 |
css = """
|
| 133 |
#output {
|
| 134 |
-
height:
|
| 135 |
overflow: auto;
|
| 136 |
border: 1px solid #ccc;
|
| 137 |
}
|
| 138 |
"""
|
| 139 |
-
|
| 140 |
with gr.Blocks(css=css) as demo:
|
| 141 |
gr.Markdown(TITLE)
|
| 142 |
gr.Markdown(DESCRIPTION)
|
|
@@ -144,23 +109,19 @@ with gr.Blocks(css=css) as demo:
|
|
| 144 |
with gr.Row():
|
| 145 |
with gr.Column():
|
| 146 |
input_img = gr.Image(label="Input Picture")
|
| 147 |
-
text_input = gr.Textbox(label="Text Input (optional)")
|
| 148 |
submit_btn = gr.Button(value="Submit")
|
| 149 |
with gr.Column():
|
| 150 |
output_img = gr.Image(label="Output Image with Bounding Boxes")
|
| 151 |
gr.Examples(
|
| 152 |
examples=[
|
| 153 |
-
["https://huggingface.co/datasets/CATMuS/medieval-segmentation/resolve/main/data/dev/london-british-library-egerton-821/page-002-of-004.jpg"
|
| 154 |
-
["https://huggingface.co/datasets/CATMuS/medieval-segmentation/resolve/main/data/dev/paris-bnf-lat-12449/page-002-of-003.jpg", None],
|
| 155 |
-
["https://huggingface.co/datasets/CATMuS/medieval-segmentation/resolve/main/data/dev/paris-bnf-nal-1909/page-009-of-012.jpg", None],
|
| 156 |
-
["https://huggingface.co/datasets/CATMuS/medieval-segmentation/resolve/main/data/test/paris-bnf-fr-574/page-001-of-003.jpg", None]
|
| 157 |
],
|
| 158 |
-
inputs=[input_img
|
| 159 |
outputs=[output_img],
|
| 160 |
fn=run_example,
|
| 161 |
cache_examples=True,
|
| 162 |
label='Try the examples below'
|
| 163 |
)
|
| 164 |
-
submit_btn.click(run_example, [input_img
|
| 165 |
|
| 166 |
demo.launch(debug=True)
|
|
|
|
| 14 |
|
| 15 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 16 |
|
|
|
|
| 17 |
model_dir = "medieval-data/florence2-medieval-bbox-zone-detection"
|
| 18 |
|
| 19 |
+
# Load the configuration
|
| 20 |
+
config = AutoConfig.from_pretrained(model_dir, trust_remote_code=True)
|
| 21 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 22 |
+
model_dir,
|
| 23 |
+
trust_remote_code=True
|
| 24 |
+
)
|
| 25 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
TITLE = "# [Florence-2- Medieval Manuscript Layout Parsing Demo](https://huggingface.co/medieval-data/florence2-medieval-bbox-zone-detection)"
|
| 27 |
DESCRIPTION = "The demo for Florence-2 fine-tuned on CATMuS Segmentation Dataset. This app has two models: one for line detection and one for zone detection."
|
| 28 |
|
|
|
|
| 30 |
colormap = plt.cm.get_cmap('tab20')
|
| 31 |
|
| 32 |
@spaces.GPU
|
| 33 |
+
def process_image(image):
|
| 34 |
max_size = 1000
|
| 35 |
prompt = "<OD>"
|
| 36 |
|
|
|
|
| 78 |
plt.axis('off')
|
| 79 |
return fig
|
| 80 |
|
| 81 |
+
def run_example(image):
|
| 82 |
if isinstance(image, str): # If image is a URL
|
| 83 |
response = requests.get(image)
|
| 84 |
image = Image.open(BytesIO(response.content))
|
| 85 |
elif isinstance(image, np.ndarray): # If image is a numpy array
|
| 86 |
image = Image.fromarray(image)
|
| 87 |
|
| 88 |
+
result, processed_image = process_image(image)
|
| 89 |
fig = visualize_bboxes(result, processed_image)
|
| 90 |
|
| 91 |
# Convert matplotlib figure to image
|
|
|
|
| 95 |
output_image = Image.open(img_buf)
|
| 96 |
|
| 97 |
return output_image
|
|
|
|
| 98 |
css = """
|
| 99 |
#output {
|
| 100 |
+
height: 1000px;
|
| 101 |
overflow: auto;
|
| 102 |
border: 1px solid #ccc;
|
| 103 |
}
|
| 104 |
"""
|
|
|
|
| 105 |
with gr.Blocks(css=css) as demo:
|
| 106 |
gr.Markdown(TITLE)
|
| 107 |
gr.Markdown(DESCRIPTION)
|
|
|
|
| 109 |
with gr.Row():
|
| 110 |
with gr.Column():
|
| 111 |
input_img = gr.Image(label="Input Picture")
|
|
|
|
| 112 |
submit_btn = gr.Button(value="Submit")
|
| 113 |
with gr.Column():
|
| 114 |
output_img = gr.Image(label="Output Image with Bounding Boxes")
|
| 115 |
gr.Examples(
|
| 116 |
examples=[
|
| 117 |
+
["https://huggingface.co/datasets/CATMuS/medieval-segmentation/resolve/main/data/dev/london-british-library-egerton-821/page-002-of-004.jpg"],
|
|
|
|
|
|
|
|
|
|
| 118 |
],
|
| 119 |
+
inputs=[input_img],
|
| 120 |
outputs=[output_img],
|
| 121 |
fn=run_example,
|
| 122 |
cache_examples=True,
|
| 123 |
label='Try the examples below'
|
| 124 |
)
|
| 125 |
+
submit_btn.click(run_example, [input_img], [output_img])
|
| 126 |
|
| 127 |
demo.launch(debug=True)
|