Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytesseract
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import re
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# OCR & Parsing Function
|
| 7 |
+
def extract_info_from_image(image):
|
| 8 |
+
text = pytesseract.image_to_string(image, lang='hin+eng')
|
| 9 |
+
|
| 10 |
+
def extract_data(text):
|
| 11 |
+
data = {}
|
| 12 |
+
data['क्रमांक'] = re.search(r'क्रमांक\s*[:\-]?\s*JS\s*(\d+)', text)
|
| 13 |
+
data['पूरा नाम'] = re.search(r'पूरा नाम\s*[:\-]?\s*(.+)', text)
|
| 14 |
+
data['लिंग'] = 'पुरुष' if '[x]' in text.split('पुरुष')[0][-3:] else 'महिला' if '[x]' in text.split('महिला')[0][-3:] else 'अन्य'
|
| 15 |
+
data['उम्र'] = re.search(r'उम्र\s*[:\-]?\s*(\d+)', text)
|
| 16 |
+
data['गांव'] = re.search(r'गांव\s*[:\-]?\s*(.+)', text)
|
| 17 |
+
data['पंचायत'] = re.search(r'पंचायत\s*[:\-]?\s*(.+)', text)
|
| 18 |
+
data['ब्लॉक'] = re.search(r'ब्लॉक\s*[:\-]?\s*(.+)', text)
|
| 19 |
+
data['जिला'] = re.search(r'जिला\s*[:\-]?\s*(.+)', text)
|
| 20 |
+
data['पिन कोड'] = re.search(r'पिन कोड\s*[:\-]?\s*(\d+)', text)
|
| 21 |
+
data['मोबाइल नंबर'] = re.search(r'मोबाइल नंबर\s*[:\-]?\s*([\d०१२३४५६७८९]+)', text)
|
| 22 |
+
|
| 23 |
+
preferences = {
|
| 24 |
+
"चुनाव के माध्यम से": "चुनाव के माध्यम से",
|
| 25 |
+
"PK ऐप चलव खोलकर": "PK ऐप चलव खोलकर",
|
| 26 |
+
"संगठन में जिम्मेदारी लेकर": "संगठन में जिम्मेदारी लेकर",
|
| 27 |
+
"डिजिटल वालंटियर बनकर": "डिजिटल वालंटियर बनकर"
|
| 28 |
+
}
|
| 29 |
+
support_methods = []
|
| 30 |
+
for phrase in preferences:
|
| 31 |
+
if '[x]' in text.split(phrase)[0][-3:]:
|
| 32 |
+
support_methods.append(phrase)
|
| 33 |
+
data['सहयोग के प्रकार'] = support_methods
|
| 34 |
+
|
| 35 |
+
checklist = {
|
| 36 |
+
"उद्देश्य बताया": "संस्थापक सदस्य को विस्तार से जन सुराज का उद्देश्य बताया",
|
| 37 |
+
"कार्य और अधिकार बताए": "सदस्य को बताया गया कि उसके कार्य और अधिकारों से अवगत कराया",
|
| 38 |
+
"कॉल बताया": "सदस्य को सहयोग के लिए संपर्क नंबर"
|
| 39 |
+
}
|
| 40 |
+
checklist_results = {}
|
| 41 |
+
for key, phrase in checklist.items():
|
| 42 |
+
match = re.search(rf'{re.escape(phrase)}\?\s*\[([x ])\]', text)
|
| 43 |
+
checklist_results[key] = 'हाँ' if match and match.group(1).strip() == 'x' else 'नहीं'
|
| 44 |
+
data['चेकलिस्ट'] = checklist_results
|
| 45 |
+
|
| 46 |
+
for k, v in data.items():
|
| 47 |
+
if isinstance(v, re.Match):
|
| 48 |
+
data[k] = v.group(1).strip()
|
| 49 |
+
return data
|
| 50 |
+
|
| 51 |
+
extracted_info = extract_data(text)
|
| 52 |
+
return extracted_info
|
| 53 |
+
|
| 54 |
+
# Gradio Interface
|
| 55 |
+
iface = gr.Interface(
|
| 56 |
+
fn=extract_info_from_image,
|
| 57 |
+
inputs=gr.Image(type="pil"),
|
| 58 |
+
outputs="json",
|
| 59 |
+
title="जन सुराज सदस्यता फॉर्म डेटा एक्सट्रैक्टर",
|
| 60 |
+
description="Upload a scanned जन सुराज सदस्यता form and extract the information automatically using OCR."
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
iface.launch()
|