leonsimon23 commited on
Commit
c888602
·
verified ·
1 Parent(s): 6e1e281

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -12,7 +12,7 @@ app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024 # 10MB max file size
12
 
13
  # Gemini API configuration
14
  GEMINI_API_KEY = os.environ.get('GEMINI_API_KEY')
15
- GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-image-preview:generateContent"
16
 
17
  ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
18
 
@@ -20,6 +20,7 @@ def allowed_file(filename):
20
  return '.' in filename and \
21
  filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
22
 
 
23
  def optimize_image(image_file, max_size=(1024, 1024), quality=85):
24
  """优化图像大小和质量"""
25
  try:
@@ -49,6 +50,7 @@ def optimize_image(image_file, max_size=(1024, 1024), quality=85):
49
  image_file.seek(0)
50
  return image_file.read()
51
 
 
52
  def call_gemini_api(text_content=None, image_data=None):
53
  """调用Gemini API进行分析"""
54
 
@@ -76,8 +78,8 @@ def call_gemini_api(text_content=None, image_data=None):
76
 
77
  ## 🎯 可能诊断
78
  1. **最可能诊断**: [诊断名称] - 可能性: X%
79
- - 依据: [具体理由]
80
-
81
  2. **次要考虑**: [其他可能诊断]
82
 
83
  ## 💊 治疗建议
@@ -195,12 +197,18 @@ def analyze():
195
  text_content = request.form.get('text', '').strip()
196
  image_data = None
197
 
198
- # 处理上传的图像
199
  if 'image' in request.files:
200
  file = request.files['image']
201
  if file.filename != '':
202
- if file and allowed_file(file.filename):
203
  try:
 
 
 
 
 
 
204
  # 优化图像
205
  image_data = optimize_image(file)
206
  except Exception as e:
@@ -242,14 +250,9 @@ def internal_error(e):
242
  return jsonify({"error": "服务器内部错误"}), 500
243
 
244
  if __name__ == '__main__':
245
- # 确保模板目录存在
246
  os.makedirs('templates', exist_ok=True)
247
-
248
- # 检查API密钥
249
  if not GEMINI_API_KEY:
250
  print("⚠️ 警告: 未设置GEMINI_API_KEY环境变量")
251
- print(" 请设置环境变量: export GEMINI_API_KEY=your_api_key")
252
-
253
- # 启动应用
254
- port = int(os.environ.get('PORT', 7860)) # Hugging Face默认端口
255
  app.run(host='0.0.0.0', port=port, debug=False)
 
12
 
13
  # Gemini API configuration
14
  GEMINI_API_KEY = os.environ.get('GEMINI_API_KEY')
15
+ GEMINI_API_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent"
16
 
17
  ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
18
 
 
20
  return '.' in filename and \
21
  filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
22
 
23
+ # 优化图像函数不需要改动
24
  def optimize_image(image_file, max_size=(1024, 1024), quality=85):
25
  """优化图像大小和质量"""
26
  try:
 
50
  image_file.seek(0)
51
  return image_file.read()
52
 
53
+ # call_gemini_api 函数不需要改动
54
  def call_gemini_api(text_content=None, image_data=None):
55
  """调用Gemini API进行分析"""
56
 
 
78
 
79
  ## 🎯 可能诊断
80
  1. **最可能诊断**: [诊断名称] - 可能性: X%
81
+ - 依据: [具体理由]
82
+
83
  2. **次要考虑**: [其他可能诊断]
84
 
85
  ## 💊 治疗建议
 
197
  text_content = request.form.get('text', '').strip()
198
  image_data = None
199
 
200
+ # 修复图像处理逻辑
201
  if 'image' in request.files:
202
  file = request.files['image']
203
  if file.filename != '':
204
+ if allowed_file(file.filename):
205
  try:
206
+ # 从 FileStorage 对象中直接读取数据
207
+ image_data = file.read()
208
+
209
+ # 确保文件指针回到开头,以便 optimize_image 函数能正确读取
210
+ file.seek(0)
211
+
212
  # 优化图像
213
  image_data = optimize_image(file)
214
  except Exception as e:
 
250
  return jsonify({"error": "服务器内部错误"}), 500
251
 
252
  if __name__ == '__main__':
 
253
  os.makedirs('templates', exist_ok=True)
 
 
254
  if not GEMINI_API_KEY:
255
  print("⚠️ 警告: 未设置GEMINI_API_KEY环境变量")
256
+ print(" 请设置环境变量: export GEMINI_API_KEY=your_api_key")
257
+ port = int(os.environ.get('PORT', 7860))
 
 
258
  app.run(host='0.0.0.0', port=port, debug=False)