app.py
Browse files
app.py
CHANGED
@@ -50,35 +50,22 @@ if not OPENAI_API_KEY or not ASSISTANT_CONTRACT_ID or not ASSISTANT_TECHNICAL_ID
|
|
50 |
# Tabs
|
51 |
tab1, tab2, tab3 = st.tabs(["Contract", "Technical", "Flagged Responses"])
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
# Ensure the flagged responses file exists
|
56 |
-
def ensure_flagged_file():
|
57 |
-
if not os.path.exists(FLAGGED_RESPONSES_FILE):
|
58 |
-
with open(FLAGGED_RESPONSES_FILE, "w") as file:
|
59 |
-
json.dump([], file) # Initialize with an empty list
|
60 |
-
|
61 |
-
ensure_flagged_file()
|
62 |
|
63 |
def save_flagged_response(user_query, ai_response):
|
|
|
|
|
64 |
flagged_data = {
|
65 |
"query": user_query,
|
66 |
"response": ai_response,
|
67 |
"timestamp": datetime.now().isoformat()
|
68 |
}
|
69 |
|
70 |
-
with open(
|
71 |
-
|
72 |
-
existing_data = json.load(file)
|
73 |
-
except json.JSONDecodeError:
|
74 |
-
existing_data = []
|
75 |
-
|
76 |
-
existing_data.append(flagged_data)
|
77 |
-
|
78 |
-
with open(FLAGGED_RESPONSES_FILE, "w") as file:
|
79 |
-
json.dump(existing_data, file, indent=4)
|
80 |
|
81 |
-
st.success("Response flagged
|
82 |
|
83 |
# Contract Chat Section
|
84 |
def contract_chat_section(tab, assistant_id, session_key, input_key):
|
@@ -144,11 +131,14 @@ contract_chat_section(tab2, ASSISTANT_TECHNICAL_ID, "technical_messages", "techn
|
|
144 |
# Flagged Responses Tab
|
145 |
with tab3:
|
146 |
st.subheader("Flagged Responses")
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
151 |
else:
|
152 |
-
st.info("No flagged responses available.")
|
153 |
-
|
154 |
-
|
|
|
50 |
# Tabs
|
51 |
tab1, tab2, tab3 = st.tabs(["Contract", "Technical", "Flagged Responses"])
|
52 |
|
53 |
+
FLAGGED_RESPONSES_DIR = "flagged_responses"
|
54 |
+
os.makedirs(FLAGGED_RESPONSES_DIR, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
def save_flagged_response(user_query, ai_response):
|
57 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
58 |
+
filename = f"{FLAGGED_RESPONSES_DIR}/flagged_{timestamp}.json"
|
59 |
flagged_data = {
|
60 |
"query": user_query,
|
61 |
"response": ai_response,
|
62 |
"timestamp": datetime.now().isoformat()
|
63 |
}
|
64 |
|
65 |
+
with open(filename, "w") as file:
|
66 |
+
json.dump(flagged_data, file, indent=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
st.success(f"Response flagged and saved as {filename}.")
|
69 |
|
70 |
# Contract Chat Section
|
71 |
def contract_chat_section(tab, assistant_id, session_key, input_key):
|
|
|
131 |
# Flagged Responses Tab
|
132 |
with tab3:
|
133 |
st.subheader("Flagged Responses")
|
134 |
+
flagged_files = [f for f in os.listdir(FLAGGED_RESPONSES_DIR) if f.endswith(".json")]
|
135 |
+
|
136 |
+
if flagged_files:
|
137 |
+
selected_file = st.selectbox("Select a flagged response file to download:", flagged_files)
|
138 |
+
|
139 |
+
if selected_file:
|
140 |
+
with open(os.path.join(FLAGGED_RESPONSES_DIR, selected_file), "r") as file:
|
141 |
+
flagged_responses = file.read()
|
142 |
+
st.download_button("Download Selected Flagged Responses", data=flagged_responses, file_name=selected_file, mime="application/json")
|
143 |
else:
|
144 |
+
st.info("No flagged responses available.")
|
|
|
|