Spaces:
Paused
Paused
Upload 4 files
Browse files
func.py
CHANGED
|
@@ -70,60 +70,74 @@ def get_gen_model(api_key, model, temperature, max_tokens):
|
|
| 70 |
return gen_model
|
| 71 |
|
| 72 |
def process_messages_for_gemini(messages):
|
| 73 |
-
|
| 74 |
gemini_history = []
|
|
|
|
| 75 |
for message in messages:
|
| 76 |
role = message.get('role')
|
| 77 |
content = message.get('content')
|
| 78 |
|
| 79 |
-
if isinstance(content, str):
|
| 80 |
if role == 'system':
|
| 81 |
gemini_history.append({"role": "user", "parts": [content]})
|
| 82 |
elif role == 'user':
|
| 83 |
gemini_history.append({"role": "user", "parts": [content]})
|
| 84 |
elif role == 'assistant':
|
| 85 |
gemini_history.append({"role": "model", "parts": [content]})
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
parts = []
|
| 88 |
for item in content:
|
| 89 |
if item.get('type') == 'text':
|
| 90 |
-
parts.append(item.get('text'))
|
| 91 |
elif item.get('type') == 'image_url':
|
| 92 |
image_data = item.get('image_url', {}).get('url', '')
|
| 93 |
if image_data.startswith('data:image/'):
|
| 94 |
-
try:
|
| 95 |
-
image_type = image_data.split(';')[0].split('/')[1].upper()
|
| 96 |
-
base64_image = image_data.split(';base64,')[1]
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
| 113 |
else:
|
| 114 |
-
|
| 115 |
|
| 116 |
-
if
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
| 122 |
|
| 123 |
if gemini_history:
|
| 124 |
user_message = gemini_history[-1]
|
| 125 |
-
gemini_history = gemini_history[:-1]
|
| 126 |
else:
|
| 127 |
user_message = {"role": "user", "parts": [""]}
|
| 128 |
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
return gen_model
|
| 71 |
|
| 72 |
def process_messages_for_gemini(messages):
|
|
|
|
| 73 |
gemini_history = []
|
| 74 |
+
errors = []
|
| 75 |
for message in messages:
|
| 76 |
role = message.get('role')
|
| 77 |
content = message.get('content')
|
| 78 |
|
| 79 |
+
if isinstance(content, str):
|
| 80 |
if role == 'system':
|
| 81 |
gemini_history.append({"role": "user", "parts": [content]})
|
| 82 |
elif role == 'user':
|
| 83 |
gemini_history.append({"role": "user", "parts": [content]})
|
| 84 |
elif role == 'assistant':
|
| 85 |
gemini_history.append({"role": "model", "parts": [content]})
|
| 86 |
+
else:
|
| 87 |
+
errors.append(f"Invalid role: {role}")
|
| 88 |
+
elif isinstance(content, list):
|
| 89 |
parts = []
|
| 90 |
for item in content:
|
| 91 |
if item.get('type') == 'text':
|
| 92 |
+
parts.append({"text": item.get('text')})
|
| 93 |
elif item.get('type') == 'image_url':
|
| 94 |
image_data = item.get('image_url', {}).get('url', '')
|
| 95 |
if image_data.startswith('data:image/'):
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
try:
|
| 98 |
+
mime_type, base64_data = image_data.split(';')[0].split(':')[1], image_data.split(',')[1]
|
| 99 |
+
parts.append({
|
| 100 |
+
"inline_data": {
|
| 101 |
+
"mime_type": mime_type,
|
| 102 |
+
"data": base64_data
|
| 103 |
+
}
|
| 104 |
+
})
|
| 105 |
+
except (IndexError, ValueError):
|
| 106 |
+
errors.append(f"Invalid data URI for image: {image_data}")
|
| 107 |
+
else:
|
| 108 |
+
errors.append(f"Invalid image URL format for item: {item}")
|
| 109 |
+
elif item.get('type') == 'file_url':
|
| 110 |
+
file_data = item.get('file_url', {}).get('url', '')
|
| 111 |
+
if file_data.startswith('data:'):
|
| 112 |
|
| 113 |
+
try:
|
| 114 |
+
mime_type, base64_data = file_data.split(';')[0].split(':')[1], file_data.split(',')[1]
|
| 115 |
+
parts.append({
|
| 116 |
+
"inline_data": {
|
| 117 |
+
"mime_type": mime_type,
|
| 118 |
+
"data": base64_data
|
| 119 |
+
}
|
| 120 |
+
})
|
| 121 |
+
except (IndexError, ValueError):
|
| 122 |
+
errors.append(f"Invalid data URI for file: {file_data}")
|
| 123 |
else:
|
| 124 |
+
errors.append(f"Invalid file URL format for item: {item}")
|
| 125 |
|
| 126 |
+
if parts:
|
| 127 |
+
if role in ['user', 'system']:
|
| 128 |
+
gemini_history.append({"role": "user", "parts": parts})
|
| 129 |
+
elif role == 'assistant':
|
| 130 |
+
gemini_history.append({"role": "model", "parts": parts})
|
| 131 |
+
else:
|
| 132 |
+
errors.append(f"Invalid role: {role}")
|
| 133 |
|
| 134 |
if gemini_history:
|
| 135 |
user_message = gemini_history[-1]
|
| 136 |
+
gemini_history = gemini_history[:-1]
|
| 137 |
else:
|
| 138 |
user_message = {"role": "user", "parts": [""]}
|
| 139 |
|
| 140 |
+
if errors:
|
| 141 |
+
return gemini_history, user_message, (jsonify({'error': errors}), 400)
|
| 142 |
+
else:
|
| 143 |
+
return gemini_history, user_message, None
|