Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,13 @@ 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') #
|
| 14 |
|
| 15 |
# Initialize FAISS
|
| 16 |
dimension = 384 # Embedding dimension for 'all-MiniLM-L6-v2'
|
|
@@ -80,11 +81,27 @@ if uploaded_file:
|
|
| 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 |
-
#
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import faiss
|
| 6 |
from groq import Groq
|
| 7 |
import json
|
| 8 |
+
import re
|
| 9 |
|
| 10 |
# Set up Groq client
|
| 11 |
client = Groq(api_key=os.environ.get('GroqApi'))
|
| 12 |
|
| 13 |
+
# Load the embedding model
|
| 14 |
+
model = SentenceTransformer('all-MiniLM-L6-v2') # Open-source embedding model
|
| 15 |
|
| 16 |
# Initialize FAISS
|
| 17 |
dimension = 384 # Embedding dimension for 'all-MiniLM-L6-v2'
|
|
|
|
| 81 |
messages=[{"role": "user", "content": json.dumps(query_content)}],
|
| 82 |
model="llama3-8b-8192",
|
| 83 |
)
|
|
|
|
| 84 |
|
| 85 |
+
# Extract numeric value from the response
|
| 86 |
+
response_content = chat_completion.choices[0].message.content.strip()
|
| 87 |
+
|
| 88 |
+
try:
|
| 89 |
+
# Attempt to parse the response as JSON or extract the numeric value
|
| 90 |
+
if response_content.startswith('{') and response_content.endswith('}'):
|
| 91 |
+
# Parse JSON if valid
|
| 92 |
+
response_data = json.loads(response_content)
|
| 93 |
+
tariff_rate = response_data.get('tariff_rate', 0.0) # Replace 'tariff_rate' with correct key
|
| 94 |
+
else:
|
| 95 |
+
# Extract numeric value using regex
|
| 96 |
+
match = re.search(r"[-+]?\d*\.\d+|\d+", response_content)
|
| 97 |
+
tariff_rate = float(match.group()) if match else 0.0
|
| 98 |
+
|
| 99 |
+
# Calculate and display the bill
|
| 100 |
+
total_bill = tariff_rate * total_units
|
| 101 |
+
st.write(f"Tariff Rate: {tariff_rate} PKR/kWh")
|
| 102 |
+
st.write(f"Total Bill: {total_bill:.2f} PKR")
|
| 103 |
+
|
| 104 |
+
except ValueError as e:
|
| 105 |
+
st.error(f"Error parsing Groq response: {e}")
|
| 106 |
+
st.write("Groq response content:")
|
| 107 |
+
st.write(response_content)
|