about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPjotr Prins2026-04-05 17:30:32 +0200
committerPjotr Prins2026-04-05 17:30:32 +0200
commitf38a5be24654c753949065812661df83220bec3f (patch)
treef322934739ad0e2bd70f91b6b9b17bf68e23d356
parent8f84075f667fd3ad523d395cc1c66c07bbef0c23 (diff)
downloadgenecup-f38a5be24654c753949065812661df83220bec3f.tar.gz
Also cache gemini results
-rwxr-xr-xmore_functions.py3
-rwxr-xr-xserver.py18
2 files changed, 17 insertions, 4 deletions
diff --git a/more_functions.py b/more_functions.py
index a6cb86f..a115899 100755
--- a/more_functions.py
+++ b/more_functions.py
@@ -26,7 +26,8 @@ def esearch_pmids(query):
     print(f"  popen: {pmid_cmd}")
     pmids = os.popen(pmid_cmd).read().strip()
     pmid_list = [p for p in pmids.split("\n") if p.strip()] if pmids else []
-    _esearch_cache[key] = pmid_list
+    if pmid_list:
+        _esearch_cache[key] = pmid_list
     return pmid_list
 
 def undic(dic):
diff --git a/server.py b/server.py
index 6c5c6a4..a290c80 100755
--- a/server.py
+++ b/server.py
@@ -153,8 +153,17 @@ except FileNotFoundError:
 except Exception as e:
     print(f"FATAL ERROR: Could not read stress_prompt.txt: {e}")
 
+# In-memory cache for Gemini stress classification: hash(sentence) -> result
+_gemini_cache = {}
+
 # few shot Function to classify stress using Gemini API
 def classify_stress_with_gemini(sentence_text):
+    import hashlib
+    cache_key = hashlib.sha256(sentence_text.encode()).hexdigest()
+    if cache_key in _gemini_cache:
+        print(f"  Gemini cache hit for: {sentence_text[:60]}...")
+        return _gemini_cache[cache_key]
+
     if not GEMINI_API_KEY:
         print("Gemini API key not configured. Skipping classification.")
         return "error_no_api_key"
@@ -179,12 +188,15 @@ def classify_stress_with_gemini(sentence_text):
 
         # The model might return "Cellular Level Stress" or "Organismal Stress"
         if "cellular" in classification:
-            return "neg"  # 'neg' for Cellular Level Stress
+            result = "neg"  # 'neg' for Cellular Level Stress
         elif "organismal" in classification:
-            return "pos"  # 'pos' for Organismal Stress
+            result = "pos"  # 'pos' for Organismal Stress
         else:
             print(f"Warning: Gemini returned unexpected classification: '{classification}' for sentence: '{sentence_text}'")
-            return "unknown"
+            result = "unknown"
+        if result in ("pos", "neg"):
+            _gemini_cache[cache_key] = result
+        return result
 
     except Exception as e:
         print(f"Error calling Gemini API for stress classification: {e}")