Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,29 +1,40 @@
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def extract_order(ocr_text):
|
| 12 |
-
if not ocr_text.strip():
|
| 13 |
return "Error: Empty input"
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
demo = gr.Interface(
|
| 22 |
fn=extract_order,
|
| 23 |
-
inputs=gr.Textbox(label="OCR Text", lines=
|
| 24 |
-
outputs=gr.Textbox(label="
|
| 25 |
-
title="StackAdvisor
|
| 26 |
-
description="Extract delivery order info from OCR text"
|
| 27 |
)
|
| 28 |
|
| 29 |
demo.launch()
|
|
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# Global variables
|
| 7 |
+
model = None
|
| 8 |
+
tokenizer = None
|
| 9 |
+
|
| 10 |
+
def load_model():
|
| 11 |
+
global model, tokenizer
|
| 12 |
+
if model is None:
|
| 13 |
+
print("Loading model...")
|
| 14 |
+
tokenizer = T5Tokenizer.from_pretrained("younsmirzai/stackadvisor-extractor")
|
| 15 |
+
model = T5ForConditionalGeneration.from_pretrained("younsmirzai/stackadvisor-extractor")
|
| 16 |
+
print("✅ Model loaded!")
|
| 17 |
+
return model, tokenizer
|
| 18 |
|
| 19 |
def extract_order(ocr_text):
|
| 20 |
+
if not ocr_text or not ocr_text.strip():
|
| 21 |
return "Error: Empty input"
|
| 22 |
|
| 23 |
+
try:
|
| 24 |
+
m, t = load_model()
|
| 25 |
+
inputs = t(ocr_text, return_tensors="pt", max_length=256, truncation=True)
|
| 26 |
+
outputs = m.generate(**inputs, max_length=128)
|
| 27 |
+
result = t.decode(outputs[0], skip_special_tokens=True)
|
| 28 |
+
return result
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Error: {str(e)}"
|
| 31 |
|
| 32 |
+
# Simple interface
|
| 33 |
demo = gr.Interface(
|
| 34 |
fn=extract_order,
|
| 35 |
+
inputs=gr.Textbox(label="OCR Text", lines=3),
|
| 36 |
+
outputs=gr.Textbox(label="Result"),
|
| 37 |
+
title="StackAdvisor",
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
demo.launch()
|