Add application file
Browse files- app.py +69 -4
- requirements.txt +4 -0
app.py
CHANGED
|
@@ -1,7 +1,72 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoProcessor, Gemma3nForConditionalGeneration
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
import textwrap
|
| 6 |
|
| 7 |
+
# π Load model and processor
|
| 8 |
+
model_id = "google/gemma-3n-e2b-it"
|
| 9 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 10 |
+
model = Gemma3nForConditionalGeneration.from_pretrained(
|
| 11 |
+
model_id,
|
| 12 |
+
torch_dtype=torch.float32,
|
| 13 |
+
device_map="cpu"
|
| 14 |
+
).eval()
|
| 15 |
+
|
| 16 |
+
# π οΈ Helper to format output
|
| 17 |
+
def print_response(text: str) -> str:
|
| 18 |
+
return "\n".join(textwrap.fill(line, 100) for line in text.split("\n"))
|
| 19 |
+
|
| 20 |
+
# π Inference function
|
| 21 |
+
def predict(image: Image.Image, instruction: str) -> str:
|
| 22 |
+
messages = [
|
| 23 |
+
{
|
| 24 |
+
"role": "system",
|
| 25 |
+
"content": [{"type": "text", "text": "You are a helpful assistant that extracts fields from documents."}],
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"role": "user",
|
| 29 |
+
"content": [
|
| 30 |
+
{"type": "image", "image": image},
|
| 31 |
+
{"type": "text", "text": instruction}
|
| 32 |
+
],
|
| 33 |
+
},
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
inputs = processor.apply_chat_template(
|
| 37 |
+
messages,
|
| 38 |
+
add_generation_prompt=True,
|
| 39 |
+
tokenize=True,
|
| 40 |
+
return_dict=True,
|
| 41 |
+
return_tensors="pt"
|
| 42 |
+
).to(model.device)
|
| 43 |
+
|
| 44 |
+
input_len = inputs["input_ids"].shape[-1]
|
| 45 |
+
|
| 46 |
+
with torch.inference_mode():
|
| 47 |
+
output = model.generate(
|
| 48 |
+
**inputs,
|
| 49 |
+
max_new_tokens=500,
|
| 50 |
+
do_sample=False,
|
| 51 |
+
use_cache=False # π₯ Fixes CPU bug
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
gen = output[0][input_len:]
|
| 55 |
+
decoded = processor.decode(gen, skip_special_tokens=True)
|
| 56 |
+
return print_response(decoded)
|
| 57 |
+
|
| 58 |
+
# ποΈ Gradio Interface
|
| 59 |
+
demo = gr.Interface(
|
| 60 |
+
fn=predict,
|
| 61 |
+
inputs=[
|
| 62 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 63 |
+
gr.Textbox(lines=2, label="Instruction", value="List visible fields as JSON array {field, value}.")
|
| 64 |
+
],
|
| 65 |
+
outputs=gr.Textbox(label="Output"),
|
| 66 |
+
title="Gemma 3n Vision + Text",
|
| 67 |
+
description="Upload an image (e.g., a passport or form) and ask for structured output."
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
demo.launch()
|
| 72 |
|
|
|
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers>=4.42.0
|
| 2 |
+
torch
|
| 3 |
+
gradio
|
| 4 |
+
|