Hanan-Alnakhal commited on
Commit
49ad316
·
verified ·
1 Parent(s): 1a9762b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
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'] = "/tmp"
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
- # Generate explanations
132
  rag = get_rag_system()
133
- explanations = {}
134
 
135
- for i, result in enumerate(results):
136
- print(f" Explaining {i+1}/{len(results)}: {result.test_name}...")
137
- try:
138
- explanation = rag.explain_result(result)
139
- explanations[result.test_name] = explanation
140
- except Exception as e:
141
- print(f" Error: {str(e)}")
142
- explanations[result.test_name] = f"Unable to generate explanation: {str(e)}"
 
 
 
 
 
 
 
 
 
 
143
 
144
  # Store explanations in session
145
  session_storage[session_id]['explanations'] = explanations
146
 
147
- print("✅ All explanations generated")
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
- return jsonify({'error': str(e)}), 500
 
 
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():