utkarsh134 commited on
Commit
c770c67
·
verified ·
1 Parent(s): 80c70f1

Upload 7 files

Browse files
Files changed (7) hide show
  1. Dockerfile +18 -0
  2. client.py +9 -0
  3. extract.py +133 -0
  4. json_dict.py +17 -0
  5. main.py +34 -0
  6. parsing.py +142 -0
  7. requirements.txt +7 -0
Dockerfile ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python image
2
+ FROM python:3.10
3
+
4
+ # Set the working directory
5
+ WORKDIR /app
6
+
7
+ # Copy requirements file and install dependencies
8
+ COPY requirements.txt .
9
+ RUN pip install --no-cache-dir -r requirements.txt
10
+
11
+ # Copy all application files
12
+ COPY . .
13
+
14
+ # Expose the port Hugging Face Spaces uses (7860)
15
+ EXPOSE 7860
16
+
17
+ # Run the FastAPI application with Uvicorn
18
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
client.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import boto3
2
+ import os
3
+ from dotenv import load_dotenv
4
+ load_dotenv()
5
+
6
+ def get_textract_client():
7
+ return boto3.client("textract", aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"), aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"), region_name=os.getenv("AWS_REGION")
8
+ )
9
+
extract.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from client import get_textract_client
2
+ from io import BytesIO
3
+
4
+ def extract_data_from_image(image_input):
5
+ # Initialize Textract client
6
+ client = get_textract_client()
7
+
8
+ # Handle both file paths and BytesIO objects
9
+ if isinstance(image_input, (str, bytes)):
10
+ # If it's a file path or bytes
11
+ if isinstance(image_input, str):
12
+ with open(image_input, 'rb') as document:
13
+ image_bytes = document.read()
14
+ else:
15
+ image_bytes = image_input
16
+ elif isinstance(image_input, BytesIO):
17
+ # If it's BytesIO
18
+ image_bytes = image_input.getvalue()
19
+ else:
20
+ raise ValueError("Invalid input type. Expected str, bytes, or BytesIO")
21
+
22
+ # Process the image document
23
+ response = client.analyze_document(
24
+ Document={'Bytes': image_bytes},
25
+ FeatureTypes=["TABLES", "FORMS"] # Added FORMS to extract key-value pairs
26
+ )
27
+
28
+ # Process Textract response
29
+ def process_blocks(blocks):
30
+ text_lines = []
31
+ tables = []
32
+ key_values = []
33
+
34
+ # First, create a dictionary of all blocks for lookup
35
+ block_map = {block['Id']: block for block in blocks}
36
+
37
+ for block in blocks:
38
+ block_type = block['BlockType']
39
+
40
+ if block_type == "LINE":
41
+ text_lines.append(block['Text'])
42
+
43
+ elif block_type == "TABLE":
44
+ table_cells = {}
45
+ rows = []
46
+
47
+ # Get cells for this table
48
+ if 'Relationships' in block and any(rel['Type'] == 'CHILD' for rel in block['Relationships']):
49
+ for rel in block['Relationships']:
50
+ if rel['Type'] == 'CHILD':
51
+ for cell_id in rel['Ids']:
52
+ cell_block = block_map[cell_id]
53
+ if cell_block['BlockType'] == 'CELL':
54
+ row_index = cell_block['RowIndex']
55
+ col_index = cell_block['ColumnIndex']
56
+
57
+ # Get cell content
58
+ cell_text = ""
59
+ if 'Relationships' in cell_block and any(rel['Type'] == 'CHILD' for rel in cell_block['Relationships']):
60
+ child_ids = next(rel['Ids'] for rel in cell_block['Relationships'] if rel['Type'] == 'CHILD')
61
+ for child_id in child_ids:
62
+ child_block = block_map[child_id]
63
+ if child_block['BlockType'] in ['WORD', 'LINE']:
64
+ cell_text += child_block['Text'] + " "
65
+
66
+ if not cell_text and 'Text' in cell_block:
67
+ cell_text = cell_block['Text']
68
+
69
+ cell_text = cell_text.strip()
70
+
71
+ # Create row if not exists
72
+ if row_index not in table_cells:
73
+ table_cells[row_index] = {}
74
+
75
+ # Add cell to row
76
+ table_cells[row_index][col_index] = cell_text
77
+
78
+ # Format table as tab-separated rows
79
+ for row_idx in sorted(table_cells.keys()):
80
+ row_data = table_cells[row_idx]
81
+ row_text = "\t".join([row_data.get(col_idx, "") for col_idx in sorted(row_data.keys())])
82
+ rows.append(row_text)
83
+
84
+ if rows:
85
+ tables.append("\n".join(rows))
86
+
87
+ elif block_type == "KEY_VALUE_SET":
88
+ if 'EntityTypes' in block and 'KEY' in block['EntityTypes']:
89
+ key_block = block
90
+ key_text = ""
91
+ value_text = ""
92
+
93
+ # Get key text
94
+ if 'Relationships' in key_block:
95
+ for relationship in key_block['Relationships']:
96
+ if relationship['Type'] == 'CHILD':
97
+ for child_id in relationship['Ids']:
98
+ child_block = block_map[child_id]
99
+ if child_block['BlockType'] == 'WORD':
100
+ key_text += child_block['Text'] + " "
101
+
102
+ # Get value block
103
+ if 'Relationships' in key_block:
104
+ for relationship in key_block['Relationships']:
105
+ if relationship['Type'] == 'VALUE':
106
+ for value_id in relationship['Ids']:
107
+ value_block = block_map[value_id]
108
+
109
+ # Get value text
110
+ if 'Relationships' in value_block:
111
+ for value_relationship in value_block['Relationships']:
112
+ if value_relationship['Type'] == 'CHILD':
113
+ for value_child_id in value_relationship['Ids']:
114
+ value_child_block = block_map[value_child_id]
115
+ if value_child_block['BlockType'] == 'WORD':
116
+ value_text += value_child_block['Text'] + " "
117
+
118
+ if key_text and value_text:
119
+ key_values.append(f"{key_text.strip()}: {value_text.strip()}")
120
+
121
+ return {
122
+ 'text': '\n'.join(text_lines),
123
+ 'tables': tables,
124
+ 'key_values': key_values
125
+ }
126
+
127
+ processed_data = process_blocks(response['Blocks'])
128
+ formatted_output = (
129
+ f"Text Content:\n{processed_data['text']}\n\n"
130
+ f"Key-Value Pairs:\n{chr(10).join(processed_data['key_values'])}\n\n"
131
+ f"Tables:\n" + "\n\n".join(processed_data['tables'])
132
+ )
133
+ return formatted_output
json_dict.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ def parse_json_response(json_output):
4
+ try:
5
+ # Attempt to parse directly
6
+ return json.loads(json_output)
7
+ except json.JSONDecodeError:
8
+ print("Invalid JSON detected, attempting cleanup...")
9
+
10
+ # Remove problematic characters like backticks
11
+ json_output = json_output.strip("`").replace("\n", "").replace("\t", "")
12
+
13
+ try:
14
+ return json.loads(json_output) # Try parsing again
15
+ except json.JSONDecodeError as e:
16
+ print(f"Final JSON parsing failed: {e}")
17
+ return None
main.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, HTTPException
2
+ from io import BytesIO
3
+ import pandas as pd
4
+ from fastapi.responses import StreamingResponse
5
+ from extract import extract_data_from_image
6
+ from parsing import parse_data_with_llm
7
+ from parsing import compute_values
8
+
9
+
10
+ app = FastAPI()
11
+
12
+ @app.post("/process-image/")
13
+ async def process_image(file: UploadFile = File(...)):
14
+ try:
15
+ file_content = await file.read()
16
+ extracted_text = extract_data_from_image(BytesIO(file_content))
17
+
18
+ structured_data = parse_data_with_llm(extracted_text)
19
+
20
+ final_data = compute_values(structured_data)
21
+
22
+ df = pd.DataFrame([final_data])
23
+ output = BytesIO()
24
+ df.to_csv(output, index=False)
25
+ output.seek(0)
26
+
27
+
28
+ return StreamingResponse(
29
+ output,
30
+ media_type="text/csv",
31
+ headers={"Content-Disposition": "attachment; filename=output.csv"}
32
+ )
33
+ except Exception as e:
34
+ raise HTTPException(status_code=400, detail=str(e))
parsing.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.prompts import PromptTemplate
2
+ from langchain_google_genai import ChatGoogleGenerativeAI
3
+ import os
4
+ import json
5
+ import re
6
+ from dotenv import load_dotenv
7
+ load_dotenv()
8
+
9
+ def get_llm():
10
+ """Create and return a ChatGoogleGenerativeAI instance with proper authentication."""
11
+ google_api_key = os.getenv("GOOGLE_API_KEY")
12
+ if not google_api_key:
13
+ raise ValueError("GOOGLE_API_KEY environment variable is not set")
14
+ return ChatGoogleGenerativeAI(
15
+ model="gemini-2.0-flash",
16
+ temperature=0,
17
+ google_api_key=google_api_key
18
+ )
19
+
20
+ def extract_json_from_text(text):
21
+ """Extract JSON from text that might contain explanation text or code blocks."""
22
+ # Look for text between curly braces
23
+ json_pattern = r'\{.*\}'
24
+ match = re.search(json_pattern, text, re.DOTALL)
25
+
26
+ if match:
27
+ potential_json = match.group(0)
28
+ try:
29
+ return json.loads(potential_json)
30
+ except json.JSONDecodeError:
31
+ pass
32
+
33
+ # Try to find JSON in markdown code blocks
34
+ code_block_pattern = r'```(?:json)?\s*([\s\S]*?)\s*```'
35
+ matches = re.findall(code_block_pattern, text, re.DOTALL)
36
+
37
+ for potential_json in matches:
38
+ try:
39
+ # Remove any triple backticks that might be included
40
+ cleaned = potential_json.strip().replace('```', '')
41
+ if cleaned.startswith('{') and cleaned.endswith('}'):
42
+ return json.loads(cleaned)
43
+ except json.JSONDecodeError:
44
+ continue
45
+
46
+ return None
47
+
48
+ def parse_data_with_llm(extracted_text):
49
+ """Extract structured JSON from extracted text but do calculations in Python."""
50
+ llm = get_llm()
51
+
52
+ prompt_template = PromptTemplate(
53
+ input_variables=["extracted_data"],
54
+ template= """
55
+ You are a data extraction assistant. Given the following extracted text, structure the output as valid JSON.
56
+ **Do NOT perform any calculations.** Just extract the numeric values exactly as they appear.
57
+
58
+ **Extracted data:**
59
+ {extracted_data}
60
+
61
+ ### **Extract the Following Fields (Raw Data Only)**:
62
+ - **Customer Ref**: Extract "Customer PO".
63
+ - **Wt. in gram 14K**: Extract all "14K WG" weight values of parts and return as a list.
64
+ - **Wt. in gram 18K**: Extract all "18K WG" weight values of parts and return as a list.
65
+ - **Wt. in gram PLT**: Extract all "PLT" weight values of parts and return as a list.
66
+
67
+ **Output STRICTLY as JSON (No markdown, no explanations, no formatting issues):**
68
+ ```json
69
+ {{
70
+ "Customer Ref": "...",
71
+ "Wt. in gram 14K": [...],
72
+ "Wt. in gram 18K": [...],
73
+ "Wt. in gram PLT": [...]
74
+ }}
75
+ ```
76
+ """
77
+ )
78
+
79
+ prompt = prompt_template.format(extracted_data=extracted_text)
80
+ response = llm.invoke(prompt)
81
+
82
+ # Extract valid JSON from response
83
+ response_text = response.content.strip() if hasattr(response, 'content') else str(response).strip()
84
+ json_data = extract_json_from_text(response_text)
85
+
86
+ if not json_data:
87
+ cleaned_response = response_text.replace("```json", "").replace("```", "").strip()
88
+ try:
89
+ json_data = json.loads(cleaned_response)
90
+ except json.JSONDecodeError as e:
91
+ raise ValueError(f"LLM response is not in valid JSON format: {str(e)}")
92
+
93
+ return json_data
94
+
95
+
96
+ def compute_values(data):
97
+ """Perform gold and platinum calculations based on extracted values."""
98
+ gold_lock = 2800
99
+ platinum_lock = 1040
100
+
101
+ # Convert extracted weights to float before summing
102
+ wt_14k = sum([float(x) for x in data.get("Wt. in gram 14K", []) if x])
103
+ wt_18k = sum([float(x) for x in data.get("Wt. in gram 18K", []) if x])
104
+ wt_plt = sum([float(x) for x in data.get("Wt. in gram PLT", []) if x])
105
+
106
+ # Gold & Platinum Rate Calculations
107
+ gold_rate_14k = (gold_lock / 31.1035) * 0.585 * 1.1
108
+ gold_rate_18k = (gold_lock / 31.1035) * 0.75 * 1.1
109
+ plat_rate = (platinum_lock / 31.1035) * 0.95 * 1.2
110
+
111
+ # Final Computed Values
112
+ gold_value_14k = wt_14k * gold_rate_14k
113
+ gold_value_18k = wt_18k * gold_rate_18k
114
+ plat_value = wt_plt * plat_rate
115
+
116
+ # Return final JSON
117
+ return {
118
+ "Customer Ref": data.get("Customer Ref", ""),
119
+ "Gold Lock @ $ 2800": gold_lock,
120
+ "Platinum Lock $ 1040": platinum_lock,
121
+ "Wt. in gram 14K": wt_14k,
122
+ "Wt. in gram 18K": wt_18k,
123
+ "Wt. in gram PLT": wt_plt,
124
+ "Gold Rate (including loss) 14KT": gold_rate_14k,
125
+ "Gold Rate (including loss) 18KT": gold_rate_18k,
126
+ "Plat rate (including Lab)": plat_rate,
127
+ "Gold Value 14K": gold_value_14k,
128
+ "Gold Value 18K": gold_value_18k,
129
+ "Plat Value": plat_value
130
+ }
131
+
132
+
133
+
134
+
135
+ def process_extracted_text(extracted_text):
136
+ # Step 1: Get extracted numbers from LLM
137
+ extracted_data = parse_data_with_llm(extracted_text)
138
+
139
+ # Step 2: Perform calculations in Python
140
+ final_data = compute_values(extracted_data)
141
+
142
+ return final_data
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ boto3
4
+ pandas
5
+ python-dotenv
6
+ langchain
7
+ langchain-google-genai