utkarsh134 commited on
Commit
b3b99d1
·
verified ·
1 Parent(s): f911f40

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +95 -34
main.py CHANGED
@@ -1,34 +1,95 @@
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))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, Form, 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 process_extracted_text
7
+
8
+ app = FastAPI()
9
+
10
+ @app.post("/process-image/")
11
+ async def process_image(
12
+ file: UploadFile = File(...),
13
+ finding_14kt: float = Form(...),
14
+ finding_18kt: float = Form(...),
15
+ finding_plt: float = Form(...)
16
+ ):
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,
29
+ "Finding for Plt.": finding_plt
30
+ })
31
+
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",
39
+ "Gold Rate (including loss) 14KT", "Gold Rate (including loss) 18KT",
40
+ "Plat rate (including Lab)", "Gold Value 14K", "Gold Value 18K", "Plat Value"
41
+ ]
42
+
43
+ finding_columns = [
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))