mgbam commited on
Commit
68e917e
·
verified ·
1 Parent(s): 69fbea4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -9
app.py CHANGED
@@ -15,7 +15,6 @@ summarizer = None
15
  nlp = None
16
  initialization_status = "Initializing..." # Track initialization state
17
 
18
-
19
  # ---------------------------- Helper Functions ----------------------------
20
 
21
  def log_error(message: str):
@@ -27,6 +26,39 @@ def log_error(message: str):
27
  except:
28
  print("Couldn't write to error log file.") #If logging fails, still print to console
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  # ---------------------------- Tool Functions ----------------------------
31
 
32
  def search_pubmed(query: str) -> list:
@@ -109,11 +141,22 @@ def setup():
109
  initialization_status = "Initializing..."
110
  try:
111
  print("Initializing summarization pipeline...")
 
112
  summarizer = pipeline("summarization", model=SUMMARIZATION_MODEL, token=HUGGINGFACE_API_TOKEN)
113
  print("Summarization pipeline initialized.")
 
 
114
  print("Loading SpaCy model...")
115
- nlp = spacy.load(SPACY_MODEL)
 
 
 
 
 
 
116
  print("SpaCy model loaded.")
 
 
117
  initialization_status = "MedAI Agent initialized successfully!"
118
  return initialization_status # Return the status message
119
  except Exception as e:
@@ -125,20 +168,16 @@ def setup():
125
 
126
  def launch_gradio():
127
  """Launches the Gradio interface."""
128
- global initialization_status #Allows the function to modify global variable
129
  with gr.Blocks() as iface:
130
  gr.Markdown("# MedAI: Medical Literature Review and Summarization")
131
- status_display = gr.Textbox(value=initialization_status, interactive=False) # Displays initialization status
132
  query_input = gr.Textbox(lines=3, placeholder="Enter your medical query (e.g., 'new treatments for diabetes')...")
133
  submit_button = gr.Button("Submit")
134
  output_results = gr.Markdown()
135
 
136
  submit_button.click(medai_agent, inputs=query_input, outputs=output_results)
137
- #The run of the agent will not change.
138
-
139
- #The setup is running. The value of the text display will update based on this step.
140
- setup_result = setup()
141
- status_display.value = setup_result #update the status display.
142
 
143
  iface.launch()
144
 
 
15
  nlp = None
16
  initialization_status = "Initializing..." # Track initialization state
17
 
 
18
  # ---------------------------- Helper Functions ----------------------------
19
 
20
  def log_error(message: str):
 
26
  except:
27
  print("Couldn't write to error log file.") #If logging fails, still print to console
28
 
29
+ # ---------------------------- Language Model Loading ----------------------------
30
+
31
+ def load_spacy_model(model_name="en_core_web_sm"):
32
+ """Loads the SpaCy language model, downloading it if necessary."""
33
+ global initialization_status # To update the initialization status
34
+
35
+ try:
36
+ print(f"Attempting to load SpaCy model '{model_name}'...")
37
+ nlp_model = spacy.load(model_name)
38
+ print(f"Successfully loaded SpaCy model '{model_name}'.")
39
+ initialization_status += f"\nSpaCy model '{model_name}' loaded."
40
+ return nlp_model
41
+ except OSError:
42
+ print(f"SpaCy model '{model_name}' not found. Downloading...")
43
+ initialization_status += f"\nSpaCy model '{model_name}' not found. Downloading..."
44
+ try:
45
+ import subprocess
46
+ subprocess.check_call(["python", "-m", "spacy", "download", model_name])
47
+ nlp_model = spacy.load(model_name)
48
+ print(f"Successfully loaded SpaCy model '{model_name}' after downloading.")
49
+ initialization_status += f"\nSuccessfully loaded SpaCy model '{model_name}' after downloading."
50
+ return nlp_model
51
+
52
+ except Exception as e:
53
+ log_error(f"Failed to download or load SpaCy model '{model_name}': {e}")
54
+ initialization_status += f"\nFailed to download or load SpaCy model '{model_name}': {e}"
55
+ return None # Indicate failure
56
+
57
+ except Exception as e:
58
+ log_error(f"Error loading SpaCy model '{model_name}': {e}")
59
+ initialization_status += f"\nError loading SpaCy model '{model_name}': {e}"
60
+ return None
61
+
62
  # ---------------------------- Tool Functions ----------------------------
63
 
64
  def search_pubmed(query: str) -> list:
 
141
  initialization_status = "Initializing..."
142
  try:
143
  print("Initializing summarization pipeline...")
144
+ initialization_status += "\nInitializing summarization pipeline..."
145
  summarizer = pipeline("summarization", model=SUMMARIZATION_MODEL, token=HUGGINGFACE_API_TOKEN)
146
  print("Summarization pipeline initialized.")
147
+ initialization_status += "\nSummarization pipeline initialized."
148
+
149
  print("Loading SpaCy model...")
150
+ initialization_status += "\nLoading SpaCy model..."
151
+ global nlp
152
+ nlp = load_spacy_model() # Call the SpaCy loading function.
153
+ if nlp is None:
154
+ initialization_status += "\nSpaCy model failed to load. Check the error log."
155
+ return initialization_status
156
+
157
  print("SpaCy model loaded.")
158
+ initialization_status += "\nSpaCy model loaded."
159
+
160
  initialization_status = "MedAI Agent initialized successfully!"
161
  return initialization_status # Return the status message
162
  except Exception as e:
 
168
 
169
  def launch_gradio():
170
  """Launches the Gradio interface."""
171
+ global initialization_status # Allows the function to modify global variable
172
  with gr.Blocks() as iface:
173
  gr.Markdown("# MedAI: Medical Literature Review and Summarization")
174
+ status_display = gr.Textbox(value=initialization_status, interactive=False) # Displays initialization status
175
  query_input = gr.Textbox(lines=3, placeholder="Enter your medical query (e.g., 'new treatments for diabetes')...")
176
  submit_button = gr.Button("Submit")
177
  output_results = gr.Markdown()
178
 
179
  submit_button.click(medai_agent, inputs=query_input, outputs=output_results)
180
+ status_display.value = setup() # Set the status after running setup
 
 
 
 
181
 
182
  iface.launch()
183