Darshan03 commited on
Commit
c8dc87e
·
verified ·
1 Parent(s): 5ff9031

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -22
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import streamlit as st
2
  import json
3
  import os
4
  from datasets import load_dataset
@@ -31,9 +31,50 @@ if not os.path.exists(DATA_FOLDER):
31
 
32
  st.title("JSON File Uploader and Question Answering")
33
 
34
- uploaded_file = st.file_uploader("Upload a JSON file", type=["json"])
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  if uploaded_file is not None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  try:
38
  # Load API keys and Hugging Face token from environment variables
39
  groq_api = os.environ.get("groq_api")
@@ -51,21 +92,12 @@ if uploaded_file is not None:
51
  )
52
  st.stop()
53
 
54
- # Read the JSON file
55
- json_data = json.load(uploaded_file)
56
-
57
- # Get the filename
58
- file_name = uploaded_file.name
59
-
60
- # Construct the full file path
61
- file_path = os.path.join(DATA_FOLDER, file_name)
62
-
63
  # Save the file to the data folder
64
- with open(file_path, "w") as f:
65
  json.dump(json_data, f, indent=4) # Save with indentation for readability
66
 
67
- st.success(f"File '{file_name}' successfully uploaded and saved to:")
68
- st.code(file_path, language="plaintext")
69
 
70
  st.subheader("Process JSON Data and Enable Question Answering")
71
 
@@ -176,7 +208,7 @@ just reformulate it if needed and otherwise return it as is."""),
176
  st.subheader("Generate Structured Output")
177
  if st.button("Generate Structured Cancer Information"):
178
  with st.spinner("Generating structured output..."):
179
- json_data = json.loads(Path(file_path).read_text())
180
  context = ""
181
  for item in json_data:
182
  context += json.dumps(item, indent=4)
@@ -210,7 +242,7 @@ just reformulate it if needed and otherwise return it as is."""),
210
  """Structured information about cancer diagnosis and medication."""
211
  diagnosis_characteristics: List[DiagnosisCharacteristic] = field(metadata={"description": "List of primary cancers"})
212
  cancer_related_medications: List[CancerRelatedMedication] = field(metadata={"description": "List of cancer related medication given to the patient"})
213
-
214
  llm = ChatGroq(groq_api_key=groq_api, model_name="llama-3.1-8b-instant")
215
  structured_llm = llm.with_structured_output(CancerInformation)
216
  try:
@@ -219,7 +251,7 @@ just reformulate it if needed and otherwise return it as is."""),
219
  st.json(output.dict())
220
 
221
  # Save the generated output to a JSON file
222
- output_filename = f"{Path(file_path).stem}_structured.json"
223
  output_filepath = os.path.join(DATA_FOLDER, output_filename)
224
  with open(output_filepath, "w") as f:
225
  json.dump(output, f, indent=4)
@@ -235,10 +267,7 @@ just reformulate it if needed and otherwise return it as is."""),
235
 
236
  except Exception as e:
237
  st.error(f"Error generating structured output: {e}")
238
-
239
- except json.JSONDecodeError:
240
- st.error("Error: The uploaded file is not a valid JSON file.")
241
  except Exception as e:
242
- st.error(f"An error occurred: {e}")
243
  else:
244
- st.info("Please upload a JSON file.")
 
1
+ code ="import streamlit as st
2
  import json
3
  import os
4
  from datasets import load_dataset
 
31
 
32
  st.title("JSON File Uploader and Question Answering")
33
 
34
+ # Option to upload a file or provide a local file path
35
+ input_option = st.radio("Choose input method:", ("Upload a JSON file", "Enter local file path"))
36
+
37
+ uploaded_file = None
38
+ local_file_path_input = None
39
+
40
+ if input_option == "Upload a JSON file":
41
+ uploaded_file = st.file_uploader("Upload a JSON file", type=["json"])
42
+ elif input_option == "Enter local file path":
43
+ local_file_path_input = st.text_input("Enter local JSON file path (e.g., datasets/1.json):")
44
+
45
+ file_path_to_process = None
46
+ file_name = None
47
+ json_data = None
48
 
49
  if uploaded_file is not None:
50
+ try:
51
+ json_data = json.load(uploaded_file)
52
+ file_name = uploaded_file.name
53
+ file_path_to_process = os.path.join(DATA_FOLDER, file_name)
54
+ except json.JSONDecodeError:
55
+ st.error("Error: The uploaded file is not a valid JSON file.")
56
+ st.stop()
57
+ except Exception as e:
58
+ st.error(f"An error occurred while processing the uploaded file: {e}")
59
+ st.stop()
60
+ elif local_file_path_input:
61
+ if os.path.exists(local_file_path_input):
62
+ try:
63
+ with open(local_file_path_input, 'r') as f:
64
+ json_data = json.load(f)
65
+ file_name = os.path.basename(local_file_path_input)
66
+ file_path_to_process = os.path.join(DATA_FOLDER, file_name)
67
+ except json.JSONDecodeError:
68
+ st.error("Error: The provided local file is not a valid JSON file.")
69
+ st.stop()
70
+ except Exception as e:
71
+ st.error(f"An error occurred while processing the local file: {e}")
72
+ st.stop()
73
+ else:
74
+ st.error(f"Error: The local file path '{local_file_path_input}' does not exist.")
75
+ st.stop()
76
+
77
+ if json_data is not None:
78
  try:
79
  # Load API keys and Hugging Face token from environment variables
80
  groq_api = os.environ.get("groq_api")
 
92
  )
93
  st.stop()
94
 
 
 
 
 
 
 
 
 
 
95
  # Save the file to the data folder
96
+ with open(file_path_to_process, "w") as f:
97
  json.dump(json_data, f, indent=4) # Save with indentation for readability
98
 
99
+ st.success(f"File '{file_name}' successfully loaded and saved to:")
100
+ st.code(file_path_to_process, language="plaintext")
101
 
102
  st.subheader("Process JSON Data and Enable Question Answering")
103
 
 
208
  st.subheader("Generate Structured Output")
209
  if st.button("Generate Structured Cancer Information"):
210
  with st.spinner("Generating structured output..."):
211
+ json_data = json.loads(Path(file_path_to_process).read_text())
212
  context = ""
213
  for item in json_data:
214
  context += json.dumps(item, indent=4)
 
242
  """Structured information about cancer diagnosis and medication."""
243
  diagnosis_characteristics: List[DiagnosisCharacteristic] = field(metadata={"description": "List of primary cancers"})
244
  cancer_related_medications: List[CancerRelatedMedication] = field(metadata={"description": "List of cancer related medication given to the patient"})
245
+
246
  llm = ChatGroq(groq_api_key=groq_api, model_name="llama-3.1-8b-instant")
247
  structured_llm = llm.with_structured_output(CancerInformation)
248
  try:
 
251
  st.json(output.dict())
252
 
253
  # Save the generated output to a JSON file
254
+ output_filename = f"{Path(file_path_to_process).stem}_structured.json"
255
  output_filepath = os.path.join(DATA_FOLDER, output_filename)
256
  with open(output_filepath, "w") as f:
257
  json.dump(output, f, indent=4)
 
267
 
268
  except Exception as e:
269
  st.error(f"Error generating structured output: {e}")
 
 
 
270
  except Exception as e:
271
+ st.error(f"An unexpected error occurred: {e}")
272
  else:
273
+ st.info("Please upload a JSON file or enter a local file path.")