Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,85 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from
|
| 5 |
|
| 6 |
-
|
| 7 |
-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 8 |
-
logger = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
@app.route('/healthz')
|
| 19 |
def health_check():
|
| 20 |
-
|
| 21 |
return "OK", 200
|
| 22 |
|
| 23 |
@app.route('/')
|
| 24 |
def index():
|
| 25 |
-
|
| 26 |
-
return
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
"
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
if __name__ == '__main__':
|
| 44 |
-
logger.info("Starting Flask app...")
|
| 45 |
app.run(host='0.0.0.0', port=8080)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
from flask import Flask, request, jsonify, send_from_directory
|
| 5 |
|
| 6 |
+
app = Flask(__name__, static_folder='static', static_url_path='')
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 9 |
+
GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent"
|
| 10 |
|
| 11 |
+
SYSTEM_PROMPT = """
|
| 12 |
+
你是一个世界顶级的皮肤科医生AI助手。你的任务是基于用户上传的皮肤图像和病历文本,进行专业的临床诊疗辅助分析。
|
| 13 |
+
你的输出必须是一个单一、有效的JSON对象,不包含任何JSON格式之外的文字或标记(例如不要使用 "```json")。
|
| 14 |
+
请严格遵循以下JSON结构:
|
| 15 |
+
{
|
| 16 |
+
"image_features": ["从图像中识别出的关键皮损形态学特征列表", "例如:色素网络不规则", "例如:蓝白结构"],
|
| 17 |
+
"differential_diagnosis": [
|
| 18 |
+
{ "diagnosis": "可能性最高的诊断名称", "likelihood": "高", "evidence": "支持该诊断的关键图像和文本依据" },
|
| 19 |
+
{ "diagnosis": "可能性次之的诊断名称", "likelihood": "中", "evidence": "支持该诊断的关键依据" }
|
| 20 |
+
],
|
| 21 |
+
"comprehensive_analysis": "一段对整个病情的全面、综合性分析和总结。",
|
| 22 |
+
"recommendations": {
|
| 23 |
+
"further_examinations": ["建议的下一步检查列表", "例如:皮肤镜检查", "例如:皮肤活检"],
|
| 24 |
+
"treatment_suggestions": ["初步的、符合临床指南的治疗方向建议列表", "例如:外用糖皮质激素", "例如:建议手术切除"]
|
| 25 |
+
}
|
| 26 |
+
}
|
| 27 |
+
"""
|
| 28 |
|
| 29 |
@app.route('/healthz')
|
| 30 |
def health_check():
|
| 31 |
+
"""专门用于平台健康检查的路由,确保100%稳定"""
|
| 32 |
return "OK", 200
|
| 33 |
|
| 34 |
@app.route('/')
|
| 35 |
def index():
|
| 36 |
+
"""提供前端HTML页面"""
|
| 37 |
+
return send_from_directory(app.static_folder, 'index.html')
|
| 38 |
+
|
| 39 |
+
@app.route('/api/analyze', methods=['POST'])
|
| 40 |
+
def analyze_skin():
|
| 41 |
+
"""处理分析请求的核心API端点"""
|
| 42 |
+
if not GEMINI_API_KEY:
|
| 43 |
+
return jsonify({"error": "服务端未配置GEMINI_API_KEY"}), 500
|
| 44 |
+
|
| 45 |
+
data = request.json
|
| 46 |
+
if not data or 'image' not in data or 'text' not in data:
|
| 47 |
+
return jsonify({"error": "请求中缺少图像或文本信息"}), 400
|
| 48 |
+
|
| 49 |
+
image_base64 = data.get('image')
|
| 50 |
+
text_prompt = data.get('text')
|
| 51 |
+
|
| 52 |
+
headers = {'Content-Type': 'application/json'}
|
| 53 |
+
|
| 54 |
+
payload = {
|
| 55 |
+
"contents": [{"parts": [
|
| 56 |
+
{"text": SYSTEM_PROMPT},
|
| 57 |
+
{"text": f"临床病历信息如下:\n{text_prompt}"},
|
| 58 |
+
{"inline_data": {"mime_type": "image/jpeg", "data": image_base64}}
|
| 59 |
+
]}]
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
response = requests.post(f"{GEMINI_API_URL}?key={GEMINI_API_KEY}", headers=headers, json=payload, timeout=60)
|
| 64 |
+
response.raise_for_status()
|
| 65 |
+
|
| 66 |
+
gemini_result = response.json()
|
| 67 |
+
|
| 68 |
+
# 稳健地提取内容
|
| 69 |
+
analysis_content_string = gemini_result['candidates']['content']['parts']['text']
|
| 70 |
+
parsed_json = json.loads(analysis_content_string)
|
| 71 |
+
|
| 72 |
+
return jsonify(parsed_json)
|
| 73 |
+
|
| 74 |
+
except requests.exceptions.RequestException as e:
|
| 75 |
+
return jsonify({"error": f"调用Gemini API时发生网络错误: {e}"}), 500
|
| 76 |
+
except (KeyError, IndexError, TypeError):
|
| 77 |
+
error_details = response.json() if 'response' in locals() else "No response from API"
|
| 78 |
+
return jsonify({"error": "解析Gemini API响应失败,格式不正确", "details": error_details}), 500
|
| 79 |
+
except json.JSONDecodeError:
|
| 80 |
+
return jsonify({"error": "Gemini API未返回有效的JSON格式", "details": analysis_content_string}), 500
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return jsonify({"error": f"发生未知错误: {e}"}), 500
|
| 83 |
|
| 84 |
if __name__ == '__main__':
|
|
|
|
| 85 |
app.run(host='0.0.0.0', port=8080)
|