Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,43 @@
|
|
| 1 |
-
from flask import Flask
|
| 2 |
-
|
| 3 |
-
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
model
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def ocr_infer(prompt, image):
|
| 18 |
-
if image is None:
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
demo = gr.Interface(
|
| 23 |
fn=ocr_infer,
|
| 24 |
inputs=[gr.Textbox(label="Prompt"), gr.Image(label="Upload Image")],
|
| 25 |
outputs="text",
|
| 26 |
-
title="RolmOCR
|
| 27 |
-
description="Runs
|
| 28 |
)
|
| 29 |
|
| 30 |
-
app = Flask(__name__)
|
| 31 |
-
|
| 32 |
-
@app.route("/")
|
| 33 |
-
def index():
|
| 34 |
-
return send_from_directory(".", "index.html")
|
| 35 |
-
|
| 36 |
if __name__ == "__main__":
|
| 37 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
from flask import Flask
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
import base64, io
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
+
print("Loading RolmOCR (Q4 GGUF) via llama‑cpp...")
|
| 8 |
+
model_path = "/home/user/.cache/huggingface/hub/models--Nasa1423--RolmOCR-Q4_K_M-GGUF/blobs/3831d4a42b3a054bbe0a6634e8ee5bb24d275d6104a39250cffeecbb1bbc3d19"
|
| 9 |
|
| 10 |
+
# if llama‑cpp cannot find the model automatically, use the HF download path like above,
|
| 11 |
+
# or rely on repo_id below:
|
| 12 |
+
try:
|
| 13 |
+
llm = Llama.from_pretrained(
|
| 14 |
+
repo_id="Nasa1423/RolmOCR-Q4_K_M-GGUF",
|
| 15 |
+
filename="rolmocr-q4_k_m.gguf",
|
| 16 |
+
n_ctx=2048,
|
| 17 |
+
n_threads=4,
|
| 18 |
+
)
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print("Auto load failed", e)
|
| 21 |
+
# fallback: load local path explicitly
|
| 22 |
+
llm = Llama(model_path=model_path, n_ctx=2048, n_threads=4)
|
| 23 |
|
| 24 |
def ocr_infer(prompt, image):
|
| 25 |
+
if image is None:
|
| 26 |
+
return "(No image provided)"
|
| 27 |
+
# image validation only — the model processes text prompt
|
| 28 |
+
Image.fromarray(image)
|
| 29 |
+
q = f"You are an OCR system.\n{prompt}\nRead the uploaded image and output markdown text."
|
| 30 |
+
output = llm(q, max_tokens=512, temperature=0.1)
|
| 31 |
+
# llama‑cpp returns a dict with 'choices'
|
| 32 |
+
return output["choices"][0]["text"].strip()
|
| 33 |
|
| 34 |
demo = gr.Interface(
|
| 35 |
fn=ocr_infer,
|
| 36 |
inputs=[gr.Textbox(label="Prompt"), gr.Image(label="Upload Image")],
|
| 37 |
outputs="text",
|
| 38 |
+
title="RolmOCR GGUF (LLAMA‑CPP)",
|
| 39 |
+
description="Runs RolmOCR Q4_K_M on CPU via llama‑cpp‑python; fully Space compatible.",
|
| 40 |
)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
if __name__ == "__main__":
|
| 43 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|