nehakothari commited on
Commit
6e742cd
·
verified ·
1 Parent(s): cd34540

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -192
app.py DELETED
@@ -1,192 +0,0 @@
1
- import subprocess
2
- import sys
3
- import os
4
- import gradio as gr
5
- from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
6
- from qwen_vl_utils import process_vision_info
7
- import torch
8
- import pandas as pd
9
- import pytesseract
10
- import cv2
11
- import pymssql
12
-
13
- # Hardcoded Hugging Face token and SQL server IP address
14
-
15
- SERVER_IP = "35.227.148.156"
16
-
17
- # Function to install dependencies dynamically
18
- def install_dependencies():
19
- packages = [
20
- "pytesseract",
21
- "torch==2.1.0",
22
- "torchvision==0.16.0",
23
- "torchaudio==2.1.0",
24
- "transformers==4.38.2",
25
- "auto-gptq==0.7.1",
26
- "autoawq==0.2.8",
27
- "qwen_vl_utils==0.0.8",
28
- "gradio==4.27.0",
29
- "pyodbc",
30
- "sqlalchemy",
31
- "azure-storage-blob",
32
- "pymssql",
33
- "pandas",
34
- "opencv-python"
35
- ]
36
- for package in packages:
37
- subprocess.check_call([sys.executable, "-m", "pip", "install", package])
38
- print(f"Installed {package}")
39
-
40
- install_dependencies()
41
-
42
- # Install system dependencies
43
- def install_system_dependencies():
44
- commands = [
45
- "apt-get update",
46
- "apt-get install -y unixodbc-dev tesseract-ocr",
47
- "ACCEPT_EULA=Y apt-get install -y msodbcsql17"
48
- ]
49
- for command in commands:
50
- subprocess.run(command, shell=True, check=True)
51
- print(f"Executed: {command}")
52
-
53
- install_system_dependencies()
54
-
55
- # Initialize model and processor
56
- model = Qwen2VLForConditionalGeneration.from_pretrained(
57
- "Qwen/Qwen2-VL-2B-Instruct-AWQ",
58
- torch_dtype="auto",
59
- use_auth_token=HUGGINGFACE_API_KEY
60
- )
61
-
62
- processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct-AWQ", use_auth_token=HUGGINGFACE_API_KEY)
63
-
64
- pytesseract.pytesseract_cmd = r'/usr/bin/tesseract'
65
-
66
- # Function to preprocess the image for OCR
67
- def preprocess_image(image_path):
68
- image = cv2.imread(image_path)
69
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
70
- _, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
71
- return binary
72
-
73
- # Function to extract text using OCR
74
- def ocr_extract_text(image_path):
75
- preprocessed_image = preprocess_image(image_path)
76
- return pytesseract.image_to_string(preprocessed_image)
77
-
78
- # Function to process image and extract details
79
- def process_image(image_path):
80
- try:
81
- messages = [{
82
- "role": "user",
83
- "content": [
84
- {"type": "image", "image": image_path},
85
- {"type": "text", "text": (
86
- "Extract the following details from the invoice:\n"
87
- "- 'invoice_number'\n"
88
- "- 'date'\n"
89
- "- 'place'\n"
90
- "- 'amount' (monetary value in the relevant currency)\n"
91
- "- 'category' (based on the invoice type)"
92
- )}
93
- ]
94
- }]
95
-
96
- text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
97
- image_inputs, video_inputs = process_vision_info(messages)
98
- inputs = processor(text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt")
99
- inputs = inputs.to(model.device)
100
-
101
- generated_ids = model.generate(**inputs, max_new_tokens=128)
102
- output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)
103
-
104
- return parse_details(output_text[0])
105
-
106
- except Exception as e:
107
- print(f"Model failed, falling back to OCR: {e}")
108
- ocr_text = ocr_extract_text(image_path)
109
- return parse_details(ocr_text)
110
-
111
- # Function to parse details from extracted text
112
- def parse_details(details):
113
- parsed_data = {
114
- "Invoice Number": None,
115
- "Date": None,
116
- "Place": None,
117
- "Amount": None,
118
- "Category": None
119
- }
120
-
121
- lines = details.split("\n")
122
- for line in lines:
123
- lower_line = line.lower()
124
- if "invoice" in lower_line:
125
- parsed_data["Invoice Number"] = line.split(":")[-1].strip()
126
- elif "date" in lower_line:
127
- parsed_data["Date"] = line.split(":")[-1].strip()
128
- elif "place" in lower_line:
129
- parsed_data["Place"] = line.split(":")[-1].strip()
130
- elif any(keyword in lower_line for keyword in ["total", "amount", "cost"]):
131
- parsed_data["Amount"] = line.split(":")[-1].strip()
132
- else:
133
- parsed_data["Category"] = "General"
134
-
135
- return parsed_data
136
-
137
- # Store extracted data in Azure SQL Database
138
- def store_to_azure_sql(dataframe):
139
- conn_str = (
140
- f"Driver={{ODBC Driver 17 for SQL Server}};"
141
- f"Server={SERVER_IP};"
142
- "Database=Invoices;"
143
- "UID=pio-admin;"
144
- "PWD=Poctest123#;"
145
- )
146
- try:
147
- with pymssql.connect(conn_str) as conn:
148
- cursor = conn.cursor()
149
- create_table_query = """
150
- IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='Invoices' AND xtype='U')
151
- CREATE TABLE Invoices (
152
- InvoiceNumber NVARCHAR(255),
153
- Date NVARCHAR(255),
154
- Place NVARCHAR(255),
155
- Amount NVARCHAR(255),
156
- Category NVARCHAR(255)
157
- )
158
- """
159
- cursor.execute(create_table_query)
160
-
161
- for _, row in dataframe.iterrows():
162
- insert_query = """
163
- INSERT INTO Invoices (InvoiceNumber, Date, Place, Amount, Category)
164
- VALUES (%s, %s, %s, %s, %s)
165
- """
166
- cursor.execute(insert_query, row['Invoice Number'], row['Date'], row['Place'], row['Amount'], row['Category'])
167
- conn.commit()
168
- print("Data successfully stored in Azure SQL Database.")
169
- except Exception as e:
170
- print(f"Error storing data to database: {e}")
171
-
172
- # Gradio interface for invoice processing
173
- def gradio_interface(image_files):
174
- results = []
175
- for image_file in image_files:
176
- details = process_image(image_file)
177
- results.append(details)
178
-
179
- df = pd.DataFrame(results)
180
- store_to_azure_sql(df)
181
- return df
182
-
183
- # Launch Gradio interface
184
- grpc_interface = gr.Interface(
185
- fn=gradio_interface,
186
- inputs=gr.Files(label="Upload Invoice Images"),
187
- outputs=gr.Dataframe(interactive=True),
188
- title="Invoice Extraction System",
189
- )
190
-
191
- if __name__ == "__main__":
192
- grpc_interface.launch(share=True)