danial0203 commited on
Commit
e6560fa
·
verified ·
1 Parent(s): 7e4ce2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -45
app.py CHANGED
@@ -53,28 +53,26 @@ def process_files_fixed(image_path, page_identifier, error_pages):
53
  error_pages.append(page_identifier)
54
  return []
55
 
56
- prompt = """Perform OCR on this image. Analyze the table in the provided image , focusing on TOTAL OF ELEVEN COLUMNS labeled S.No, Admission No.,Date of Admission,Name of Student,Father's Name,Date of Birth ,Telephone No., Address, F.CNIC, S.CNIC and M.Name . Get the Telephone No. from the last column ignore office and residence column under it and write them. For F.CNIC, S.CNIC and M.Name you will find this under REMARKS column.I don't want any mistakes in the obtained data. In case the table headers are not visible or not present, assume the mentioned order for the columns. Extract and list the data only from these columns, omitting any additional columns that may be present. But DO NOT skip any row from the table, extract all the rows present in the table. Verify both for better ocr in integers.
57
- Return the response in the following JSON response format:
58
- {
59
- "data": [
60
- {
61
- "S_No": "1",
62
- "Admission No.": "1604",
63
- "Date of Admission": "25-4-17",
64
- "Name of Student" : "Maham Tariq",
65
- "Father's Name" : "Tariq Mehman",
66
- "Date of Birth" : "12-05-12",
67
- "Telephone No." : "03125350838",
68
- "Address" : "Dewan- e. umar Masjid F1014",
69
- "F. CNIC" : "61101-9729652-7",
70
- "S.CNIC" : "61101-8018797-4",
71
- "M.Name" : "Nasira"
72
-
73
-
74
- },
75
- ...
76
- ]
77
- }"""
78
 
79
  payload = {
80
  "model": "gpt-4-vision-preview",
@@ -96,38 +94,37 @@ Return the response in the following JSON response format:
96
  "max_tokens": 4096
97
  }
98
 
99
- try:
100
- response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
101
- logging.info(f"Full API response: {response.text}")
102
- if response.status_code == 200:
103
- response_text = response.text
104
- # Use regex to extract JSON from the API response content
105
- matches = re.findall(r"```json\n(\{.*?\})\n```", response_text, re.DOTALL)
106
- if matches:
107
- try:
108
- json_data = json.loads(matches[0]) # Assuming the first match is the required JSON data
109
- if "data" in json_data and json_data["data"]:
110
- return json_data["data"]
111
- else:
112
- logging.error(f"No records found in page/file: {page_identifier}")
 
 
 
113
  error_pages.append(page_identifier)
114
  return []
115
- except json.JSONDecodeError:
116
- logging.error(f"JSON parsing error in response for page/file {page_identifier}")
117
  error_pages.append(page_identifier)
118
  return []
119
  else:
120
- logging.error(f"No valid JSON found in response for page/file {page_identifier}")
121
  error_pages.append(page_identifier)
122
  return []
123
- else:
124
- logging.error(f"Error in API call for page/file {page_identifier}: HTTP {response.status_code} - {response.text}")
125
  error_pages.append(page_identifier)
126
  return []
127
- except requests.exceptions.RequestException as e:
128
- logging.error(f"Network or API error when processing page/file {page_identifier}: {e}")
129
- error_pages.append(page_identifier)
130
- return []
131
 
132
  def process_pdf_and_generate_csv(file_path):
133
  error_pages = [] # Initialize the list to track error pages or files
 
53
  error_pages.append(page_identifier)
54
  return []
55
 
56
+ prompt = """Perform OCR on this image. Analyze the table in the provided image, focusing on TOTAL OF ELEVEN COLUMNS labeled S.No, Admission No., Date of Admission, Name of Student, Father's Name, Date of Birth, Telephone No., Address, F.CNIC, S.CNIC and M.Name. Get the Telephone No. from the last column, ignore office and residence column under it and write them. For F.CNIC, S.CNIC and M.Name you will find this under REMARKS column. I don't want any mistakes in the obtained data. In case the table headers are not visible or not present, assume the mentioned order for the columns. Extract and list the data only from these columns, omitting any additional columns that may be present. But DO NOT skip any row from the table, extract all the rows present in the table. Verify both for better OCR in integers.
57
+ Return the response in the following JSON response format:
58
+ {
59
+ "data": [
60
+ {
61
+ "S_No": "1",
62
+ "Admission No.": "1604",
63
+ "Date of Admission": "25-4-17",
64
+ "Name of Student": "Maham Tariq",
65
+ "Father's Name": "Tariq Mehman",
66
+ "Date of Birth": "12-05-12",
67
+ "Telephone No.": "03125350838",
68
+ "Address": "Dewan-e-umar Masjid F1014",
69
+ "F. CNIC": "61101-9729652-7",
70
+ "S.CNIC": "61101-8018797-4",
71
+ "M.Name": "Nasira"
72
+ },
73
+ ...
74
+ ]
75
+ }"""
 
 
76
 
77
  payload = {
78
  "model": "gpt-4-vision-preview",
 
94
  "max_tokens": 4096
95
  }
96
 
97
+ try:
98
+ response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
99
+ logging.info(f"Full API response: {response.text}")
100
+ if response.status_code == 200:
101
+ response_text = response.text
102
+ matches = re.findall(r"```json\n(\{.*?\})\n```", response_text, re.DOTALL)
103
+ if matches:
104
+ try:
105
+ json_data = json.loads(matches[0])
106
+ if "data" in json_data and json_data["data"]:
107
+ return json_data["data"]
108
+ else:
109
+ logging.error(f"No records found in page/file: {page_identifier}")
110
+ error_pages.append(page_identifier)
111
+ return []
112
+ except json.JSONDecodeError:
113
+ logging.error(f"JSON parsing error in response for page/file {page_identifier}")
114
  error_pages.append(page_identifier)
115
  return []
116
+ else:
117
+ logging.error(f"No valid JSON found in response for page/file {page_identifier}")
118
  error_pages.append(page_identifier)
119
  return []
120
  else:
121
+ logging.error(f"Error in API call for page/file {page_identifier}: HTTP {response.status_code} - {response.text}")
122
  error_pages.append(page_identifier)
123
  return []
124
+ except requests.exceptions.RequestException as e:
125
+ logging.error(f"Network or API error when processing page/file {page_identifier}: {e}")
126
  error_pages.append(page_identifier)
127
  return []
 
 
 
 
128
 
129
  def process_pdf_and_generate_csv(file_path):
130
  error_pages = [] # Initialize the list to track error pages or files