From 59de73d7b09f2f398d8e043ffcb8b586e1d50bc5 Mon Sep 17 00:00:00 2001 From: Pjotr Prins Date: Sun, 5 Apr 2026 17:55:47 +0200 Subject: Show page when calling GEMINI API --- server.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'server.py') diff --git a/server.py b/server.py index cb9d009..eea6487 100755 --- a/server.py +++ b/server.py @@ -1506,12 +1506,27 @@ def sentences(): if not GEMINI_API_KEY: print("Gemini API key not configured. Skipping batch classification.") else: - try: - # Create the batched prompt - sentences_to_classify_str = "" - for i, s_obj in enumerate(all_stress_sentences): - sentences_to_classify_str += f'Sentence {i}: "{s_obj["raw_text"]}"\n' + # Build cache key early to check if we need the loading page + sentences_to_classify_str = "" + for i, s_obj in enumerate(all_stress_sentences): + sentences_to_classify_str += f'Sentence {i}: "{s_obj["raw_text"]}"\n' + import hashlib + batch_cache_key = hashlib.sha256(sentences_to_classify_str.encode()).hexdigest() + + # Show loading page if not cached and not yet asked to classify + if batch_cache_key not in _gemini_cache and not request.args.get('classify'): + loading_url = request.url + ('&' if '?' in request.url else '?') + 'classify=1' + return f''' +
+ + + +Classifying {len(all_stress_sentences)} sentences as cellular/organismal stress.
+Please wait, this page will refresh automatically.
+''' + try: batched_prompt = f"""For each sentence below, classify it as describing "Cellular Stress" or "Organismal Stress". Return your response as a valid JSON object where keys are the sentence numbers (e.g., "0", "1", "2") and values are the classification ("Cellular Stress" or "Organismal Stress"). @@ -1520,9 +1535,6 @@ Example format: {{"0": "Cellular Stress", "1": "Organismal Stress"}} Here are the sentences to classify: {sentences_to_classify_str} """ - # Check cache (keyed on the batch of sentences) - import hashlib - batch_cache_key = hashlib.sha256(sentences_to_classify_str.encode()).hexdigest() if batch_cache_key in _gemini_cache: print(f" Gemini batch cache hit ({len(all_stress_sentences)} sentences)") classifications = _gemini_cache[batch_cache_key] -- cgit 1.4.1