Spaces:
Paused
Paused
Upload 2 files
Browse files
func.py
CHANGED
|
@@ -1,13 +1,6 @@
|
|
| 1 |
-
from io import BytesIO
|
| 2 |
-
import base64
|
| 3 |
-
from PIL import Image
|
| 4 |
from flask import jsonify
|
| 5 |
import logging
|
| 6 |
-
import json
|
| 7 |
-
import re
|
| 8 |
import os
|
| 9 |
-
import requests
|
| 10 |
-
import google.generativeai as genai
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
|
|
@@ -34,12 +27,14 @@ def authenticate_request(request):
|
|
| 34 |
|
| 35 |
return True, None, None
|
| 36 |
|
|
|
|
|
|
|
| 37 |
def process_messages_for_gemini(messages):
|
| 38 |
gemini_history = []
|
| 39 |
errors = []
|
| 40 |
system_instruction_text = ""
|
| 41 |
is_system_phase = True
|
| 42 |
-
for message in messages:
|
| 43 |
role = message.get('role')
|
| 44 |
content = message.get('content')
|
| 45 |
|
|
@@ -52,23 +47,27 @@ def process_messages_for_gemini(messages):
|
|
| 52 |
else:
|
| 53 |
is_system_phase = False
|
| 54 |
|
| 55 |
-
if role
|
| 56 |
-
|
| 57 |
-
elif role == 'system':
|
| 58 |
-
gemini_history.append({"role": "user", "parts": [{"text": content}]})
|
| 59 |
elif role == 'assistant':
|
| 60 |
-
|
| 61 |
else:
|
| 62 |
errors.append(f"Invalid role: {role}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
elif isinstance(content, list):
|
| 64 |
parts = []
|
| 65 |
for item in content:
|
| 66 |
if item.get('type') == 'text':
|
| 67 |
-
parts.append({"text": item.get('text')})
|
| 68 |
elif item.get('type') == 'image_url':
|
| 69 |
image_data = item.get('image_url', {}).get('url', '')
|
| 70 |
if image_data.startswith('data:image/'):
|
| 71 |
-
|
| 72 |
try:
|
| 73 |
mime_type, base64_data = image_data.split(';')[0].split(':')[1], image_data.split(',')[1]
|
| 74 |
parts.append({
|
|
@@ -84,7 +83,6 @@ def process_messages_for_gemini(messages):
|
|
| 84 |
elif item.get('type') == 'file_url':
|
| 85 |
file_data = item.get('file_url', {}).get('url', '')
|
| 86 |
if file_data.startswith('data:'):
|
| 87 |
-
|
| 88 |
try:
|
| 89 |
mime_type, base64_data = file_data.split(';')[0].split(':')[1], file_data.split(',')[1]
|
| 90 |
parts.append({
|
|
@@ -98,13 +96,18 @@ def process_messages_for_gemini(messages):
|
|
| 98 |
else:
|
| 99 |
errors.append(f"Invalid file URL format for item: {item}")
|
| 100 |
|
| 101 |
-
if parts:
|
| 102 |
if role in ['user', 'system']:
|
| 103 |
-
|
| 104 |
-
elif role
|
| 105 |
-
|
| 106 |
else:
|
| 107 |
errors.append(f"Invalid role: {role}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
if errors:
|
| 110 |
return gemini_history, {"parts": [{"text": system_instruction_text}]}, (jsonify({'error': errors}), 400)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from flask import jsonify
|
| 2 |
import logging
|
|
|
|
|
|
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
logger = logging.getLogger(__name__)
|
| 5 |
|
| 6 |
|
|
|
|
| 27 |
|
| 28 |
return True, None, None
|
| 29 |
|
| 30 |
+
from flask import jsonify
|
| 31 |
+
|
| 32 |
def process_messages_for_gemini(messages):
|
| 33 |
gemini_history = []
|
| 34 |
errors = []
|
| 35 |
system_instruction_text = ""
|
| 36 |
is_system_phase = True
|
| 37 |
+
for i, message in enumerate(messages):
|
| 38 |
role = message.get('role')
|
| 39 |
content = message.get('content')
|
| 40 |
|
|
|
|
| 47 |
else:
|
| 48 |
is_system_phase = False
|
| 49 |
|
| 50 |
+
if role in ['user', 'system']:
|
| 51 |
+
role_to_use = 'user'
|
|
|
|
|
|
|
| 52 |
elif role == 'assistant':
|
| 53 |
+
role_to_use = 'model'
|
| 54 |
else:
|
| 55 |
errors.append(f"Invalid role: {role}")
|
| 56 |
+
continue
|
| 57 |
+
|
| 58 |
+
if gemini_history and gemini_history[-1]['role'] == role_to_use:
|
| 59 |
+
gemini_history[-1]['parts'].append({"text": content})
|
| 60 |
+
else:
|
| 61 |
+
gemini_history.append({"role": role_to_use, "parts": [{"text": content}]})
|
| 62 |
+
|
| 63 |
elif isinstance(content, list):
|
| 64 |
parts = []
|
| 65 |
for item in content:
|
| 66 |
if item.get('type') == 'text':
|
| 67 |
+
parts.append({"text": item.get('text')})
|
| 68 |
elif item.get('type') == 'image_url':
|
| 69 |
image_data = item.get('image_url', {}).get('url', '')
|
| 70 |
if image_data.startswith('data:image/'):
|
|
|
|
| 71 |
try:
|
| 72 |
mime_type, base64_data = image_data.split(';')[0].split(':')[1], image_data.split(',')[1]
|
| 73 |
parts.append({
|
|
|
|
| 83 |
elif item.get('type') == 'file_url':
|
| 84 |
file_data = item.get('file_url', {}).get('url', '')
|
| 85 |
if file_data.startswith('data:'):
|
|
|
|
| 86 |
try:
|
| 87 |
mime_type, base64_data = file_data.split(';')[0].split(':')[1], file_data.split(',')[1]
|
| 88 |
parts.append({
|
|
|
|
| 96 |
else:
|
| 97 |
errors.append(f"Invalid file URL format for item: {item}")
|
| 98 |
|
| 99 |
+
if parts:
|
| 100 |
if role in ['user', 'system']:
|
| 101 |
+
role_to_use = 'user'
|
| 102 |
+
elif role == 'assistant':
|
| 103 |
+
role_to_use = 'model'
|
| 104 |
else:
|
| 105 |
errors.append(f"Invalid role: {role}")
|
| 106 |
+
continue
|
| 107 |
+
if gemini_history and gemini_history[-1]['role'] == role_to_use:
|
| 108 |
+
gemini_history[-1]['parts'].extend(parts)
|
| 109 |
+
else:
|
| 110 |
+
gemini_history.append({"role": role_to_use, "parts": parts})
|
| 111 |
|
| 112 |
if errors:
|
| 113 |
return gemini_history, {"parts": [{"text": system_instruction_text}]}, (jsonify({'error': errors}), 400)
|