Shivangguptasih commited on
Commit
246a717
·
verified ·
1 Parent(s): 9c0aea5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -23
app.py CHANGED
@@ -1,37 +1,43 @@
1
- from flask import Flask, send_from_directory
2
- from ctransformers import AutoModelForCausalLM
3
- import gradio as gr, io, base64
 
4
  from PIL import Image
5
 
6
- MODEL_REPO = "Nasa1423/RolmOCR-Q4_K_M-GGUF"
7
- print("Loading RolmOCRGGUF on CPU…")
8
 
9
- model = AutoModelForCausalLM.from_pretrained(
10
- MODEL_REPO,
11
- model_type="llama",
12
- context_length=2048,
13
- gpu_layers=0,
14
- lib="basic",
15
- )
 
 
 
 
 
 
16
 
17
  def ocr_infer(prompt, image):
18
- if image is None: return "(No image given)"
19
- _ = Image.fromarray(image)
20
- return model(f"{prompt}\nConvert image to markdown text.", max_new_tokens=512)
 
 
 
 
 
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="RolmOCRGGUF CPU Space",
27
- description="Runs quantized RolmOCR (Q4_K_M)locally on HF CPU infrastructure.",
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="RolmOCRGGUF (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)