about summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xserver.py112
1 files changed, 99 insertions, 13 deletions
diff --git a/server.py b/server.py
index 25ccad5..9305453 100755
--- a/server.py
+++ b/server.py
@@ -129,6 +129,18 @@ with app.app_context():
     db.create_all()
 
 # Configure Gemini API Client
+#
+# MODEL is the model the batch stress classifier calls.  It is named here
+# (not only at the call site) so that the error messages, the log and the
+# container's boot-time key check (gn-machines: (genenetwork services
+# genecup), %gemini-model) all talk about the same thing -- and can be
+# kept in sync.
+GEMINI_MODEL = 'gemini-3-flash-preview'
+# Where to look when classification fails, quoted in the page it breaks.
+GEMINI_KEY_FILE = '~/.config/gemini/credentials'  # of the genecup user
+GEMINI_LOG_FILE = '/var/log/genecup.log'
+GEMINI_CHECK_LOG_FILE = '/var/log/gemini-check.log'
+
 GEMINI_API_KEY = _read_gemini_api_key()
 gemini_client = None
 if not GEMINI_API_KEY:
@@ -1566,10 +1578,26 @@ def sentences():
             pmid_list.append(pmid+cat0)
 
     # Step 2: If the category is 'stress' and we have sentences, perform batch classification
+    # gemini_error: the API call failed (nothing to classify with).
+    # gemini_note:   the API answered but produced no usable labels.
+    # Both are reported on the page and in the log; without them the
+    # fallback heading below cannot tell "no key" from "model refused"
+    # from "nothing to classify", which is exactly the confusion the
+    # heading caused in practice.
     gemini_error = None
+    gemini_note = None
+    gemini_kind = None          # "no-key" | "error" | "no-labels"
+    gemini_attempts = 0
+    gemini_raw = None
     if cat0 == 'stress' and all_stress_sentences:
         if not GEMINI_API_KEY:
-            print("Gemini API key not configured. Skipping batch classification.")
+            print(f"Gemini classification not available: no API key at "
+                  f"{GEMINI_KEY_FILE} (must exist, be readable by the "
+                  f"genecup user and have mode 0400). Skipping batch "
+                  f"classification; sentences are listed unclassified.")
+            gemini_kind = "no-key"
+            gemini_note = (f"no API key at {GEMINI_KEY_FILE} (missing, "
+                           f"unreadable or not mode 0400)")
         else:
             # Build cache key early to check if we need the loading page
             sentences_to_classify_str = ""
@@ -1600,42 +1628,76 @@ Here are the sentences to classify:
                 else:
                     # Call API with retry
                     import time as _time
+                    import traceback
                     last_error = None
                     classifications = None
                     for attempt in range(3):
+                        gemini_attempts = attempt + 1
                         try:
                             if attempt > 0:
                                 _time.sleep(2 * attempt)
                                 print(f"  Gemini batch retry {attempt + 1}/3")
-                            print(f"Gemini API call: batch stress classification (gemini-3-flash-preview)\n  Prompt: {batched_prompt}")
+                            print(f"Gemini API call: batch stress classification "
+                                  f"({GEMINI_MODEL})\n  Prompt: {batched_prompt}")
                             response = gemini_client.models.generate_content(
-                                model='gemini-3-flash-preview',
+                                model=GEMINI_MODEL,
                                 contents=batched_prompt
                             )
-                            print(f"  Gemini response: {response.text.strip()}")
-                            cleaned_response_text = response.text.strip().replace("```json", "").replace("```", "").strip()
-                            classifications = json.loads(cleaned_response_text)
+                            gemini_raw = response.text.strip()
+                            print(f"  Gemini response: {gemini_raw}")
+                            cleaned_response_text = gemini_raw.replace("```json", "").replace("```", "").strip()
+                            try:
+                                classifications = json.loads(cleaned_response_text)
+                            except json.JSONDecodeError as jde:
+                                # Retrying will not help with a malformed
+                                # body, but say what came back.
+                                raise RuntimeError(
+                                    f"{GEMINI_MODEL} did not return the expected "
+                                    f"JSON object ({jde}); response was "
+                                    f"{gemini_raw[:300]!r}") from jde
                             # Cache on success
                             _gemini_cache[batch_cache_key] = classifications
                             break
                         except Exception as retry_e:
                             last_error = retry_e
-                            print(f"  Gemini batch attempt {attempt + 1}/3 failed: {retry_e}")
+                            print(f"  Gemini batch attempt {attempt + 1}/3 "
+                                  f"failed ({type(retry_e).__name__}): {retry_e}")
                     if classifications is None:
