Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,7 @@ load_dotenv()
|
|
| 20 |
app = Flask(__name__)
|
| 21 |
app.secret_key = os.getenv('SECRET_KEY', secrets.token_hex(16))
|
| 22 |
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
|
| 23 |
-
app.config['UPLOAD_FOLDER'] =
|
| 24 |
|
| 25 |
# Initialize RAG system (singleton)
|
| 26 |
rag_system = None
|
|
@@ -128,23 +128,32 @@ def explain_results():
|
|
| 128 |
|
| 129 |
print(f"🧠 Generating explanations for {len(results)} results...")
|
| 130 |
|
| 131 |
-
#
|
| 132 |
rag = get_rag_system()
|
| 133 |
-
explanations = {}
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
# Store explanations in session
|
| 145 |
session_storage[session_id]['explanations'] = explanations
|
| 146 |
|
| 147 |
-
print("✅
|
| 148 |
|
| 149 |
return jsonify({
|
| 150 |
'success': True,
|
|
@@ -153,7 +162,9 @@ def explain_results():
|
|
| 153 |
|
| 154 |
except Exception as e:
|
| 155 |
print(f"❌ Explanation error: {str(e)}")
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
|
| 158 |
@app.route('/api/ask', methods=['POST'])
|
| 159 |
def ask_question():
|
|
|
|
| 20 |
app = Flask(__name__)
|
| 21 |
app.secret_key = os.getenv('SECRET_KEY', secrets.token_hex(16))
|
| 22 |
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
|
| 23 |
+
app.config['UPLOAD_FOLDER'] = tempfile.gettempdir()
|
| 24 |
|
| 25 |
# Initialize RAG system (singleton)
|
| 26 |
rag_system = None
|
|
|
|
| 128 |
|
| 129 |
print(f"🧠 Generating explanations for {len(results)} results...")
|
| 130 |
|
| 131 |
+
# Initialize RAG system
|
| 132 |
rag = get_rag_system()
|
|
|
|
| 133 |
|
| 134 |
+
# Generate explanations with timeout protection
|
| 135 |
+
from concurrent.futures import TimeoutError
|
| 136 |
+
import signal
|
| 137 |
+
|
| 138 |
+
try:
|
| 139 |
+
explanations = rag.explain_all_results(results)
|
| 140 |
+
except Exception as e:
|
| 141 |
+
print(f"⚠️ Explanation generation error: {str(e)}")
|
| 142 |
+
# Provide basic explanations as fallback
|
| 143 |
+
explanations = {}
|
| 144 |
+
for result in results:
|
| 145 |
+
status_msg = {
|
| 146 |
+
'normal': f"✅ Your {result.test_name} is within normal range.",
|
| 147 |
+
'high': f"⚠️ Your {result.test_name} is above normal range. Consult your doctor.",
|
| 148 |
+
'low': f"⚠️ Your {result.test_name} is below normal range. Consult your doctor.",
|
| 149 |
+
'unknown': f"Your {result.test_name} result is {result.value} {result.unit}."
|
| 150 |
+
}
|
| 151 |
+
explanations[result.test_name] = status_msg.get(result.status, "Result recorded.")
|
| 152 |
|
| 153 |
# Store explanations in session
|
| 154 |
session_storage[session_id]['explanations'] = explanations
|
| 155 |
|
| 156 |
+
print(f"✅ Generated {len(explanations)} explanations")
|
| 157 |
|
| 158 |
return jsonify({
|
| 159 |
'success': True,
|
|
|
|
| 162 |
|
| 163 |
except Exception as e:
|
| 164 |
print(f"❌ Explanation error: {str(e)}")
|
| 165 |
+
import traceback
|
| 166 |
+
traceback.print_exc()
|
| 167 |
+
return jsonify({'error': 'Error generating explanations. Please try uploading again.'}), 500
|
| 168 |
|
| 169 |
@app.route('/api/ask', methods=['POST'])
|
| 170 |
def ask_question():
|