Moonfanz commited on
Commit
ce63f30
·
verified ·
1 Parent(s): ce9d099

Upload 4 files

Browse files
Files changed (2) hide show
  1. app.py +0 -1
  2. func.py +5 -10
app.py CHANGED
@@ -107,7 +107,6 @@ def chat_completions():
107
  safety_settings=safety_settings
108
  )
109
 
110
-
111
  if stream:
112
 
113
  if gemini_history:
 
107
  safety_settings=safety_settings
108
  )
109
 
 
110
  if stream:
111
 
112
  if gemini_history:
func.py CHANGED
@@ -34,14 +34,14 @@ def process_messages_for_gemini(messages):
34
  role = message.get('role')
35
  content = message.get('content')
36
 
37
- if isinstance(content, str): # 纯文本
38
  if role == 'system':
39
  gemini_history.append({"role": "user", "parts": [content]})
40
  elif role == 'user':
41
  gemini_history.append({"role": "user", "parts": [content]})
42
  elif role == 'assistant':
43
  gemini_history.append({"role": "model", "parts": [content]})
44
- elif isinstance(content, list): # 图文
45
  parts = []
46
  for item in content:
47
  if item.get('type') == 'text':
@@ -50,22 +50,19 @@ def process_messages_for_gemini(messages):
50
  image_data = item.get('image_url', {}).get('url', '')
51
  if image_data.startswith('data:image/'):
52
  try:
53
- # 提取 base64 编码和图片类型
54
- image_type = image_data.split(';')[0].split('/')[1].upper() # 提取图片类型并转为大写
55
  base64_image = image_data.split(';base64,')[1]
56
 
57
  image = Image.open(BytesIO(base64.b64decode(base64_image)))
58
 
59
- # 将图片转换为 RGB 模式
60
  if image.mode != 'RGB':
61
  image = image.convert('RGB')
62
 
63
- # 压缩图像
64
  if image.width > 2048 or image.height > 2048:
65
  image.thumbnail((2048, 2048))
66
 
67
  output_buffer = BytesIO()
68
- image.save(output_buffer, format=image_type) # 使用原始图片类型保存
69
  output_buffer.seek(0)
70
  parts.append(image)
71
  except Exception as e:
@@ -74,7 +71,6 @@ def process_messages_for_gemini(messages):
74
  else:
75
  return [], None, (jsonify({'error': 'Invalid image URL format'}), 400)
76
 
77
- # 根据 role 添加到 gemini_history
78
  if role in ['user', 'system']:
79
  gemini_history.append({"role": "user", "parts": parts})
80
  elif role == 'assistant':
@@ -82,10 +78,9 @@ def process_messages_for_gemini(messages):
82
  else:
83
  return [], None, (jsonify({'error': f'Invalid role: {role}'}), 400)
84
 
85
- # 用户最后一条消息
86
  if gemini_history:
87
  user_message = gemini_history[-1]
88
- gemini_history = gemini_history[:-1] # 历史记录不包含最后一条消息
89
  else:
90
  user_message = {"role": "user", "parts": [""]}
91
 
 
34
  role = message.get('role')
35
  content = message.get('content')
36
 
37
+ if isinstance(content, str):
38
  if role == 'system':
39
  gemini_history.append({"role": "user", "parts": [content]})
40
  elif role == 'user':
41
  gemini_history.append({"role": "user", "parts": [content]})
42
  elif role == 'assistant':
43
  gemini_history.append({"role": "model", "parts": [content]})
44
+ elif isinstance(content, list):
45
  parts = []
46
  for item in content:
47
  if item.get('type') == 'text':
 
50
  image_data = item.get('image_url', {}).get('url', '')
51
  if image_data.startswith('data:image/'):
52
  try:
53
+ image_type = image_data.split(';')[0].split('/')[1].upper()
 
54
  base64_image = image_data.split(';base64,')[1]
55
 
56
  image = Image.open(BytesIO(base64.b64decode(base64_image)))
57
 
 
58
  if image.mode != 'RGB':
59
  image = image.convert('RGB')
60
 
 
61
  if image.width > 2048 or image.height > 2048:
62
  image.thumbnail((2048, 2048))
63
 
64
  output_buffer = BytesIO()
65
+ image.save(output_buffer, format=image_type)
66
  output_buffer.seek(0)
67
  parts.append(image)
68
  except Exception as e:
 
71
  else:
72
  return [], None, (jsonify({'error': 'Invalid image URL format'}), 400)
73
 
 
74
  if role in ['user', 'system']:
75
  gemini_history.append({"role": "user", "parts": parts})
76
  elif role == 'assistant':
 
78
  else:
79
  return [], None, (jsonify({'error': f'Invalid role: {role}'}), 400)
80
 
 
81
  if gemini_history:
82
  user_message = gemini_history[-1]
83
+ gemini_history = gemini_history[:-1]
84
  else:
85
  user_message = {"role": "user", "parts": [""]}
86