Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -5,41 +5,143 @@ 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@app.post("/process-image/")
|
| 12 |
async def process_image(
|
| 13 |
file: UploadFile = File(...),
|
| 14 |
finding_14kt: float = Form(...),
|
| 15 |
finding_18kt: float = Form(...),
|
| 16 |
-
finding_plt: float = Form(...)
|
|
|
|
| 17 |
):
|
| 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,
|
| 28 |
"Finding for Plt.": finding_plt
|
| 29 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
if not diamonds:
|
| 32 |
final_df = pd.DataFrame([metal_data])
|
| 33 |
else:
|
|
|
|
|
|
|
|
|
|
| 34 |
metal_columns = [
|
| 35 |
-
"Customer Ref",
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
]
|
| 40 |
|
| 41 |
finding_columns = [
|
| 42 |
-
"Finding For Gold 14KT", "Finding For Gold 18KT", "Finding for Plt."
|
|
|
|
| 43 |
]
|
| 44 |
|
| 45 |
labour_columns = [
|
|
@@ -48,7 +150,11 @@ async def process_image(
|
|
| 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()
|
|
|
|
| 5 |
from extract import extract_data_from_image
|
| 6 |
from parsing import process_extracted_text
|
| 7 |
import csv
|
| 8 |
+
import json
|
| 9 |
+
import math
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
+
@app.post("/extract-diamonds/")
|
| 14 |
+
async def extract_diamonds(file: UploadFile = File(...)):
|
| 15 |
+
try:
|
| 16 |
+
file_content = await file.read()
|
| 17 |
+
extracted_text = extract_data_from_image(BytesIO(file_content))
|
| 18 |
+
processed_data = process_extracted_text(extracted_text)
|
| 19 |
+
return {"diamonds": processed_data["Diamonds"]}
|
| 20 |
+
except Exception as e:
|
| 21 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 22 |
+
|
| 23 |
+
@app.post("/process-image/")
|
| 24 |
@app.post("/process-image/")
|
| 25 |
async def process_image(
|
| 26 |
file: UploadFile = File(...),
|
| 27 |
finding_14kt: float = Form(...),
|
| 28 |
finding_18kt: float = Form(...),
|
| 29 |
+
finding_plt: float = Form(...),
|
| 30 |
+
diamond_rates: str = Form(...)
|
| 31 |
):
|
| 32 |
try:
|
| 33 |
+
diamond_rates_dict = json.loads(diamond_rates)
|
| 34 |
file_content = await file.read()
|
| 35 |
extracted_text = extract_data_from_image(BytesIO(file_content))
|
| 36 |
processed_data = process_extracted_text(extracted_text)
|
| 37 |
metal_data = processed_data["Metal Details"]
|
| 38 |
diamonds = processed_data["Diamonds"]
|
| 39 |
|
| 40 |
+
# Add diamond rates and calculate values for each diamond
|
| 41 |
+
diamond_values_sum = 0
|
| 42 |
+
for idx, diamond in enumerate(diamonds):
|
| 43 |
+
rate = float(diamond_rates_dict.get(str(idx), 0))
|
| 44 |
+
diamond["Diamond Rate"] = rate
|
| 45 |
+
# Calculate Diamond Value using Diamond Weight and Rate
|
| 46 |
+
diamond_weight = float(diamond.get("Diamond Weight", 0))
|
| 47 |
+
diamond_value = round(diamond_weight * rate, 2)
|
| 48 |
+
diamond["Diamond Value"] = diamond_value
|
| 49 |
+
diamond_values_sum += diamond_value
|
| 50 |
+
|
| 51 |
metal_data.update({
|
| 52 |
"Finding For Gold 14KT": finding_14kt,
|
| 53 |
"Finding For Gold 18KT": finding_18kt,
|
| 54 |
"Finding for Plt.": finding_plt
|
| 55 |
})
|
| 56 |
+
|
| 57 |
+
# Recalculate prices with actual diamond values
|
| 58 |
+
# Price calculations using the correct formulas
|
| 59 |
+
usd_14k = (
|
| 60 |
+
metal_data["Gold Value 14K"] +
|
| 61 |
+
finding_14kt +
|
| 62 |
+
diamond_values_sum +
|
| 63 |
+
metal_data["Labour 14K"] +
|
| 64 |
+
metal_data["Rhodium"]
|
| 65 |
+
)
|
| 66 |
+
usd_14k = math.ceil(usd_14k) + 1 # Round up and add 1
|
| 67 |
+
|
| 68 |
+
usd_18k = (
|
| 69 |
+
metal_data["Gold Value 18K"] +
|
| 70 |
+
finding_18kt +
|
| 71 |
+
diamond_values_sum +
|
| 72 |
+
metal_data["Labour 18K"] +
|
| 73 |
+
metal_data["Rhodium"]
|
| 74 |
+
)
|
| 75 |
+
usd_18k = math.ceil(usd_18k) + 1 # Round up and add 1
|
| 76 |
+
|
| 77 |
+
usd_pt = (
|
| 78 |
+
metal_data["Plat Value"] +
|
| 79 |
+
finding_plt +
|
| 80 |
+
diamond_values_sum +
|
| 81 |
+
metal_data["Labour PT"] +
|
| 82 |
+
metal_data["Rhodium"]
|
| 83 |
+
)
|
| 84 |
+
usd_pt = math.ceil(usd_pt) + 1 # Round up and add 1
|
| 85 |
+
|
| 86 |
+
# GBP conversions
|
| 87 |
+
gbp_14k = usd_14k * 0.78
|
| 88 |
+
gbp_18k = usd_18k * 0.78
|
| 89 |
+
gbp_pt = usd_pt * 0.78
|
| 90 |
+
|
| 91 |
+
# Duty calculations (add 2.5% to GBP amounts)
|
| 92 |
+
duty_14k = gbp_14k * 1.025 # Adding 2.5% to GBP amount
|
| 93 |
+
duty_18k = gbp_18k * 1.025
|
| 94 |
+
duty_pt = gbp_pt * 1.025
|
| 95 |
+
|
| 96 |
+
# Freight constant
|
| 97 |
+
freight = 75
|
| 98 |
+
|
| 99 |
+
# Landing costs
|
| 100 |
+
landing_14k = duty_14k + freight
|
| 101 |
+
landing_18k = duty_18k + freight
|
| 102 |
+
landing_pt = duty_pt + freight
|
| 103 |
+
|
| 104 |
+
# Update metal_data with new calculations
|
| 105 |
+
metal_data.update({
|
| 106 |
+
"Price of USD 14K": f"$ {usd_14k:.2f}",
|
| 107 |
+
"Price of USD 18K": f"$ {usd_18k:.2f}",
|
| 108 |
+
"Price of USD PT": f"$ {usd_pt:.2f}",
|
| 109 |
+
"Price of GBP 14K @ 1/0.78": f"£ {gbp_14k:.2f}",
|
| 110 |
+
"Price of GBP 18K @ 1/0.78": f"£ {gbp_18k:.2f}",
|
| 111 |
+
"Price of GBP PT @ 1/0.78": f"£ {gbp_pt:.2f}",
|
| 112 |
+
"14K Duty @ 2.5% Including": f"£ {duty_14k:.2f}",
|
| 113 |
+
"18K Duty @ 2.5% Including": f"£ {duty_18k:.2f}",
|
| 114 |
+
"PT950 Duty @ 2.5% Including": f"£ {duty_pt:.2f}",
|
| 115 |
+
"Freight": freight,
|
| 116 |
+
"14K Landing Cost (Duty + Freight)": f"£ {landing_14k:.2f}",
|
| 117 |
+
"18K Landing Cost (Duty + Freight)": f"£ {landing_18k:.2f}",
|
| 118 |
+
"PT950 Landing Cost (Duty + Freight)": f"£ {landing_pt:.2f}"
|
| 119 |
+
})
|
| 120 |
|
| 121 |
if not diamonds:
|
| 122 |
final_df = pd.DataFrame([metal_data])
|
| 123 |
else:
|
| 124 |
+
gold_lock_col = next(col for col in metal_data.keys() if "Gold Lock @" in col)
|
| 125 |
+
platinum_lock_col = next(col for col in metal_data.keys() if "Platinum Lock" in col)
|
| 126 |
+
|
| 127 |
metal_columns = [
|
| 128 |
+
"Customer Ref",
|
| 129 |
+
next(col for col in metal_data.keys() if "Gold Lock @" in col), # Dynamic gold lock column
|
| 130 |
+
next(col for col in metal_data.keys() if "Platinum Lock" in col), # Dynamic platinum lock column
|
| 131 |
+
"Wt. in gram 14K",
|
| 132 |
+
"Wt. in gram 18K",
|
| 133 |
+
"Wt. in gram PLT",
|
| 134 |
+
"Gold Rate (including loss) 14KT",
|
| 135 |
+
"Gold Rate (including loss) 18KT",
|
| 136 |
+
"Plat rate (including Lab)",
|
| 137 |
+
"Gold Value 14K",
|
| 138 |
+
"Gold Value 18K",
|
| 139 |
+
"Plat Value"
|
| 140 |
]
|
| 141 |
|
| 142 |
finding_columns = [
|
| 143 |
+
"Finding For Gold 14KT", "Finding For Gold 18KT", "Finding for Plt.",
|
| 144 |
+
"Rhodium", "Stamping" # Add the new fields here
|
| 145 |
]
|
| 146 |
|
| 147 |
labour_columns = [
|
|
|
|
| 150 |
|
| 151 |
price_columns = [
|
| 152 |
"Price of USD 14K", "Price of USD 18K", "Price of USD PT",
|
| 153 |
+
"Price of GBP 14K @ 1/0.78", "Price of GBP 18K @ 1/0.78", "Price of GBP PT @ 1/0.78",
|
| 154 |
+
"14K Duty @ 2.5% Including", "18K Duty @ 2.5% Including", "PT950 Duty @ 2.5% Including",
|
| 155 |
+
"Freight",
|
| 156 |
+
"14K Landing Cost (Duty + Freight)", "18K Landing Cost (Duty + Freight)",
|
| 157 |
+
"PT950 Landing Cost (Duty + Freight)"
|
| 158 |
]
|
| 159 |
|
| 160 |
metal_data_basic = {k: v for k, v in metal_data.items()
|