Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import easyocr
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
import os, json, re
|
| 5 |
+
from PIL import Image, ImageEnhance, ImageFilter
|
| 6 |
+
|
| 7 |
+
# π API key
|
| 8 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 9 |
+
|
| 10 |
+
# OCR
|
| 11 |
+
reader = easyocr.Reader(['en'])
|
| 12 |
+
model = genai.GenerativeModel("gemini-3-flash-preview")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def preprocess_image(path):
|
| 16 |
+
img = Image.open(path).convert("L")
|
| 17 |
+
img = ImageEnhance.Contrast(img).enhance(2)
|
| 18 |
+
img = img.filter(ImageFilter.SHARPEN)
|
| 19 |
+
return img
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def extract_text(image):
|
| 23 |
+
result = reader.readtext(image, detail=0)
|
| 24 |
+
text = "\n".join(result)
|
| 25 |
+
|
| 26 |
+
text = re.sub(r'\.{2,}', ' ', text)
|
| 27 |
+
text = re.sub(r'\s+', ' ', text)
|
| 28 |
+
|
| 29 |
+
return text
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def build_prompt(text):
|
| 33 |
+
return f"""
|
| 34 |
+
You are a strict JSON generator.
|
| 35 |
+
|
| 36 |
+
Convert menu text into VALID JSON.
|
| 37 |
+
|
| 38 |
+
RULES:
|
| 39 |
+
- Output ONLY JSON
|
| 40 |
+
- Detect categories
|
| 41 |
+
- Each item has "name" and "sizes"
|
| 42 |
+
- 1 price β Regular
|
| 43 |
+
- 2 prices β infer meaning
|
| 44 |
+
- Fix OCR errors
|
| 45 |
+
|
| 46 |
+
MENU:
|
| 47 |
+
{text}
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def extract_json(text):
|
| 52 |
+
try:
|
| 53 |
+
start = text.index("{")
|
| 54 |
+
end = text.rindex("}") + 1
|
| 55 |
+
return json.loads(text[start:end])
|
| 56 |
+
except:
|
| 57 |
+
return None
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def validate_schema(data):
|
| 61 |
+
if not data or "categories" not in data:
|
| 62 |
+
return False
|
| 63 |
+
return True
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def generate_json(text):
|
| 67 |
+
for _ in range(3):
|
| 68 |
+
prompt = build_prompt(text)
|
| 69 |
+
response = model.generate_content(prompt)
|
| 70 |
+
|
| 71 |
+
parsed = extract_json(response.text)
|
| 72 |
+
|
| 73 |
+
if validate_schema(parsed):
|
| 74 |
+
return parsed
|
| 75 |
+
|
| 76 |
+
return {"error": "Failed to generate valid JSON"}
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def process(image):
|
| 80 |
+
img = preprocess_image(image)
|
| 81 |
+
text = extract_text(img)
|
| 82 |
+
result = generate_json(text)
|
| 83 |
+
|
| 84 |
+
return json.dumps(result, indent=2)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
app = gr.Interface(
|
| 88 |
+
fn=process,
|
| 89 |
+
inputs=gr.Image(type="filepath"),
|
| 90 |
+
outputs=gr.Code(language="json"),
|
| 91 |
+
title="π Production Menu Parser (OCR + Gemini)",
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
app.launch()
|