-                        raise Exception(f"Gemini batch failed after 3 attempts: {last_error}")
+                        print(f"  Gemini batch failed after 3 attempts; last "
+                              f"error:\n{traceback.format_exc()}")
+                        raise Exception(f"{GEMINI_MODEL} call failed after 3 "
+                                        f"attempts ({type(last_error).__name__}): "
+                                        f"{last_error}")
 
                 # Step 4: Distribute the sentences into buckets based on the parsed classifications
+                labelled = unlabelled = 0
                 for i, s_obj in enumerate(all_stress_sentences):
                     # Get the classification for sentence 'i'. Use .get() for safety.
-                    classification = classifications.get(str(i), "unknown").lower()
+                    classification = str(classifications.get(str(i), "unknown")).lower()
                     if "cellular" in classification:
                         out_neg += s_obj['html_line']
+                        labelled += 1
                     elif "organismal" in classification:
                         out_pos += s_obj['html_line']
+                        labelled += 1
+                    else:
+                        unlabelled += 1
+                print(f"  Gemini batch: {labelled} of {len(all_stress_sentences)} "
+                      f"sentences labelled ({GEMINI_MODEL})")
+                if labelled == 0:
+                    gemini_kind = "no-labels"
+                    gemini_note = (
+                        f"{GEMINI_MODEL} answered {len(all_stress_sentences)} "
+                        f"sentence(s) but none with a Cellular Stress or "
+                        f"Organismal Stress label; response was "
+                        f"{(gemini_raw or '')[:300]!r}")
 
             except Exception as e:
-                gemini_error = str(e)
-                print(f"Error during batch Gemini classification: {gemini_error}")
+                gemini_kind = "error"
+                gemini_error = f"{type(e).__name__}: {e}"
+                print(f"Error during batch Gemini classification "
+                      f"(model {GEMINI_MODEL}, {gemini_attempts} attempt(s)): "
+                      f"{gemini_error}")
     out1="<h3>"+gene0 + " and " + cat0  + "</h3>\n"
     if len(pmid_list)>1:
         out2 = str(num_abstract) + ' sentences in ' + " <a href=\"https://www.ncbi.nlm.nih.gov/pubmed/?term=" + pmid_string.strip() +"\" target=_new>"+ str(len(pmid_list)) + ' studies' +"<br></a>" + "<br><br>"
@@ -1646,14 +1708,38 @@ Here are the sentences to classify:
 
 
     if(cat0 == 'stress'): # Only show stress classification if category is stress
+        # Say which of the three very different situations happened: no
+        # key, the call failed, or the model answered without usable
+        # labels.  The old single heading covered all of them.
         if gemini_error:
-            error_html = f"<p><b>Gemini API error:</b> {gemini_error}</p>"
+            error_html = (
+                f"<p><b>Gemini classification not available:</b> {gemini_error}</p>"
+                f"<p>Model <code>{GEMINI_MODEL}</code>, {gemini_attempts} "
+                f"attempt(s). The sentences below are listed unclassified. "
+                f"The server log (<code>{GEMINI_LOG_FILE}</code>) has the "
+                f"request, the response and the traceback; the boot-time "
+                f"check of the key and the model is in "
+                f"<code>{GEMINI_CHECK_LOG_FILE}</code>.</p>")
+        elif gemini_note:
+            error_html = (f"<p><b>Gemini classification not available:</b> "
+                          f"{gemini_note}</p>")
         else:
             error_html = ""
         if(out_neg == "" and out_pos == ""):
             # If no classification results, show all sentences if any, or a message
             if out3:
-                 out= out1+ out2 + error_html + "<b>All related sentences (Gemini classification not available or no specific stress types found):</b><hr><ol>" + out3
+                 if gemini_kind == "error":
+                     heading = ("All related sentences (classification failed, "
+                                "listed unclassified):")
+                 elif gemini_kind == "no-labels":
+                     heading = ("All related sentences (no Cellular/Organismal "
+                                "labels returned, listed unclassified):")
+                 elif gemini_kind == "no-key":
+                     heading = ("All related sentences (not classified: no "
+                                "Gemini API key):")
+                 else:
+                     heading = "All related sentences (not classified):"
+                 out= out1+ out2 + error_html + "<b>" + heading + "</b><hr><ol>" + out3
             else:
                  out = out1 + out2 + error_html + "No sentences found for this combination, or Gemini classification yielded no results."
         elif(out_pos != "" and out_neg!=""):