Spaces:
Running
Running
Upload med_streamlit.py
Browse files- med_streamlit.py +22 -7
med_streamlit.py
CHANGED
@@ -49,6 +49,19 @@ def update_dataframe(records: List[Dict] | pd.DataFrame):
|
|
49 |
st.session_state.df = new_data # Assign the updated DataFrame
|
50 |
#st.rerun() # Trigger a rerun
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# Page config
|
54 |
st.set_page_config(layout="wide", page_title="RP Medication Analyzer")
|
@@ -114,6 +127,9 @@ if "last_response" not in st.session_state:
|
|
114 |
st.session_state.last_response = {}
|
115 |
if "history" not in st.session_state:
|
116 |
st.session_state.history = [] # Stores all past interactions
|
|
|
|
|
|
|
117 |
|
118 |
# File uploader
|
119 |
file = st.file_uploader("Upload an Excel file", type=["xlsx"])
|
@@ -129,7 +145,7 @@ if file and file != st.session_state.uploaded_file:
|
|
129 |
update_dataframe(data_df)
|
130 |
else:
|
131 |
st.error("🚨 No 'Data' sheet found in the uploaded file. Make sure the file has it")
|
132 |
-
|
133 |
st.session_state.uploaded_file = file
|
134 |
print("File uploaded successfully!")
|
135 |
st.success("✅ File uploaded successfully!")
|
@@ -171,7 +187,6 @@ if input_text:
|
|
171 |
else:
|
172 |
# Convert dataframe to JSON for LLM processing
|
173 |
json_data = st.session_state.df.to_json(orient="records")
|
174 |
-
print(json_data)
|
175 |
with st.spinner(f"Processing request: *{input_text}*..."):
|
176 |
response = None # Ensure response is defined before use
|
177 |
|
@@ -191,14 +206,11 @@ if input_text:
|
|
191 |
api_key=api_key,
|
192 |
)
|
193 |
|
194 |
-
print(f"Response:{response}")
|
195 |
-
|
196 |
# Ensure response exists before processing
|
197 |
if response:
|
198 |
-
st.session_state.
|
199 |
try:
|
200 |
parsed_response = validate_llm_response(response)
|
201 |
-
print(f"Parsed response: {parsed_response}")
|
202 |
|
203 |
st.session_state.last_prompt = input_text
|
204 |
st.session_state.last_response = response # Keep full JSON response
|
@@ -207,7 +219,6 @@ if input_text:
|
|
207 |
if "error" in parsed_response:
|
208 |
st.error(parsed_response["error"])
|
209 |
else:
|
210 |
-
print(f"Parsed data: {parsed_response['dataset']}")
|
211 |
update_dataframe(parsed_response["dataset"])
|
212 |
st.session_state.explanation = parsed_response["explanation"]
|
213 |
st.session_state.references = parsed_response["references"]
|
@@ -260,3 +271,7 @@ if st.session_state.df is not None:
|
|
260 |
file_name="updated_dataset.xlsx",
|
261 |
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
262 |
)
|
|
|
|
|
|
|
|
|
|
49 |
st.session_state.df = new_data # Assign the updated DataFrame
|
50 |
#st.rerun() # Trigger a rerun
|
51 |
|
52 |
+
def undo():
|
53 |
+
"""Undo the last operation by restoring the previous DataFrame."""
|
54 |
+
if st.session_state.prev_df is not None:
|
55 |
+
st.session_state.df = st.session_state.prev_df
|
56 |
+
st.session_state.prev_df = None
|
57 |
+
|
58 |
+
if st.session_state.history:
|
59 |
+
st.session_state.history.pop()
|
60 |
+
if st.session_state.explanation:
|
61 |
+
st.session_state.explanation = "Changes undone."
|
62 |
+
if st.session_state.references:
|
63 |
+
st.session_state.references = ""
|
64 |
+
|
65 |
|
66 |
# Page config
|
67 |
st.set_page_config(layout="wide", page_title="RP Medication Analyzer")
|
|
|
127 |
st.session_state.last_response = {}
|
128 |
if "history" not in st.session_state:
|
129 |
st.session_state.history = [] # Stores all past interactions
|
130 |
+
if "prev_df" not in st.session_state:
|
131 |
+
st.session_state.prev_df = None # Stores the previous DataFrame for undo
|
132 |
+
|
133 |
|
134 |
# File uploader
|
135 |
file = st.file_uploader("Upload an Excel file", type=["xlsx"])
|
|
|
145 |
update_dataframe(data_df)
|
146 |
else:
|
147 |
st.error("🚨 No 'Data' sheet found in the uploaded file. Make sure the file has it")
|
148 |
+
print(f"History: {st.session_state.history}")
|
149 |
st.session_state.uploaded_file = file
|
150 |
print("File uploaded successfully!")
|
151 |
st.success("✅ File uploaded successfully!")
|
|
|
187 |
else:
|
188 |
# Convert dataframe to JSON for LLM processing
|
189 |
json_data = st.session_state.df.to_json(orient="records")
|
|
|
190 |
with st.spinner(f"Processing request: *{input_text}*..."):
|
191 |
response = None # Ensure response is defined before use
|
192 |
|
|
|
206 |
api_key=api_key,
|
207 |
)
|
208 |
|
|
|
|
|
209 |
# Ensure response exists before processing
|
210 |
if response:
|
211 |
+
st.session_state.prev_df = st.session_state.df
|
212 |
try:
|
213 |
parsed_response = validate_llm_response(response)
|
|
|
214 |
|
215 |
st.session_state.last_prompt = input_text
|
216 |
st.session_state.last_response = response # Keep full JSON response
|
|
|
219 |
if "error" in parsed_response:
|
220 |
st.error(parsed_response["error"])
|
221 |
else:
|
|
|
222 |
update_dataframe(parsed_response["dataset"])
|
223 |
st.session_state.explanation = parsed_response["explanation"]
|
224 |
st.session_state.references = parsed_response["references"]
|
|
|
271 |
file_name="updated_dataset.xlsx",
|
272 |
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
273 |
)
|
274 |
+
|
275 |
+
st.sidebar.subheader("Undo Changes")
|
276 |
+
if st.sidebar.button("Undo", disabled=st.session_state.prev_df is None):
|
277 |
+
undo()
|