Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pdfplumber
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# Function to extract text from PDF
|
| 7 |
+
def extract_text_from_pdf(pdf_path):
|
| 8 |
+
try:
|
| 9 |
+
with pdfplumber.open(pdf_path) as pdf:
|
| 10 |
+
text = "".join([page.extract_text() for page in pdf.pages])
|
| 11 |
+
return text
|
| 12 |
+
except Exception as e:
|
| 13 |
+
return str(e)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# Function to extract numeric rates
|
| 17 |
+
def extract_rates(text):
|
| 18 |
+
rates = re.findall(r"\b\d+(?:\.\d+)?\b", text) # Regex for numbers, including decimals
|
| 19 |
+
return rates
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# Function to calculate electricity bill
|
| 23 |
+
def calculate_bill(units, rate_per_unit):
|
| 24 |
+
return units * rate_per_unit
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# Streamlit App
|
| 28 |
+
def main():
|
| 29 |
+
st.title("Tariff Rate Extractor and Electricity Bill Calculator")
|
| 30 |
+
st.sidebar.title("Options")
|
| 31 |
+
|
| 32 |
+
app_mode = st.sidebar.selectbox(
|
| 33 |
+
"Choose the mode:",
|
| 34 |
+
["Extract Rates from PDF", "Calculate Electricity Bill"]
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
if app_mode == "Extract Rates from PDF":
|
| 38 |
+
st.header("Upload a PDF to Extract Rates")
|
| 39 |
+
pdf_file = st.file_uploader("Upload PDF", type=["pdf"])
|
| 40 |
+
|
| 41 |
+
if pdf_file is not None:
|
| 42 |
+
with open(f"/tmp/{pdf_file.name}", "wb") as f:
|
| 43 |
+
f.write(pdf_file.getbuffer())
|
| 44 |
+
|
| 45 |
+
# Extract text and rates
|
| 46 |
+
text = extract_text_from_pdf(f"/tmp/{pdf_file.name}")
|
| 47 |
+
rates = extract_rates(text)
|
| 48 |
+
|
| 49 |
+
st.subheader("Extracted Numeric Rates")
|
| 50 |
+
if rates:
|
| 51 |
+
st.write(rates)
|
| 52 |
+
else:
|
| 53 |
+
st.write("No numeric rates found in the document.")
|
| 54 |
+
|
| 55 |
+
elif app_mode == "Calculate Electricity Bill":
|
| 56 |
+
st.header("Electricity Bill Calculator")
|
| 57 |
+
|
| 58 |
+
units = st.number_input(
|
| 59 |
+
"Enter the number of units consumed (kWh):", min_value=0.0, step=0.1
|
| 60 |
+
)
|
| 61 |
+
rate_per_unit = st.number_input(
|
| 62 |
+
"Enter the rate per unit (e.g., 5.5):", min_value=0.0, step=0.1
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
if st.button("Calculate Bill"):
|
| 66 |
+
total_bill = calculate_bill(units, rate_per_unit)
|
| 67 |
+
st.success(f"Your total electricity bill is: ₹{total_bill:.2f}")
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
main()
|