Engineer786 commited on
Commit
837e6e4
·
verified ·
1 Parent(s): 35382f7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from PyPDF2 import PdfReader
4
+ from sentence_transformers import SentenceTransformer
5
+ import faiss
6
+ from groq import Groq
7
+ import json
8
+
9
+ # Set up Groq client
10
+ client = Groq(api_key=os.environ.get('GroqApi'))
11
+
12
+ # Load the model
13
+ model = SentenceTransformer('all-MiniLM-L6-v2') # Use an open-source embedding model
14
+
15
+ # Initialize FAISS
16
+ dimension = 384 # Embedding dimension for 'all-MiniLM-L6-v2'
17
+ index = faiss.IndexFlatL2(dimension)
18
+
19
+ # Streamlit app
20
+ st.title("Electricity Bill Calculation App")
21
+
22
+ # File upload
23
+ uploaded_file = st.file_uploader("Upload your electricity bill PDF", type="pdf")
24
+
25
+ if uploaded_file:
26
+ # Extract text from PDF
27
+ reader = PdfReader(uploaded_file)
28
+ text = " ".join([page.extract_text() for page in reader.pages])
29
+
30
+ # Tokenize and chunk text
31
+ sentences = text.split(". ") # Simple sentence splitting
32
+ embeddings = model.encode(sentences)
33
+
34
+ # Store embeddings in FAISS
35
+ faiss.normalize_L2(embeddings)
36
+ index.add(embeddings)
37
+
38
+ # Extract company and user type
39
+ company = None
40
+ if "LESCO" in text.upper():
41
+ company = "LESCO"
42
+ elif "FESCO" in text.upper():
43
+ company = "FESCO"
44
+
45
+ user_type = None
46
+ if "PROTECTED" in text.upper():
47
+ user_type = "Protected"
48
+ elif "UNPROTECTED" in text.upper():
49
+ user_type = "Unprotected"
50
+
51
+ st.write(f"Detected Company: {company}")
52
+ st.write(f"Detected User Type: {user_type}")
53
+
54
+ if company and user_type:
55
+ # Appliance usage input
56
+ st.subheader("Appliance Usage Details")
57
+ num_appliances = st.number_input("Number of appliances", min_value=1, max_value=20, step=1)
58
+
59
+ appliance_data = []
60
+ for i in range(num_appliances):
61
+ st.write(f"Appliance {i + 1}")
62
+ name = st.text_input(f"Name of Appliance {i + 1}", key=f"appliance_{i}")
63
+ power = st.number_input(f"Power (Watts) of {name}", key=f"power_{i}")
64
+ hours = st.number_input(f"Usage hours per day for {name}", key=f"hours_{i}")
65
+ if name and power and hours:
66
+ appliance_data.append({"name": name, "power": power, "hours": hours})
67
+
68
+ if st.button("Calculate Bill"):
69
+ # Calculate total units
70
+ total_units = sum([(appliance["power"] * appliance["hours"] * 30) / 1000 for appliance in appliance_data])
71
+ st.write(f"Total Units Consumed: {total_units:.2f} kWh")
72
+
73
+ # Get tariff rate from Groq
74
+ query_content = {
75
+ "company": company,
76
+ "user_type": user_type,
77
+ "units": total_units,
78
+ }
79
+ chat_completion = client.chat.completions.create(
80
+ messages=[{"role": "user", "content": json.dumps(query_content)}],
81
+ model="llama3-8b-8192",
82
+ )
83
+ tariff_rate = float(chat_completion.choices[0].message.content.strip())
84
+
85
+ # Calculate and display bill
86
+ total_bill = tariff_rate * total_units
87
+ st.write(f"Tariff Rate: {tariff_rate} PKR/kWh")
88
+ st.write(f"Total Bill: {total_bill:.2f} PKR")
89
+
90
+ # Run Streamlit using: streamlit run app.py