utkarsh134 commited on
Commit
611c977
·
verified ·
1 Parent(s): 82752bf

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +39 -15
main.py CHANGED
@@ -4,6 +4,7 @@ import pandas as pd
4
  from fastapi.responses import StreamingResponse
5
  from extract import extract_data_from_image
6
  from parsing import process_extracted_text
 
7
 
8
  app = FastAPI()
9
 
@@ -17,12 +18,10 @@ async def process_image(
17
  try:
18
  file_content = await file.read()
19
  extracted_text = extract_data_from_image(BytesIO(file_content))
20
-
21
  processed_data = process_extracted_text(extracted_text)
22
  metal_data = processed_data["Metal Details"]
23
  diamonds = processed_data["Diamonds"]
24
 
25
- # Add finding values to metal_data
26
  metal_data.update({
27
  "Finding For Gold 14KT": finding_14kt,
28
  "Finding For Gold 18KT": finding_18kt,
@@ -32,7 +31,6 @@ async def process_image(
32
  if not diamonds:
33
  final_df = pd.DataFrame([metal_data])
34
  else:
35
- # Separate data into categories
36
  metal_columns = [
37
  "Customer Ref", "Gold Lock @ $ 2800", "Platinum Lock $ 1040",
38
  "Wt. in gram 14K", "Wt. in gram 18K", "Wt. in gram PLT",
@@ -44,52 +42,78 @@ async def process_image(
44
  "Finding For Gold 14KT", "Finding For Gold 18KT", "Finding for Plt."
45
  ]
46
 
 
 
 
 
47
  price_columns = [
48
- "Price of USD 14K", "Price of USD 18K", "Price of USD PT"
 
49
  ]
50
 
51
- # Create metal data DataFrame without findings and prices
52
  metal_data_basic = {k: v for k, v in metal_data.items()
53
  if k in metal_columns}
54
 
55
  findings_data = {k: v for k, v in metal_data.items()
56
  if k in finding_columns}
57
 
 
 
 
58
  price_data = {k: v for k, v in metal_data.items()
59
  if k in price_columns}
60
 
61
- # Create DataFrames
62
  metal_df = pd.DataFrame([metal_data_basic])
63
  findings_df = pd.DataFrame([findings_data])
 
64
  price_df = pd.DataFrame([price_data])
65
  diamonds_df = pd.DataFrame(diamonds)
66
 
67
- # Create empty rows for subsequent entries
68
  empty_metal = pd.DataFrame([{k: '' for k in metal_columns}] * (len(diamonds_df) - 1))
69
  empty_findings = pd.DataFrame([{k: '' for k in finding_columns}] * (len(diamonds_df) - 1))
 
70
  empty_prices = pd.DataFrame([{k: '' for k in price_columns}] * (len(diamonds_df) - 1))
71
 
72
- # Combine DataFrames in the desired order
73
  combined_metal = pd.concat([metal_df, empty_metal], ignore_index=True)
74
  combined_findings = pd.concat([findings_df, empty_findings], ignore_index=True)
 
75
  combined_prices = pd.concat([price_df, empty_prices], ignore_index=True)
76
 
77
- # Final concatenation in desired order
78
  final_df = pd.concat([
79
- combined_metal, # Metal details first
80
- diamonds_df, # Diamond data second
81
  combined_findings, # Findings third
82
- combined_prices # Prices last
 
83
  ], axis=1)
84
 
 
 
 
 
 
 
 
 
 
85
  output = BytesIO()
86
- final_df.to_csv(output, index=False)
 
 
 
 
 
 
 
87
  output.seek(0)
88
 
89
  return StreamingResponse(
90
  output,
91
- media_type="text/csv",
92
- headers={"Content-Disposition": "attachment; filename=output.csv"}
 
 
 
93
  )
94
  except Exception as e:
95
  raise HTTPException(status_code=400, detail=str(e))
 
4
  from fastapi.responses import StreamingResponse
5
  from extract import extract_data_from_image
6
  from parsing import process_extracted_text
7
+ import csv
8
 
9
  app = FastAPI()
10
 
 
18
  try:
19
  file_content = await file.read()
20
  extracted_text = extract_data_from_image(BytesIO(file_content))
 
21
  processed_data = process_extracted_text(extracted_text)
22
  metal_data = processed_data["Metal Details"]
23
  diamonds = processed_data["Diamonds"]
24
 
 
25
  metal_data.update({
26
  "Finding For Gold 14KT": finding_14kt,
27
  "Finding For Gold 18KT": finding_18kt,
 
31
  if not diamonds:
32
  final_df = pd.DataFrame([metal_data])
33
  else:
 
34
  metal_columns = [
35
  "Customer Ref", "Gold Lock @ $ 2800", "Platinum Lock $ 1040",
36
  "Wt. in gram 14K", "Wt. in gram 18K", "Wt. in gram PLT",
 
42
  "Finding For Gold 14KT", "Finding For Gold 18KT", "Finding for Plt."
43
  ]
44
 
45
+ labour_columns = [
46
+ "Labour 14K", "Labour 18K", "Labour PT"
47
+ ]
48
+
49
  price_columns = [
50
+ "Price of USD 14K", "Price of USD 18K", "Price of USD PT",
51
+ "Price of GBP 14K @ 1/0.78", "Price of GBP 18K @ 1/0.78", "Price of GBP PT @ 1/0.78"
52
  ]
53
 
 
54
  metal_data_basic = {k: v for k, v in metal_data.items()
55
  if k in metal_columns}
56
 
57
  findings_data = {k: v for k, v in metal_data.items()
58
  if k in finding_columns}
59
 
60
+ labour_data = {k: v for k, v in metal_data.items()
61
+ if k in labour_columns}
62
+
63
  price_data = {k: v for k, v in metal_data.items()
64
  if k in price_columns}
65
 
 
66
  metal_df = pd.DataFrame([metal_data_basic])
67
  findings_df = pd.DataFrame([findings_data])
68
+ labour_df = pd.DataFrame([labour_data])
69
  price_df = pd.DataFrame([price_data])
70
  diamonds_df = pd.DataFrame(diamonds)
71
 
 
72
  empty_metal = pd.DataFrame([{k: '' for k in metal_columns}] * (len(diamonds_df) - 1))
73
  empty_findings = pd.DataFrame([{k: '' for k in finding_columns}] * (len(diamonds_df) - 1))
74
+ empty_labour = pd.DataFrame([{k: '' for k in labour_columns}] * (len(diamonds_df) - 1))
75
  empty_prices = pd.DataFrame([{k: '' for k in price_columns}] * (len(diamonds_df) - 1))
76
 
 
77
  combined_metal = pd.concat([metal_df, empty_metal], ignore_index=True)
78
  combined_findings = pd.concat([findings_df, empty_findings], ignore_index=True)
79
+ combined_labour = pd.concat([labour_df, empty_labour], ignore_index=True)
80
  combined_prices = pd.concat([price_df, empty_prices], ignore_index=True)
81
 
 
82
  final_df = pd.concat([
83
+ combined_metal, # Metal details first
84
+ diamonds_df, # Diamond data second
85
  combined_findings, # Findings third
86
+ combined_labour, # Labour fourth
87
+ combined_prices # Prices last
88
  ], axis=1)
89
 
90
+ # Format currency values and fix pound sign encoding
91
+ for col in price_columns:
92
+ final_df[col] = final_df[col].astype(str)
93
+ # Remove any existing quotes
94
+ final_df[col] = final_df[col].apply(lambda x: x.strip('"').strip("'") if x else '')
95
+ # Fix pound sign encoding by directly using the UTF-8 pound symbol
96
+ if "GBP" in col:
97
+ final_df[col] = final_df[col].apply(lambda x: x.replace('£', '£') if x else '')
98
+
99
  output = BytesIO()
100
+ # Write CSV with UTF-8 encoding and BOM
101
+ final_df.to_csv(
102
+ output,
103
+ index=False,
104
+ encoding='utf-8-sig',
105
+ quoting=csv.QUOTE_MINIMAL,
106
+ escapechar='\\'
107
+ )
108
  output.seek(0)
109
 
110
  return StreamingResponse(
111
  output,
112
+ media_type="text/csv; charset=utf-8",
113
+ headers={
114
+ "Content-Disposition": "attachment; filename=output.csv",
115
+ "Content-Type": "text/csv; charset=utf-8"
116
+ }
117
  )
118
  except Exception as e:
119
  raise HTTPException(status_code=400, detail=str(e))