Spaces:
Sleeping
Sleeping
costs
Browse files
bill.py
CHANGED
|
@@ -78,12 +78,14 @@ def parseBill(data):
|
|
| 78 |
|
| 79 |
|
| 80 |
return {
|
| 81 |
-
"
|
| 82 |
-
"
|
| 83 |
-
"
|
| 84 |
-
"
|
| 85 |
-
"
|
| 86 |
-
"
|
|
|
|
|
|
|
| 87 |
|
| 88 |
}
|
| 89 |
|
|
@@ -129,6 +131,29 @@ def process_query(query, user_id, model_name):
|
|
| 129 |
|
| 130 |
return context
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
def main():
|
| 133 |
st.title("Telecom Bill Chat with LLM Agent")
|
| 134 |
|
|
@@ -140,41 +165,54 @@ def main():
|
|
| 140 |
user_id = st.sidebar.text_input("Introdu numărul de telefon:")
|
| 141 |
# display the user data if the user_id is set
|
| 142 |
#st.write(f"User ID: {user_id}")
|
| 143 |
-
|
|
|
|
|
|
|
| 144 |
if user_id and user_id != st.session_state.user_id:
|
| 145 |
data = load_user_data(user_id)
|
|
|
|
| 146 |
if data:
|
| 147 |
st.session_state.user_id = user_id
|
| 148 |
st.success("Utilizator găsit!")
|
|
|
|
|
|
|
|
|
|
| 149 |
else:
|
| 150 |
-
st.warning("Nu am găsit date pentru acest ID.
|
|
|
|
| 151 |
st.session_state.user_id = user_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
uploaded_file = st.file_uploader("Upload JSON Bill", type="json")
|
| 154 |
-
if uploaded_file and st.session_state.user_id:
|
| 155 |
-
bill_data = json.load(uploaded_file)
|
| 156 |
-
parsed_bill = parseBill(bill_data)
|
| 157 |
-
existing_data = load_user_data(st.session_state.user_id)
|
| 158 |
-
|
| 159 |
-
# Check if the billNo already exists in the existing data
|
| 160 |
-
existing_bill_nos = [bill.get("billNo") for bill in existing_data.get("bills", [])]
|
| 161 |
-
if parsed_bill.get("billNo") in existing_bill_nos:
|
| 162 |
-
st.warning("Factura existentă.")
|
| 163 |
-
else:
|
| 164 |
-
if "bills" not in existing_data:
|
| 165 |
-
existing_data["bills"] = []
|
| 166 |
-
existing_data["bills"].append(parsed_bill)
|
| 167 |
-
save_user_data(st.session_state.user_id, existing_data)
|
| 168 |
-
st.success("Factura a fost încărcată și salvată cu succes!")
|
| 169 |
-
|
| 170 |
-
if st.session_state.user_id:
|
| 171 |
-
data = load_user_data(st.session_state.user_id)
|
| 172 |
-
st.write(f"Numar telefon: {st.session_state.user_id}")
|
| 173 |
-
st.write("Facturi existente:")
|
| 174 |
-
for bill in data.get("bills", []):
|
| 175 |
-
st.write(bill)
|
| 176 |
-
else:
|
| 177 |
-
st.info("Introduceți un ID și/sau încărcați o factură JSON pentru a continua.")
|
| 178 |
|
| 179 |
# Initialize conversation in the session state
|
| 180 |
# "context_prompt_added" indicates whether we've added the specialized "bill info" context yet.
|
|
@@ -193,7 +231,7 @@ def main():
|
|
| 193 |
|
| 194 |
if prompt := st.chat_input("Introduceți întrebarea aici:"):
|
| 195 |
if not st.session_state.user_id:
|
| 196 |
-
st.error("Trebuie să
|
| 197 |
return
|
| 198 |
|
| 199 |
# If the context prompt hasn't been added yet, build & inject it once;
|
|
@@ -225,5 +263,32 @@ def main():
|
|
| 225 |
st.write("Completion tokens:", completion.usage.completion_tokens)
|
| 226 |
st.write("Total tokens:", completion.usage.total_tokens)
|
| 227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
if __name__ == "__main__":
|
| 229 |
main()
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
return {
|
| 81 |
+
"Data Factura": billDate,
|
| 82 |
+
"Serie numar factura": billNo,
|
| 83 |
+
"De plata": amountDue,
|
| 84 |
+
"Costuri suplimentare": extraCharge,
|
| 85 |
+
"Total plata factura": totalBillCosts,
|
| 86 |
+
"Costuri utilizatori": subscriberCosts,
|
| 87 |
+
"Lista categorii": list(categories),
|
| 88 |
+
"Lista costuri": list(names)
|
| 89 |
|
| 90 |
}
|
| 91 |
|
|
|
|
| 131 |
|
| 132 |
return context
|
| 133 |
|
| 134 |
+
# import the datetime class from the datetime module
|
| 135 |
+
from datetime import datetime
|
| 136 |
+
def log_conversation(user_id, user_query, assistant_response, tokens, cost):
|
| 137 |
+
log_entry = {
|
| 138 |
+
"timestamp": datetime.now().isoformat(),
|
| 139 |
+
"user_id": user_id,
|
| 140 |
+
"user_query": user_query,
|
| 141 |
+
"assistant_response": assistant_response,
|
| 142 |
+
"tokens": tokens,
|
| 143 |
+
"cost": cost
|
| 144 |
+
}
|
| 145 |
+
log_file_path = os.path.join("logs", "conversation_logs.json")
|
| 146 |
+
os.makedirs(os.path.dirname(log_file_path), exist_ok=True)
|
| 147 |
+
if os.path.exists(log_file_path):
|
| 148 |
+
with open(log_file_path, "r") as log_file:
|
| 149 |
+
logs = json.load(log_file)
|
| 150 |
+
else:
|
| 151 |
+
logs = []
|
| 152 |
+
logs.append(log_entry)
|
| 153 |
+
with open(log_file_path, "w") as log_file:
|
| 154 |
+
json.dump(logs, log_file, indent=4)
|
| 155 |
+
|
| 156 |
+
|
| 157 |
def main():
|
| 158 |
st.title("Telecom Bill Chat with LLM Agent")
|
| 159 |
|
|
|
|
| 165 |
user_id = st.sidebar.text_input("Introdu numărul de telefon:")
|
| 166 |
# display the user data if the user_id is set
|
| 167 |
#st.write(f"User ID: {user_id}")
|
| 168 |
+
|
| 169 |
+
st.session_state.user_data = None
|
| 170 |
+
|
| 171 |
if user_id and user_id != st.session_state.user_id:
|
| 172 |
data = load_user_data(user_id)
|
| 173 |
+
st.session_state.user_data = data
|
| 174 |
if data:
|
| 175 |
st.session_state.user_id = user_id
|
| 176 |
st.success("Utilizator găsit!")
|
| 177 |
+
st.write(f"Numar telefon: {st.session_state.user_id}")
|
| 178 |
+
st.session_state.user_data = data
|
| 179 |
+
|
| 180 |
else:
|
| 181 |
+
st.warning("Nu am găsit date pentru acest ID.")
|
| 182 |
+
st.warning("Încărcați o factură json.")
|
| 183 |
st.session_state.user_id = user_id
|
| 184 |
+
st.session_state.user_data = None
|
| 185 |
+
|
| 186 |
+
# If the user has no data yet Show the upload (st.file_uploader...) dialog ,
|
| 187 |
+
# If the user has stored data in data\user_data\"user_data{user_id}.json, display the existing bills data - st.write(bill) but compacted
|
| 188 |
+
if st.session_state.user_data:
|
| 189 |
+
st.write("Facturi existente (extras):")
|
| 190 |
+
for bill in st.session_state.user_data.get("bills", []):
|
| 191 |
+
st.write({
|
| 192 |
+
"Data factura": bill.get("billDate"),
|
| 193 |
+
"Numar serie factura": bill.get("billNo"),
|
| 194 |
+
"De plata": bill.get("amountDue"),
|
| 195 |
+
"Costuri suplimentare": bill.get("extraCharge")
|
| 196 |
+
})
|
| 197 |
+
if not st.session_state.user_data:
|
| 198 |
+
uploaded_file = st.file_uploader("Incarca factura", type="json")
|
| 199 |
+
if uploaded_file and st.session_state.user_id:
|
| 200 |
+
bill_data = json.load(uploaded_file)
|
| 201 |
+
parsed_bill = parseBill(bill_data)
|
| 202 |
+
existing_data = load_user_data(st.session_state.user_id)
|
| 203 |
+
|
| 204 |
+
# Check if the billNo already exists in the existing data
|
| 205 |
+
existing_bill_nos = [bill.get("billNo") for bill in existing_data.get("bills", [])]
|
| 206 |
+
if parsed_bill.get("billNo") in existing_bill_nos:
|
| 207 |
+
st.warning("Factură existentă.")
|
| 208 |
+
else:
|
| 209 |
+
if "bills" not in existing_data:
|
| 210 |
+
existing_data["bills"] = []
|
| 211 |
+
existing_data["bills"].append(parsed_bill)
|
| 212 |
+
save_user_data(st.session_state.user_id, existing_data)
|
| 213 |
+
st.success("Factura a fost încărcată și salvată cu succes!")
|
| 214 |
+
|
| 215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
# Initialize conversation in the session state
|
| 218 |
# "context_prompt_added" indicates whether we've added the specialized "bill info" context yet.
|
|
|
|
| 231 |
|
| 232 |
if prompt := st.chat_input("Introduceți întrebarea aici:"):
|
| 233 |
if not st.session_state.user_id:
|
| 234 |
+
st.error("Trebuie să introduci un număr de telefon valid sau să încarci date.")
|
| 235 |
return
|
| 236 |
|
| 237 |
# If the context prompt hasn't been added yet, build & inject it once;
|
|
|
|
| 263 |
st.write("Completion tokens:", completion.usage.completion_tokens)
|
| 264 |
st.write("Total tokens:", completion.usage.total_tokens)
|
| 265 |
|
| 266 |
+
# Estimate cost per conversation (find the OpenAI costs for gpt-4o and gpt-4o-mini model per token)
|
| 267 |
+
prompt_tokens = completion.usage.prompt_tokens
|
| 268 |
+
completion_tokens = completion.usage.completion_tokens
|
| 269 |
+
total_tokens = completion.usage.total_tokens
|
| 270 |
+
|
| 271 |
+
# Estimate cost per conversation
|
| 272 |
+
if model_name == "gpt-4o":
|
| 273 |
+
cost_per_token = 0.03 / 1000 # $0.03 per 1,000 tokens
|
| 274 |
+
elif model_name == "gpt-4o-mini":
|
| 275 |
+
cost_per_token = 0.015 / 1000 # $0.015 per 1,000 tokens
|
| 276 |
+
|
| 277 |
+
estimated_cost = total_tokens * cost_per_token
|
| 278 |
+
st.write("Estimated cost:", estimated_cost)
|
| 279 |
+
|
| 280 |
+
# Log the conversation
|
| 281 |
+
log_conversation(
|
| 282 |
+
st.session_state["user_id"],
|
| 283 |
+
prompt,
|
| 284 |
+
response_text,
|
| 285 |
+
{
|
| 286 |
+
"prompt_tokens": prompt_tokens,
|
| 287 |
+
"completion_tokens": completion_tokens,
|
| 288 |
+
"total_tokens": total_tokens
|
| 289 |
+
},
|
| 290 |
+
estimated_cost
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
if __name__ == "__main__":
|
| 294 |
main()
|