Spaces:
Sleeping
Sleeping
import gradio as gr | |
def convert_to_meters(feet): | |
"""Converts feet to meters.""" | |
return round(feet * 0.3048, 2) | |
def convert_to_feet(meters): | |
"""Converts meters to feet.""" | |
return round(meters / 0.3048, 2) | |
def calculate_area(length, width): | |
"""Calculates area from length and width.""" | |
return round(length * width, 2) | |
def app( | |
price_input, | |
room1_length_ft, room1_width_ft, room1_area_sqft, | |
room2_length_ft, room2_width_ft, room2_area_sqft, | |
room3_length_ft, room3_width_ft, room3_area_sqft, | |
room4_length_ft, room4_width_ft, room4_area_sqft, | |
room5_length_ft, room5_width_ft, room5_area_sqft, | |
price_per_sqft_input | |
): | |
try: | |
total_area_sqft = 0 | |
rooms = [] | |
for length_ft, width_ft, area_sqft in [ | |
(room1_length_ft, room1_width_ft, room1_area_sqft), | |
(room2_length_ft, room2_width_ft, room2_area_sqft), | |
(room3_length_ft, room3_width_ft, room3_area_sqft), | |
(room4_length_ft, room4_width_ft, room4_area_sqft), | |
(room5_length_ft, room5_width_ft, room5_area_sqft), | |
]: | |
if length_ft and width_ft: | |
length_ft, width_ft = float(length_ft), float(width_ft) | |
length_m, width_m = convert_to_meters(length_ft), convert_to_meters(width_ft) | |
area_sqft = calculate_area(length_ft, width_ft) | |
area_sqmt = round(area_sqft * 0.092903, 2) | |
elif area_sqft: | |
area_sqft = float(area_sqft) | |
area_sqmt = round(area_sqft * 0.092903, 2) | |
length_ft = width_ft = None | |
length_m = width_m = None | |
else: | |
continue | |
rooms.append((length_ft, width_ft, length_m, width_m, area_sqft, area_sqmt)) | |
total_area_sqft += area_sqft | |
total_area_sqmt = round(total_area_sqft * 0.092903, 2) | |
total_price = float(price_input) * 1000 | |
current_ppsqft = round(total_price / total_area_sqft, 2) | |
offers = [float(offer.strip()) for offer in price_per_sqft_input.split(",")] | |
offer_results = [(offer, round(offer * total_area_sqft, 2)) for offer in offers] | |
return ( | |
f"Total Area: {total_area_sqft:.2f} sqft ({total_area_sqmt:.2f} m²)", | |
rooms, | |
f"Current Price per Sqft: £{current_ppsqft:.2f}", | |
offer_results | |
) | |
except Exception as e: | |
return f"Error: {e}", None, None, None | |
iface = gr.Interface( | |
fn=app, | |
inputs=[ | |
gr.Textbox(label="Total Price (in thousands, e.g., 100 for £100k)"), | |
gr.Textbox(label="Room 1 Length (ft)"), gr.Textbox(label="Room 1 Width (ft)"), gr.Textbox(label="Room 1 Area (sqft)"), | |
gr.Textbox(label="Room 2 Length (ft)"), gr.Textbox(label="Room 2 Width (ft)"), gr.Textbox(label="Room 2 Area (sqft)"), | |
gr.Textbox(label="Room 3 Length (ft)"), gr.Textbox(label="Room 3 Width (ft)"), gr.Textbox(label="Room 3 Area (sqft)"), | |
gr.Textbox(label="Room 4 Length (ft)"), gr.Textbox(label="Room 4 Width (ft)"), gr.Textbox(label="Room 4 Area (sqft)"), | |
gr.Textbox(label="Room 5 Length (ft)"), gr.Textbox(label="Room 5 Width (ft)"), gr.Textbox(label="Room 5 Area (sqft)"), | |
gr.Textbox(label="Price Per Square Foot Offers (£, comma-separated, e.g., 250,270,300)"), | |
], | |
outputs=[ | |
"text", | |
gr.Dataframe(headers=["Length (ft)", "Width (ft)", "Length (m)", "Width (m)", "Area (sqft)", "Area (sqmt)"], | |
label="Room Details"), | |
"text", | |
gr.Dataframe(headers=["Price Per Sqft (£)", "Offer Price (£)"], | |
label="Price Offers"), | |
], | |
title="Room Area Calculator", | |
description="Enter dimensions for up to 5 rooms in feet or area directly in sqft. The tool calculates total area and provides price offers based on input values. Prices are in thousands (e.g., 100 = £100k)." | |
) | |
if __name__ == "__main__": | |
iface.launch() | |