Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,10 +8,10 @@ app = Flask(__name__, static_folder='static', static_url_path='')
|
|
| 8 |
|
| 9 |
# 从环境变量中获取API密钥,这是在Hugging Face上部署的最佳实践
|
| 10 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
# 这是整个系统的核心:一个精心设计的系统提示(Prompt)
|
| 14 |
-
# 它指导Gemini模型扮演角色、遵循指令,并以严格的JSON格式输出
|
| 15 |
SYSTEM_PROMPT = """
|
| 16 |
你是一个世界顶级的皮肤科医生AI助手。你的任务是基于用户上传的皮肤图像和病历文本,进行专业的临床诊疗辅助分析。
|
| 17 |
你的输出必须是一个单一、有效的JSON对象,不包含任何JSON格式之外的文字或标记(例如不要使用 "```json")。
|
|
@@ -20,13 +20,13 @@ SYSTEM_PROMPT = """
|
|
| 20 |
"image_features": ["从图像中识别出的关键皮损形态学特征列表", "例如:色素网络不规则", "例如:蓝白结构"],
|
| 21 |
"differential_diagnosis": [
|
| 22 |
{
|
| 23 |
-
"diagnosis": "可能性最高的诊断名称",
|
| 24 |
-
"likelihood": "高",
|
| 25 |
"evidence": "支持该诊断的关键图像和文本依据"
|
| 26 |
},
|
| 27 |
{
|
| 28 |
-
"diagnosis": "可能性次之的诊断名称",
|
| 29 |
-
"likelihood": "中",
|
| 30 |
"evidence": "支持该诊断的关键依据"
|
| 31 |
}
|
| 32 |
],
|
|
@@ -38,6 +38,15 @@ SYSTEM_PROMPT = """
|
|
| 38 |
}
|
| 39 |
"""
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
@app.route('/')
|
| 42 |
def index():
|
| 43 |
"""提供前端HTML页面"""
|
|
@@ -49,7 +58,6 @@ def analyze_skin():
|
|
| 49 |
if not GEMINI_API_KEY:
|
| 50 |
return jsonify({"error": "服务端未配置GEMINI_API_KEY"}), 500
|
| 51 |
|
| 52 |
-
# 从请求中获取JSON数据
|
| 53 |
data = request.json
|
| 54 |
if not data or 'image' not in data or 'text' not in data:
|
| 55 |
return jsonify({"error": "请求中缺少图像或文本信息"}), 400
|
|
@@ -61,7 +69,6 @@ def analyze_skin():
|
|
| 61 |
'Content-Type': 'application/json',
|
| 62 |
}
|
| 63 |
|
| 64 |
-
# 构造发送给Gemini API的请求体
|
| 65 |
payload = {
|
| 66 |
"contents": [
|
| 67 |
{
|
|
@@ -80,35 +87,27 @@ def analyze_skin():
|
|
| 80 |
}
|
| 81 |
|
| 82 |
try:
|
| 83 |
-
|
| 84 |
-
response = requests.post(f"{GEMINI_API_URL}?key={GEMINI_API_KEY}", headers=headers, json=payload)
|
| 85 |
-
# 如果API返回错误状态码,则抛出异常
|
| 86 |
response.raise_for_status()
|
| 87 |
|
| 88 |
gemini_result = response.json()
|
| 89 |
|
| 90 |
-
# 提取模型返回的核心内容
|
| 91 |
analysis_content_string = gemini_result['candidates']['content']['parts']['text']
|
| 92 |
|
| 93 |
-
# 将模型返回的JSON字符串解析为Python字典
|
| 94 |
parsed_json = json.loads(analysis_content_string)
|
| 95 |
|
| 96 |
return jsonify(parsed_json)
|
| 97 |
|
| 98 |
except requests.exceptions.RequestException as e:
|
| 99 |
-
# 处理网络错误
|
| 100 |
return jsonify({"error": f"调用Gemini API时发生网络错误: {e}"}), 500
|
| 101 |
except (KeyError, IndexError):
|
| 102 |
-
#
|
| 103 |
-
|
|
|
|
| 104 |
except json.JSONDecodeError:
|
| 105 |
-
|
| 106 |
-
return jsonify({"error": "Gemini API未返回有效的JSON格式"}), 500
|
| 107 |
except Exception as e:
|
| 108 |
-
# 处理其他未知错误
|
| 109 |
return jsonify({"error": f"发生未知错误: {e}"}), 500
|
| 110 |
|
| 111 |
if __name__ == '__main__':
|
| 112 |
-
# 使用 0.0.0.0 主机以确保在Docker容器内可以从外部访问
|
| 113 |
-
# Hugging Face Spaces通常需要应用在8080端口上运行
|
| 114 |
app.run(host='0.0.0.0', port=8080)
|
|
|
|
| 8 |
|
| 9 |
# 从环境变量中获取API密钥,这是在Hugging Face上部署的最佳实践
|
| 10 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 11 |
+
# 修正:使用稳定且公开的 gemini-pro-vision 模型端点
|
| 12 |
+
GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent"
|
| 13 |
|
| 14 |
# 这是整个系统的核心:一个精心设计的系统提示(Prompt)
|
|
|
|
| 15 |
SYSTEM_PROMPT = """
|
| 16 |
你是一个世界顶级的皮肤科医生AI助手。你的任务是基于用户上传的皮肤图像和病历文本,进行专业的临床诊疗辅助分析。
|
| 17 |
你的输出必须是一个单一、有效的JSON对象,不包含任何JSON格式之外的文字或标记(例如不要使用 "```json")。
|
|
|
|
| 20 |
"image_features": ["从图像中识别出的关键皮损形态学特征列表", "例如:色素网络不规则", "例如:蓝白结构"],
|
| 21 |
"differential_diagnosis": [
|
| 22 |
{
|
| 23 |
+
"diagnosis": "可能性最高的诊断名称",
|
| 24 |
+
"likelihood": "高",
|
| 25 |
"evidence": "支持该诊断的关键图像和文本依据"
|
| 26 |
},
|
| 27 |
{
|
| 28 |
+
"diagnosis": "可能性次之的诊断名称",
|
| 29 |
+
"likelihood": "中",
|
| 30 |
"evidence": "支持该诊断的关键依据"
|
| 31 |
}
|
| 32 |
],
|
|
|
|
| 38 |
}
|
| 39 |
"""
|
| 40 |
|
| 41 |
+
# ==============================================================================
|
| 42 |
+
# == 新增的健康检查路由 ==
|
| 43 |
+
# 这个路由非常简单,只返回"OK"和200状态码,确保健康检查能100%通过
|
| 44 |
+
# ==============================================================================
|
| 45 |
+
@app.route('/healthz')
|
| 46 |
+
def health_check():
|
| 47 |
+
"""Health check endpoint"""
|
| 48 |
+
return "OK", 200
|
| 49 |
+
|
| 50 |
@app.route('/')
|
| 51 |
def index():
|
| 52 |
"""提供前端HTML页面"""
|
|
|
|
| 58 |
if not GEMINI_API_KEY:
|
| 59 |
return jsonify({"error": "服务端未配置GEMINI_API_KEY"}), 500
|
| 60 |
|
|
|
|
| 61 |
data = request.json
|
| 62 |
if not data or 'image' not in data or 'text' not in data:
|
| 63 |
return jsonify({"error": "请求中缺少图像或文本信息"}), 400
|
|
|
|
| 69 |
'Content-Type': 'application/json',
|
| 70 |
}
|
| 71 |
|
|
|
|
| 72 |
payload = {
|
| 73 |
"contents": [
|
| 74 |
{
|
|
|
|
| 87 |
}
|
| 88 |
|
| 89 |
try:
|
| 90 |
+
response = requests.post(f"{GEMINI_API_URL}?key={GEMINI_API_KEY}", headers=headers, json=payload, timeout=60)
|
|
|
|
|
|
|
| 91 |
response.raise_for_status()
|
| 92 |
|
| 93 |
gemini_result = response.json()
|
| 94 |
|
|
|
|
| 95 |
analysis_content_string = gemini_result['candidates']['content']['parts']['text']
|
| 96 |
|
|
|
|
| 97 |
parsed_json = json.loads(analysis_content_string)
|
| 98 |
|
| 99 |
return jsonify(parsed_json)
|
| 100 |
|
| 101 |
except requests.exceptions.RequestException as e:
|
|
|
|
| 102 |
return jsonify({"error": f"调用Gemini API时发生网络错误: {e}"}), 500
|
| 103 |
except (KeyError, IndexError):
|
| 104 |
+
# 增加对返回结果的打印,方便调试
|
| 105 |
+
error_details = response.json() if 'response' in locals() else "No response from API"
|
| 106 |
+
return jsonify({"error": "解析Gemini API响应失败,格式不正确", "details": error_details}), 500
|
| 107 |
except json.JSONDecodeError:
|
| 108 |
+
return jsonify({"error": "Gemini API未返回有效的JSON格式", "details": analysis_content_string}), 500
|
|
|
|
| 109 |
except Exception as e:
|
|
|
|
| 110 |
return jsonify({"error": f"发生未知错误: {e}"}), 500
|
| 111 |
|
| 112 |
if __name__ == '__main__':
|
|
|
|
|
|
|
| 113 |
app.run(host='0.0.0.0', port=8080)
|