Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ import gradio as gr
|
|
| 7 |
from PIL import Image
|
| 8 |
from io import BytesIO
|
| 9 |
|
| 10 |
-
# Get environment variables from HF Spaces secrets
|
| 11 |
ENDPOINT = os.environ.get("VLLM_ENDPOINT")
|
| 12 |
MODEL = os.environ.get("VLLM_MODEL")
|
| 13 |
|
|
@@ -16,34 +15,18 @@ if not ENDPOINT or not MODEL:
|
|
| 16 |
|
| 17 |
|
| 18 |
def image_to_base64(image):
|
| 19 |
-
"""Convert PIL Image to base64 string."""
|
| 20 |
buffered = BytesIO()
|
| 21 |
image.save(buffered, format="PNG")
|
| 22 |
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 23 |
|
| 24 |
|
| 25 |
-
def convert_latex_delimiters(text):
|
| 26 |
-
"""Convert [ ... ] LaTeX delimiters to $ ... $ for Gradio rendering."""
|
| 27 |
-
import re
|
| 28 |
-
|
| 29 |
-
# Replace display math: [ ... ] -> $ ... $
|
| 30 |
-
text = re.sub(r'\[\s*(.*?)\s*\]', r'$\1$', text, flags=re.DOTALL)
|
| 31 |
-
|
| 32 |
-
return text
|
| 33 |
-
|
| 34 |
-
|
| 35 |
def process_image(image, temperature):
|
| 36 |
-
"""
|
| 37 |
-
Send image to vLLM endpoint and stream the response.
|
| 38 |
-
"""
|
| 39 |
if image is None:
|
| 40 |
yield "Please upload an image first.", ""
|
| 41 |
return
|
| 42 |
|
| 43 |
-
# Convert image to base64
|
| 44 |
b64_image = image_to_base64(image)
|
| 45 |
|
| 46 |
-
# Build the payload with only image input (no text prompt)
|
| 47 |
payload = {
|
| 48 |
"model": MODEL,
|
| 49 |
"messages": [
|
|
@@ -74,7 +57,7 @@ def process_image(image, temperature):
|
|
| 74 |
if line:
|
| 75 |
line = line.decode('utf-8')
|
| 76 |
if line.startswith('data: '):
|
| 77 |
-
line = line[6:]
|
| 78 |
|
| 79 |
if line.strip() == '[DONE]':
|
| 80 |
break
|
|
@@ -86,17 +69,15 @@ def process_image(image, temperature):
|
|
| 86 |
content = delta.get('content', '')
|
| 87 |
if content:
|
| 88 |
accumulated_response += content
|
| 89 |
-
|
| 90 |
-
rendered_text = convert_latex_delimiters(accumulated_response)
|
| 91 |
-
yield rendered_text, accumulated_response
|
| 92 |
except json.JSONDecodeError:
|
| 93 |
continue
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
-
# Build the Gradio Interface
|
| 100 |
with gr.Blocks(title="π Image OCR", theme=gr.themes.Soft()) as demo:
|
| 101 |
gr.Markdown(
|
| 102 |
"""
|
|
@@ -116,7 +97,7 @@ with gr.Blocks(title="π Image OCR", theme=gr.themes.Soft()) as demo:
|
|
| 116 |
type="pil",
|
| 117 |
label="πΌοΈ Upload Image",
|
| 118 |
sources=["upload", "clipboard"],
|
| 119 |
-
height=
|
| 120 |
)
|
| 121 |
temperature = gr.Slider(
|
| 122 |
minimum=0.1,
|
|
@@ -131,8 +112,8 @@ with gr.Blocks(title="π Image OCR", theme=gr.themes.Soft()) as demo:
|
|
| 131 |
with gr.Column():
|
| 132 |
output_text = gr.Markdown(
|
| 133 |
label="π Extracted Text (Rendered)",
|
| 134 |
-
value="<div style='min-height:
|
| 135 |
-
height=
|
| 136 |
)
|
| 137 |
|
| 138 |
with gr.Row():
|
|
@@ -140,11 +121,10 @@ with gr.Blocks(title="π Image OCR", theme=gr.themes.Soft()) as demo:
|
|
| 140 |
raw_output = gr.Textbox(
|
| 141 |
label="Raw Markdown Output",
|
| 142 |
placeholder="Raw text will appear here...",
|
| 143 |
-
lines=
|
| 144 |
show_copy_button=True
|
| 145 |
)
|
| 146 |
|
| 147 |
-
# Event handlers
|
| 148 |
submit_btn.click(
|
| 149 |
fn=process_image,
|
| 150 |
inputs=[image_input, temperature],
|
|
@@ -155,11 +135,6 @@ with gr.Blocks(title="π Image OCR", theme=gr.themes.Soft()) as demo:
|
|
| 155 |
fn=lambda: (None, "", ""),
|
| 156 |
outputs=[image_input, output_text, raw_output]
|
| 157 |
)
|
| 158 |
-
|
| 159 |
-
gr.Markdown("""
|
| 160 |
-
---
|
| 161 |
-
**Note:** Configure endpoint via `VLLM_ENDPOINT` and `VLLM_MODEL` environment variables.
|
| 162 |
-
""")
|
| 163 |
|
| 164 |
|
| 165 |
if __name__ == "__main__":
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
from io import BytesIO
|
| 9 |
|
|
|
|
| 10 |
ENDPOINT = os.environ.get("VLLM_ENDPOINT")
|
| 11 |
MODEL = os.environ.get("VLLM_MODEL")
|
| 12 |
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
def image_to_base64(image):
|
|
|
|
| 18 |
buffered = BytesIO()
|
| 19 |
image.save(buffered, format="PNG")
|
| 20 |
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 21 |
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def process_image(image, temperature):
|
|
|
|
|
|
|
|
|
|
| 24 |
if image is None:
|
| 25 |
yield "Please upload an image first.", ""
|
| 26 |
return
|
| 27 |
|
|
|
|
| 28 |
b64_image = image_to_base64(image)
|
| 29 |
|
|
|
|
| 30 |
payload = {
|
| 31 |
"model": MODEL,
|
| 32 |
"messages": [
|
|
|
|
| 57 |
if line:
|
| 58 |
line = line.decode('utf-8')
|
| 59 |
if line.startswith('data: '):
|
| 60 |
+
line = line[6:]
|
| 61 |
|
| 62 |
if line.strip() == '[DONE]':
|
| 63 |
break
|
|
|
|
| 69 |
content = delta.get('content', '')
|
| 70 |
if content:
|
| 71 |
accumulated_response += content
|
| 72 |
+
yield accumulated_response, accumulated_response
|
|
|
|
|
|
|
| 73 |
except json.JSONDecodeError:
|
| 74 |
continue
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
+
error_msg = f"Error: {str(e)}"
|
| 78 |
+
yield error_msg, error_msg
|
| 79 |
|
| 80 |
|
|
|
|
| 81 |
with gr.Blocks(title="π Image OCR", theme=gr.themes.Soft()) as demo:
|
| 82 |
gr.Markdown(
|
| 83 |
"""
|
|
|
|
| 97 |
type="pil",
|
| 98 |
label="πΌοΈ Upload Image",
|
| 99 |
sources=["upload", "clipboard"],
|
| 100 |
+
height=600
|
| 101 |
)
|
| 102 |
temperature = gr.Slider(
|
| 103 |
minimum=0.1,
|
|
|
|
| 112 |
with gr.Column():
|
| 113 |
output_text = gr.Markdown(
|
| 114 |
label="π Extracted Text (Rendered)",
|
| 115 |
+
value="<div style='min-height: 600px; padding: 10px; border: 1px solid #e0e0e0; border-radius: 4px; background-color: #f9f9f9;'><em>Extracted text will appear here...</em></div>",
|
| 116 |
+
height=600
|
| 117 |
)
|
| 118 |
|
| 119 |
with gr.Row():
|
|
|
|
| 121 |
raw_output = gr.Textbox(
|
| 122 |
label="Raw Markdown Output",
|
| 123 |
placeholder="Raw text will appear here...",
|
| 124 |
+
lines=30,
|
| 125 |
show_copy_button=True
|
| 126 |
)
|
| 127 |
|
|
|
|
| 128 |
submit_btn.click(
|
| 129 |
fn=process_image,
|
| 130 |
inputs=[image_input, temperature],
|
|
|
|
| 135 |
fn=lambda: (None, "", ""),
|
| 136 |
outputs=[image_input, output_text, raw_output]
|
| 137 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
|
| 140 |
if __name__ == "__main__":
|