Update app.py
Browse files
app.py
CHANGED
|
@@ -79,7 +79,7 @@ def create_docx(blocks, output_path):
|
|
| 79 |
def convert_image_to_docx(image):
|
| 80 |
"""Main conversion function"""
|
| 81 |
if image is None:
|
| 82 |
-
return None, "
|
| 83 |
|
| 84 |
try:
|
| 85 |
# Load model
|
|
@@ -107,14 +107,14 @@ def convert_image_to_docx(image):
|
|
| 107 |
})
|
| 108 |
|
| 109 |
if not all_blocks:
|
| 110 |
-
return None, "
|
| 111 |
|
| 112 |
# Create output file
|
| 113 |
output_path = tempfile.mktemp(suffix=".docx")
|
| 114 |
create_docx(all_blocks, output_path)
|
| 115 |
|
| 116 |
# Create preview text
|
| 117 |
-
preview = f"
|
| 118 |
preview += "--- Content Preview ---\n\n"
|
| 119 |
for i, block in enumerate(all_blocks[:15], 1):
|
| 120 |
content_preview = block['content'][:100]
|
|
@@ -129,7 +129,7 @@ def convert_image_to_docx(image):
|
|
| 129 |
return output_path, preview
|
| 130 |
|
| 131 |
except Exception as e:
|
| 132 |
-
return None, f"
|
| 133 |
|
| 134 |
# Custom CSS for better styling
|
| 135 |
custom_css = """
|
|
@@ -142,12 +142,12 @@ custom_css = """
|
|
| 142 |
"""
|
| 143 |
|
| 144 |
# Create Gradio interface
|
| 145 |
-
with gr.Blocks(css=custom_css, title="
|
| 146 |
gr.Markdown(
|
| 147 |
"""
|
| 148 |
-
#
|
| 149 |
|
| 150 |
-
Upload an image of your
|
| 151 |
|
| 152 |
**Supported formats:** JPG, JPEG, PNG, BMP, WEBP
|
| 153 |
"""
|
|
@@ -156,23 +156,23 @@ with gr.Blocks(css=custom_css, title="Chemistry Notes to DOCX") as demo:
|
|
| 156 |
with gr.Row():
|
| 157 |
with gr.Column(scale=1):
|
| 158 |
input_image = gr.Image(
|
| 159 |
-
label="
|
| 160 |
type="pil"
|
| 161 |
)
|
| 162 |
convert_btn = gr.Button(
|
| 163 |
-
"
|
| 164 |
variant="primary"
|
| 165 |
)
|
| 166 |
|
| 167 |
with gr.Column(scale=1):
|
| 168 |
output_preview = gr.Textbox(
|
| 169 |
-
label="
|
| 170 |
lines=15,
|
| 171 |
max_lines=20,
|
| 172 |
interactive=False
|
| 173 |
)
|
| 174 |
output_file = gr.File(
|
| 175 |
-
label="
|
| 176 |
file_count="single"
|
| 177 |
)
|
| 178 |
|
|
@@ -185,7 +185,7 @@ with gr.Blocks(css=custom_css, title="Chemistry Notes to DOCX") as demo:
|
|
| 185 |
|
| 186 |
gr.Markdown("---")
|
| 187 |
|
| 188 |
-
with gr.Accordion("
|
| 189 |
gr.Markdown(
|
| 190 |
"""
|
| 191 |
- **Use clear, high-resolution images** - Higher quality images produce better results
|
|
|
|
| 79 |
def convert_image_to_docx(image):
|
| 80 |
"""Main conversion function"""
|
| 81 |
if image is None:
|
| 82 |
+
return None, "Please upload an image first!"
|
| 83 |
|
| 84 |
try:
|
| 85 |
# Load model
|
|
|
|
| 107 |
})
|
| 108 |
|
| 109 |
if not all_blocks:
|
| 110 |
+
return None, "No text could be extracted from the image! Please try with a clearer image."
|
| 111 |
|
| 112 |
# Create output file
|
| 113 |
output_path = tempfile.mktemp(suffix=".docx")
|
| 114 |
create_docx(all_blocks, output_path)
|
| 115 |
|
| 116 |
# Create preview text
|
| 117 |
+
preview = f"Successfully extracted {len(all_blocks)} text blocks!\n\n"
|
| 118 |
preview += "--- Content Preview ---\n\n"
|
| 119 |
for i, block in enumerate(all_blocks[:15], 1):
|
| 120 |
content_preview = block['content'][:100]
|
|
|
|
| 129 |
return output_path, preview
|
| 130 |
|
| 131 |
except Exception as e:
|
| 132 |
+
return None, f"Error during conversion: {str(e)}"
|
| 133 |
|
| 134 |
# Custom CSS for better styling
|
| 135 |
custom_css = """
|
|
|
|
| 142 |
"""
|
| 143 |
|
| 144 |
# Create Gradio interface
|
| 145 |
+
with gr.Blocks(css=custom_css, title="Image to DOCX") as demo:
|
| 146 |
gr.Markdown(
|
| 147 |
"""
|
| 148 |
+
# Image to DOCX Converter
|
| 149 |
|
| 150 |
+
Upload an image of your handwritten text and get a formatted Word document!
|
| 151 |
|
| 152 |
**Supported formats:** JPG, JPEG, PNG, BMP, WEBP
|
| 153 |
"""
|
|
|
|
| 156 |
with gr.Row():
|
| 157 |
with gr.Column(scale=1):
|
| 158 |
input_image = gr.Image(
|
| 159 |
+
label="Upload Chemistry Notes Image",
|
| 160 |
type="pil"
|
| 161 |
)
|
| 162 |
convert_btn = gr.Button(
|
| 163 |
+
"Convert to DOCX",
|
| 164 |
variant="primary"
|
| 165 |
)
|
| 166 |
|
| 167 |
with gr.Column(scale=1):
|
| 168 |
output_preview = gr.Textbox(
|
| 169 |
+
label="Extraction Preview",
|
| 170 |
lines=15,
|
| 171 |
max_lines=20,
|
| 172 |
interactive=False
|
| 173 |
)
|
| 174 |
output_file = gr.File(
|
| 175 |
+
label="Download DOCX",
|
| 176 |
file_count="single"
|
| 177 |
)
|
| 178 |
|
|
|
|
| 185 |
|
| 186 |
gr.Markdown("---")
|
| 187 |
|
| 188 |
+
with gr.Accordion("Tips for Best Results", open=False):
|
| 189 |
gr.Markdown(
|
| 190 |
"""
|
| 191 |
- **Use clear, high-resolution images** - Higher quality images produce better results
|