Engineer786 commited on
Commit
23928c9
·
verified ·
1 Parent(s): c5c0c76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -82
app.py CHANGED
@@ -1,93 +1,53 @@
1
  import os
 
2
  import streamlit as st
3
- from tariff_scraper import fetch_tariff_data
4
  import pandas as pd
5
- from groq import Groq
6
-
7
- # Initialize Groq client
8
- client = Groq(api_key=os.environ.get('GroqApi'))
9
-
10
- # Predefined appliance list with typical loads in watts
11
- APPLIANCE_OPTIONS = {
12
- "Fan": 75,
13
- "Air Conditioner (1 Ton)": 1500,
14
- "Air Conditioner (1.5 Ton)": 2200,
15
- "Refrigerator": 150,
16
- "LED Bulb (20W)": 20,
17
- "Tube Light": 40,
18
- "Iron": 1000,
19
- "Microwave Oven": 1200,
20
- "Washing Machine": 500,
21
- "Electric Heater": 1500,
22
- "Laptop": 50,
23
- "Desktop Computer": 200,
24
- "Television (LCD/LED)": 120,
25
- "Water Pump": 1000,
26
- "Geyser (Electric)": 3000
27
  }
28
 
29
- if "appliance_data" not in st.session_state:
30
- st.session_state.appliance_data = []
31
- if "tariff_rate" not in st.session_state:
32
- st.session_state.tariff_rate = None
33
-
34
- def calculate_total_units():
35
- """Calculate total units consumed in kWh."""
36
- total_energy_wh = 0
37
- for data in st.session_state.appliance_data:
38
- total_energy_wh += data["quantity"] * data["load"] * data["usage_hours"]
39
- return total_energy_wh / 1000 # Convert watt-hours to kilowatt-hours
40
 
41
- def fetch_tariff_from_groq(units):
42
- """Fetch tariff rate based on units consumed using Groq API."""
43
- try:
44
- response = client.chat.completions.create(
45
- messages=[
46
- {
47
- "role": "user",
48
- "content": f"Provide tariff rate for {units} units consumed in PKR."
49
- }
50
- ],
51
- model="llama3-8b-8192",
52
- )
53
- return float(response.choices[0].message.content.strip())
54
- except Exception as e:
55
- st.error(f"Error fetching tariff from Groq: {e}")
56
- return None
57
 
58
- st.title("Dynamic Tariff Electricity Bill Calculator")
 
 
59
 
60
- # Add Appliances
61
- st.sidebar.subheader("Add Appliances")
62
- appliance = st.sidebar.selectbox("Select Appliance", options=list(APPLIANCE_OPTIONS.keys()))
63
- default_load = APPLIANCE_OPTIONS[appliance]
64
- quantity = st.sidebar.number_input("Number of Appliances", min_value=1, value=1)
65
- load = st.sidebar.number_input("Load (Watts per Appliance)", min_value=1, value=default_load)
66
- usage_hours = st.sidebar.number_input("Usage Time (Hours per Day)", min_value=0.0, value=6.0)
67
 
68
- if st.sidebar.button("Add Appliance"):
69
- st.session_state.appliance_data.append({
70
- "appliance": appliance,
71
- "load": load,
72
- "quantity": quantity,
73
- "usage_hours": usage_hours
74
- })
75
- st.sidebar.success(f"Added {quantity} {appliance}(s) to the list!")
76
 
77
- # Display Appliance List
78
- st.subheader("Appliance List")
79
- if st.session_state.appliance_data:
80
- for idx, data in enumerate(st.session_state.appliance_data, start=1):
81
- st.write(f"{idx}. {data['appliance']} - {data['quantity']} units, {data['load']}W each, {data['usage_hours']} hours/day")
82
- else:
83
- st.write("No appliances added.")
84
 
85
- # Calculate Monthly Units and Electricity Bill
86
- if st.button("Calculate Monthly Bill"):
87
- total_units_kwh = calculate_total_units() * 30 # Assuming 30 days in a month
88
- st.write(f"**Total Units Consumed (kWh): {total_units_kwh:.2f}**")
89
- tariff_rate = fetch_tariff_from_groq(total_units_kwh)
90
- if tariff_rate:
91
- monthly_bill = total_units_kwh * tariff_rate
92
- st.subheader("Electricity Bill")
93
- st.write(f"**Total Monthly Bill: PKR {monthly_bill:.2f}**")
 
1
  import os
2
+ import requests
3
  import streamlit as st
 
4
  import pandas as pd
5
+ from scraper import scrape_tariffs
6
+ from sentence_transformers import SentenceTransformer
7
+
8
+ # Replace the model loading approach with hf_hub_download to avoid cached_download error
9
+ def download_model():
10
+ model_repo_id = "sentence-transformers/all-MiniLM-L6-v2"
11
+
12
+ # Initialize Streamlit components
13
+ st.title("Electricity Bill Estimator")
14
+ st.sidebar.header("User Input")
15
+
16
+ tariff_urls = {
17
+ "IESCO": "https://iesco.com.pk/index.php/customer-services/tariff-guide",
18
+ "FESCO": "https://fesco.com.pk/tariff",
19
+ "HESCO": "http://www.hesco.gov.pk/htmls/tariffs.htm",
20
+ "KE": "https://www.ke.com.pk/customer-services/tariff-structure/",
21
+ "LESCO": "https://www.lesco.gov.pk/ElectricityTariffs",
22
+ "PESCO": "https://pesconlinebill.pk/pesco-tariff/",
23
+ "QESCO": "http://qesco.com.pk/Tariffs.aspx",
24
+ "TESCO": "https://tesco.gov.pk/index.php/electricity-traiff"
 
 
25
  }
26
 
27
+ def show_tariff_input():
28
+ # Display tariff rates selection
29
+ tariff_data = pd.read_csv("data/tariffs.csv")
30
+ tariff_types = tariff_data["category"].unique()
31
+ tariff_choice = st.selectbox("Select your tariff category:", tariff_types)
32
+ st.write(f"Selected Tariff: {tariff_choice}")
 
 
 
 
 
33
 
34
+ def scrape_data():
35
+ # Scraping tariff data using provided URLs
36
+ scrape_tariffs(list(tariff_urls.values()))
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ # Streamlit actions
39
+ if st.sidebar.button("Scrape Data"):
40
+ scrape_data()
41
 
42
+ if st.sidebar.button("Download Model"):
43
+ download_model()
 
 
 
 
 
44
 
45
+ # User inputs for appliance and usage time (replace placeholders as needed)
46
+ appliance_load = st.number_input("Enter appliance load in watts", min_value=10, max_value=5000, value=1000)
47
+ usage_time = st.number_input("Enter usage time (in hours)", min_value=1, max_value=24, value=5)
 
 
 
 
 
48
 
49
+ # Placeholder for electricity bill calculation and output display
50
+ if appliance_load and usage_time:
51
+ bill_amount = appliance_load * usage_time * 0.25 # Add your own calculation based on tariffs
52
+ st.write(f"Your electricity bill: {bill_amount} PKR")
 
 
 
53