-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (51 loc) · 2.1 KB
/
app.py
File metadata and controls
59 lines (51 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import streamlit as st
import os
from dotenv import load_dotenv
from satisfaction_tracker import SatisfactionTracker
# Load environment variables from .env
load_dotenv()
api_key = os.getenv("OPENROUTER_API_KEY")
st.set_page_config(page_title="Satisfaction Tracker", layout="centered")
st.title("📊 Customer Satisfaction Tracker")
# Initialize session state
if 'tracker' not in st.session_state:
st.session_state.tracker = None
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
if 'results' not in st.session_state:
st.session_state.results = []
# Automatically start tracker if API key exists
if api_key:
if st.session_state.tracker is None:
st.session_state.tracker = SatisfactionTracker(openrouter_api_key=api_key)
st.success("Tracker started using API key from .env.")
else:
st.error("API Key not found in .env file!")
if st.session_state.tracker:
st.subheader("💬 Chat Interface")
col1, col2 = st.columns([1, 4])
role = col1.selectbox("Role", ["user", "assistant"])
message = col2.text_input("Message", key="msg_input")
if st.button("Send Message"):
if message.strip() != "":
result = st.session_state.tracker.add_message(role, message.strip())
st.session_state.chat_history.append((role, message.strip()))
st.session_state.results.append(result)
else:
st.warning("Message cannot be empty.")
# Show chat history
st.markdown("---")
st.subheader("🗨️ Conversation")
for role, msg in st.session_state.chat_history:
align = "👤" if role == "user" else "🤖"
with st.chat_message(role):
st.markdown(f"{align} **{role.capitalize()}**: {msg}")
# Show results
if st.session_state.results:
st.markdown("---")
st.subheader("📈 Satisfaction Updates")
for res in reversed(st.session_state.results):
st.success(f"Updated Score: {res['updated_score']}/5 | Status: {res['status']}")
st.caption(f"Reason: {res['reason']}")
else:
st.info("Tracker not initialized due to missing API key.")