kshitijsonje commited on
Commit
b7c12e5
·
verified ·
1 Parent(s): 058b08a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py CHANGED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoProcessor, AutoModelForVision2Seq
3
+ from PIL import Image
4
+ import torch
5
+ import io
6
+
7
+ # Load model and processor once
8
+ @st.cache_resource
9
+ def load_model():
10
+ model_id = "HuggingFaceTB/SmolVLM2-2.2B-Instruct"
11
+ processor = AutoProcessor.from_pretrained(model_id)
12
+ model = AutoModelForVision2Seq.from_pretrained(model_id).to("cuda" if torch.cuda.is_available() else "cpu")
13
+ return processor, model
14
+
15
+ processor, model = load_model()
16
+
17
+ # Streamlit UI
18
+ st.title("Aadhaar Card Information Extractor")
19
+ uploaded_file = st.file_uploader("Upload Aadhaar card image", type=["jpg", "png", "jpeg"])
20
+
21
+ if uploaded_file is not None:
22
+ image = Image.open(uploaded_file).convert("RGB")
23
+ st.image(image, caption="Uploaded Aadhaar Card", use_column_width=True)
24
+
25
+ if st.button("Extract Info"):
26
+ with st.spinner("Extracting..."):
27
+ prompt = (
28
+ "You are an AI system for extracting information from Indian Aadhaar cards. "
29
+ "From the image, extract and return a structured JSON with:\n"
30
+ "- Name\n"
31
+ "- Father's Name\n"
32
+ "- Date of Birth\n"
33
+ "- Gender\n"
34
+ "- Aadhaar Number\n"
35
+ "- Address (Street, Locality, District, State, PIN)\n"
36
+ "- QR code data (if visible)\n"
37
+ "- Bounding box of photograph as [x1, y1, x2, y2]\n"
38
+ "Respond only with JSON."
39
+ )
40
+
41
+ inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device)
42
+ outputs = model.generate(**inputs, max_new_tokens=512)
43
+ result = processor.batch_decode(outputs, skip_special_tokens=True)[0]
44
+ st.code(result, language="json")