omri374 commited on
Commit
ca27ac1
·
verified ·
1 Parent(s): fd2a4e0

Delete med_streamlit.py

Browse files
Files changed (1) hide show
  1. med_streamlit.py +0 -277
med_streamlit.py DELETED
@@ -1,277 +0,0 @@
1
- import os
2
- import json
3
- import io
4
- from typing import Dict, List
5
-
6
- import pandas as pd
7
- import streamlit as st
8
- from dotenv import load_dotenv
9
- from openai import OpenAI
10
-
11
- import llm_calls
12
- from llm_calls import validate_llm_response
13
-
14
- # Load environment variables
15
- load_dotenv()
16
-
17
- CONDITION_NAME = "Retinitis Pigmentosa (RP)"
18
-
19
- SYSTEM_PROMPT = f"""
20
- You are a medical assistant specialized in modifying structured medical data.
21
- You will receive JSON input representing a dataset of medications for {CONDITION_NAME}.
22
-
23
- Your task is to:
24
- - Answer user requests about the provided medication data
25
- - Either Add new columns or rows if requested, or modify existing ones
26
- - Provide references, explanations and additional remarks
27
- Always return only a JSON object with:
28
- - "dataset": updated dataset
29
- - "explanation": explanation of changes and additional information related to the findings.
30
- Specify the change made for each medication
31
- - "references": References for findings, i.e. links to scientific papers or websites.
32
- Specify which reference relates to which finding on each medication.
33
-
34
- Additional guidelines:
35
- 1. Please respond in valid JSON format only.
36
- 2. Make sure the JSON is valid, e.g. has no unterminated strings or missing commas.
37
- 3. Ensure the response starts with `{{` and ends with `}}` without any trailing text.
38
- """
39
-
40
-
41
- def update_dataframe(records: List[Dict] | pd.DataFrame):
42
- """Update the DataFrame with new records. """
43
- print(f"UPDATING DATAFRAME: {records}")
44
- if isinstance(records, pd.DataFrame):
45
- new_data = records
46
- else:
47
- new_data = pd.DataFrame(records)
48
-
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")
68
- col1, col2 = st.columns([2, 18])
69
- col1.image("rp_logo.jpg", use_container_width=True)
70
- col2.title("Analyze RP Related Medications")
71
-
72
- # Sidebar for API Key settings
73
- with st.sidebar:
74
- st.subheader("Select AI service")
75
- llm_provider = st.radio(options=["Perplexity.ai", "OpenAI"], index=0, label="API")
76
-
77
- api_key = None # Initialize API key
78
-
79
- if llm_provider == "OpenAI":
80
- st.subheader("OpenAI API key")
81
- api_base_input = st.text_input(
82
- "Enter API Base (Leave empty to use env variable)",
83
- value=os.environ.get("OPENAI_API_BASE", ""),
84
- )
85
- api_key_input = st.text_input(
86
- "Enter API Key",
87
- type="password",
88
- value=os.environ.get("OPENAI_API_KEY", ""),
89
- )
90
-
91
- openai_api_base = api_base_input if api_base_input else os.environ.get("OPENAI_API_BASE")
92
- api_key = api_key_input if api_key_input else os.environ.get("OPENAI_API_KEY")
93
-
94
- # Validate API key presence
95
- if not api_key:
96
- st.error("🚨 OpenAI API key is required!")
97
-
98
- openai_client = OpenAI(api_key=api_key)
99
- openai_client.api_base = openai_api_base
100
-
101
- elif llm_provider == "Perplexity.ai":
102
- st.subheader("Perplexity.ai API key")
103
- api_key_input = st.text_input(
104
- "Enter API Key",
105
- type="password",
106
- value=os.environ.get("PERPLEXITY_API_KEY", ""),
107
- )
108
- api_key = api_key_input if api_key_input else os.environ.get("PERPLEXITY_API_KEY")
109
-
110
- # Validate API key presence
111
- if not api_key:
112
- st.error("🚨 Perplexity.ai API key is required!")
113
-
114
-
115
- # Ensure session persistence
116
- if "df" not in st.session_state:
117
- st.session_state.df = None
118
- if "uploaded_file" not in st.session_state:
119
- st.session_state.uploaded_file = None
120
- if "explanation" not in st.session_state:
121
- st.session_state.explanation = "No modifications yet."
122
- if "references" not in st.session_state:
123
- st.session_state.references = "No additional references."
124
- if "last_prompt" not in st.session_state:
125
- st.session_state.last_prompt = ""
126
- if "last_response" not in st.session_state:
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"])
136
-
137
- print(f"FILE: {file}")
138
- if file and file != st.session_state.uploaded_file:
139
- try:
140
- with pd.ExcelFile(file) as xls:
141
- if "Metadata" in xls.sheet_names:
142
- st.session_state.history = pd.read_excel(xls, sheet_name="Metadata").to_dict(orient="records")
143
- if "Data" in xls.sheet_names:
144
- data_df = pd.read_excel(xls, sheet_name="Data")
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!")
152
- except Exception as e:
153
- print(f"Error reading file: {e}")
154
- st.error(f"🚨 Error reading file: {e}")
155
-
156
-
157
- if st.session_state.df is not None:
158
- st.write("### Updated Dataset")
159
- st.dataframe(st.session_state.df, use_container_width=True)
160
- else:
161
- st.warning("⚠️ Upload a file to proceed.")
162
-
163
- # Explanation & remarks
164
- if st.session_state.explanation:
165
- with st.expander("Explanation and remarks"):
166
- st.info(st.session_state.explanation)
167
- if st.session_state.references:
168
- with st.expander("References"):
169
- st.warning(st.session_state.references)
170
- if st.session_state.last_prompt:
171
- with st.expander("📜 Sent Prompt"):
172
- st.code(st.session_state.last_prompt, language="plaintext")
173
-
174
- # if st.session_state.last_response:
175
- # with st.expander("🧠 LLM Response (Raw)"):
176
- # st.json(st.session_state.last_response)
177
-
178
- # User query input
179
- input_text = st.chat_input("Type your prompt here")
180
-
181
- # 🚨 Validate: Ensure both API key and dataset are present before making an API call
182
- if input_text:
183
- if not api_key:
184
- st.error("🚨 API key is missing! Please provide a valid key before proceeding.")
185
- elif st.session_state.df is None:
186
- st.error("🚨 No dataset uploaded! Please upload an Excel file.")
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
-
193
- # Call the appropriate LLM provider
194
- if llm_provider == "OpenAI":
195
- response = llm_calls.query_openai(
196
- system_prompt=SYSTEM_PROMPT,
197
- user_prompt=input_text,
198
- json_data=json_data,
199
- openai_client=openai_client,
200
- )
201
- elif llm_provider == "Perplexity.ai":
202
- response = llm_calls.query_perplexity(
203
- system_prompt=SYSTEM_PROMPT,
204
- user_prompt=input_text,
205
- json_data=json_data,
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
217
-
218
- # Display structured output
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"]
225
- st.session_state.history.append({
226
- "Prompt": input_text,
227
- "Explanation": parsed_response["explanation"],
228
- "References": parsed_response["references"]
229
- })
230
- except json.JSONDecodeError:
231
- st.error("🚨 Error parsing response: Invalid JSON format.")
232
- except Exception as e:
233
- st.error(f"🚨 Unexpected error: {e}")
234
-
235
- st.rerun()
236
-
237
-
238
- # 📥 Download Updated Excel
239
- if st.session_state.df is not None:
240
- st.sidebar.subheader("Download Updated Dataset")
241
-
242
- def generate_excel(dataframe, history):
243
- output_stream = io.BytesIO()
244
- with pd.ExcelWriter(output_stream, engine="xlsxwriter") as writer:
245
- dataframe.to_excel(writer, index=False, sheet_name="Data")
246
- # Convert history to DataFrame and save in a new sheet
247
- if history:
248
- history_df = pd.DataFrame(history)
249
- history_df.to_excel(writer, index=False, sheet_name="Metadata")
250
-
251
- workbook = writer.book
252
-
253
- # Apply word wrapping
254
- for sheet_name in ["Data", "Metadata"]:
255
- if sheet_name in writer.sheets:
256
- worksheet = writer.sheets[sheet_name]
257
- wrap_format = workbook.add_format({"text_wrap": True, "align": "top", "valign": "top"})
258
-
259
- # Apply word wrap to all columns
260
- df_to_format = dataframe if sheet_name == "Data" else history_df
261
- for col_num, col_name in enumerate(df_to_format.columns):
262
- worksheet.set_column(col_num, col_num, 30, wrap_format) # Adjust width if needed
263
-
264
- output_stream.seek(0)
265
- return output_stream
266
-
267
-
268
- st.sidebar.download_button(
269
- "📥 Download Excel File",
270
- data=generate_excel(st.session_state.df, st.session_state.history),
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()