mgbam commited on
Commit
4f18c4e
·
verified ·
1 Parent(s): e7b7760

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -37
app.py CHANGED
@@ -12,75 +12,107 @@
12
  # See the License for the specific language governing permissions and
13
  # limitations under the License.
14
 
15
- from evaluation import evaluate_report, evaluation_prompt
16
- from flask import Flask, send_from_directory, request, jsonify, Response, stream_with_context, send_file
 
 
 
 
 
 
 
 
 
 
 
 
17
  from flask_cors import CORS
18
- import os, time, json, re
 
19
  from gemini import gemini_get_text_response
 
20
  from interview_simulator import stream_interview
21
  from cache import create_cache_zip
22
- from medgemma import medgemma_get_text_response
23
 
24
- app = Flask(__name__, static_folder=os.environ.get("FRONTEND_BUILD", "frontend/build"), static_url_path="/")
 
 
 
 
 
25
  CORS(app, resources={r"/api/*": {"origins": "http://localhost:3000"}})
26
 
 
 
27
  @app.route("/")
28
- def serve():
29
- """Serves the main index.html file."""
30
  return send_from_directory(app.static_folder, "index.html")
31
 
32
 
33
  @app.route("/api/stream_conversation", methods=["GET"])
34
  def stream_conversation():
35
- """Streams the conversation with the interview simulator."""
 
 
36
  patient = request.args.get("patient", "Patient")
37
  condition = request.args.get("condition", "unknown condition")
38
-
39
- def generate():
40
  try:
41
- for message in stream_interview(patient, condition):
42
- yield f"data: {message}\n\n"
43
  except Exception as e:
44
- yield f"data: Error: {str(e)}\n\n"
45
- raise e
46
-
47
- return Response(stream_with_context(generate()), mimetype="text/event-stream")
 
48
 
49
  @app.route("/api/evaluate_report", methods=["POST"])
50
  def evaluate_report_call():
51
- """Evaluates the provided medical report."""
52
- data = request.get_json()
53
- report = data.get("report", "")
 
 
 
 
 
54
  if not report:
55
  return jsonify({"error": "Report is required"}), 400
56
- condition = data.get("condition", "")
57
  if not condition:
58
- return jsonify({"error": "Condition is required"}), 400
59
-
60
- evaluation_text = evaluate_report(report, condition)
61
-
62
- return jsonify({"evaluation": evaluation_text})
63
 
64
 
65
- @app.route("/api/download_cache")
66
  def download_cache_zip():
67
- """Creates a zip file of the cache and returns it for download."""
68
- zip_filepath, error = create_cache_zip()
 
 
69
  if error:
70
  return jsonify({"error": error}), 500
71
- if not os.path.isfile(zip_filepath):
72
- return jsonify({"error": f"File not found: {zip_filepath}"}), 404
73
- return send_file(zip_filepath, as_attachment=True)
74
 
75
 
76
  @app.route("/<path:path>")
77
  def static_proxy(path):
78
- """Serves static files and defaults to index.html for unknown paths."""
79
- file_path = os.path.join(app.static_folder, path)
80
- if os.path.isfile(file_path):
 
 
81
  return send_from_directory(app.static_folder, path)
82
- else:
83
- return send_from_directory(app.static_folder, "index.html")
84
-
 
 
85
  if __name__ == "__main__":
86
  app.run(host="0.0.0.0", port=7860, threaded=True)
 
12
  # See the License for the specific language governing permissions and
13
  # limitations under the License.
14
 
15
+ import os
16
+ import time
17
+ import json
18
+ import re
19
+
20
+ from flask import (
21
+ Flask,
22
+ send_from_directory,
23
+ request,
24
+ jsonify,
25
+ Response,
26
+ stream_with_context,
27
+ send_file,
28
+ )
29
  from flask_cors import CORS
30
+
31
+ from evaluation import evaluate_report, evaluation_prompt
32
  from gemini import gemini_get_text_response
33
+ from medgemma import medgemma_get_text_response
34
  from interview_simulator import stream_interview
35
  from cache import create_cache_zip
 
36
 
37
+ # Application setup
38
+ app = Flask(
39
+ __name__,
40
+ static_folder=os.environ.get("FRONTEND_BUILD", "frontend/build"),
41
+ static_url_path="/",
42
+ )
43
  CORS(app, resources={r"/api/*": {"origins": "http://localhost:3000"}})
44
 
45
+ # -------- Routes --------
46
+
47
  @app.route("/")
48
+ def serve_index():
49
+ """Serve the main frontend entry-point."""
50
  return send_from_directory(app.static_folder, "index.html")
51
 
52
 
53
  @app.route("/api/stream_conversation", methods=["GET"])
54
  def stream_conversation():
55
+ """
56
+ Stream a simulated interview via Server-Sent Events (SSE).
57
+ """
58
  patient = request.args.get("patient", "Patient")
59
  condition = request.args.get("condition", "unknown condition")
60
+
61
+ def event_stream():
62
  try:
63
+ for msg in stream_interview(patient, condition):
64
+ yield f"data: {msg}\n\n"
65
  except Exception as e:
66
+ yield f"data: Error: {e}\n\n"
67
+ raise
68
+
69
+ return Response(stream_with_context(event_stream()), mimetype="text/event-stream")
70
+
71
 
72
  @app.route("/api/evaluate_report", methods=["POST"])
73
  def evaluate_report_call():
74
+ """
75
+ Evaluate a given medical report.
76
+ Expect JSON with "report" and "condition" keys.
77
+ """
78
+ data = request.get_json() or {}
79
+ report = data.get("report", "").strip()
80
+ condition = data.get("condition", "").strip()
81
+
82
  if not report:
83
  return jsonify({"error": "Report is required"}), 400
 
84
  if not condition:
85
+ return jsonify({"error": "Condition is required"}), 400
86
+
87
+ evaluation = evaluate_report(report, condition)
88
+ return jsonify({"evaluation": evaluation})
 
89
 
90
 
91
+ @app.route("/api/download_cache", methods=["GET"])
92
  def download_cache_zip():
93
+ """
94
+ Generate and send a zip archive of the cache directory.
95
+ """
96
+ zip_path, error = create_cache_zip()
97
  if error:
98
  return jsonify({"error": error}), 500
99
+ if not os.path.isfile(zip_path):
100
+ return jsonify({"error": f"File not found: {zip_path}"}), 404
101
+ return send_file(zip_path, as_attachment=True)
102
 
103
 
104
  @app.route("/<path:path>")
105
  def static_proxy(path):
106
+ """
107
+ Serve static assets; fallback to index.html for SPA routes.
108
+ """
109
+ fs_path = os.path.join(app.static_folder, path)
110
+ if os.path.isfile(fs_path):
111
  return send_from_directory(app.static_folder, path)
112
+ return send_from_directory(app.static_folder, "index.html")
113
+
114
+
115
+ # -------- CLI launch --------
116
+
117
  if __name__ == "__main__":
118
  app.run(host="0.0.0.0", port=7860, threaded=True)