Spaces:
Paused
Paused
Upload 2 files
Browse files
app.py
CHANGED
|
@@ -232,6 +232,7 @@ GEMINI_MODELS = [
|
|
| 232 |
{"id": "gemini-2.0-flash-exp"},
|
| 233 |
{"id": "gemini-2.0-flash-thinking-exp-1219"},
|
| 234 |
{"id": "gemini-2.0-flash-thinking-exp-01-21"},
|
|
|
|
| 235 |
{"id": "gemini-2.0-pro-exp"}
|
| 236 |
]
|
| 237 |
|
|
@@ -689,7 +690,7 @@ def embeddings():
|
|
| 689 |
def keep_alive():
|
| 690 |
try:
|
| 691 |
response = requests.get("http://127.0.0.1:7860/", timeout=10)
|
| 692 |
-
response.raise_for_status()
|
| 693 |
print(f"Keep alive ping successful: {response.status_code} at {time.ctime()}")
|
| 694 |
except requests.exceptions.RequestException as e:
|
| 695 |
print(f"Keep alive ping failed: {e} at {time.ctime()}")
|
|
|
|
| 232 |
{"id": "gemini-2.0-flash-exp"},
|
| 233 |
{"id": "gemini-2.0-flash-thinking-exp-1219"},
|
| 234 |
{"id": "gemini-2.0-flash-thinking-exp-01-21"},
|
| 235 |
+
{"id": "gemini-2.0-exp"},
|
| 236 |
{"id": "gemini-2.0-pro-exp"}
|
| 237 |
]
|
| 238 |
|
|
|
|
| 690 |
def keep_alive():
|
| 691 |
try:
|
| 692 |
response = requests.get("http://127.0.0.1:7860/", timeout=10)
|
| 693 |
+
response.raise_for_status()
|
| 694 |
print(f"Keep alive ping successful: {response.status_code} at {time.ctime()}")
|
| 695 |
except requests.exceptions.RequestException as e:
|
| 696 |
print(f"Keep alive ping failed: {e} at {time.ctime()}")
|
func.py
CHANGED
|
@@ -37,25 +37,25 @@ def process_messages_for_gemini(messages, use_system_prompt=False):
|
|
| 37 |
errors = []
|
| 38 |
system_instruction_text = ""
|
| 39 |
is_system_phase = use_system_prompt
|
| 40 |
-
|
| 41 |
|
| 42 |
for i, message in enumerate(messages):
|
| 43 |
-
|
| 44 |
role = message.get('role')
|
| 45 |
content = message.get('content')
|
| 46 |
|
| 47 |
if isinstance(content, str):
|
| 48 |
-
|
| 49 |
if is_system_phase and role == 'system':
|
| 50 |
-
|
| 51 |
if system_instruction_text:
|
| 52 |
system_instruction_text += "\n" + content
|
| 53 |
else:
|
| 54 |
system_instruction_text = content
|
| 55 |
-
|
| 56 |
else:
|
| 57 |
is_system_phase = False
|
| 58 |
-
|
| 59 |
|
| 60 |
if role in ['user', 'system']:
|
| 61 |
role_to_use = 'user'
|
|
@@ -68,22 +68,22 @@ def process_messages_for_gemini(messages, use_system_prompt=False):
|
|
| 68 |
|
| 69 |
if gemini_history and gemini_history[-1]['role'] == role_to_use:
|
| 70 |
gemini_history[-1]['parts'].append({"text": content})
|
| 71 |
-
|
| 72 |
else:
|
| 73 |
gemini_history.append({"role": role_to_use, "parts": [{"text": content}]})
|
| 74 |
-
|
| 75 |
|
| 76 |
elif isinstance(content, list):
|
| 77 |
-
|
| 78 |
parts = []
|
| 79 |
for item in content:
|
| 80 |
-
|
| 81 |
if item.get('type') == 'text':
|
| 82 |
parts.append({"text": item.get('text')})
|
| 83 |
-
|
| 84 |
elif item.get('type') == 'image_url':
|
| 85 |
image_data = item.get('image_url', {}).get('url', '')
|
| 86 |
-
|
| 87 |
if image_data.startswith('data:image/'):
|
| 88 |
try:
|
| 89 |
mime_type, base64_data = image_data.split(';')[0].split(':')[1], image_data.split(',')[1]
|
|
@@ -93,7 +93,7 @@ def process_messages_for_gemini(messages, use_system_prompt=False):
|
|
| 93 |
"data": base64_data
|
| 94 |
}
|
| 95 |
})
|
| 96 |
-
|
| 97 |
except (IndexError, ValueError) as e:
|
| 98 |
error_message = f"Invalid data URI for image: {image_data}. Error: {e}"
|
| 99 |
errors.append(error_message)
|
|
@@ -104,7 +104,7 @@ def process_messages_for_gemini(messages, use_system_prompt=False):
|
|
| 104 |
logger.error(error_message)
|
| 105 |
elif item.get('type') == 'file_url':
|
| 106 |
file_data = item.get('file_url', {}).get('url', '')
|
| 107 |
-
|
| 108 |
if file_data.startswith('data:'):
|
| 109 |
try:
|
| 110 |
mime_type, base64_data = file_data.split(';')[0].split(':')[1], file_data.split(',')[1]
|
|
@@ -114,7 +114,7 @@ def process_messages_for_gemini(messages, use_system_prompt=False):
|
|
| 114 |
"data": base64_data
|
| 115 |
}
|
| 116 |
})
|
| 117 |
-
|
| 118 |
except (IndexError, ValueError) as e:
|
| 119 |
error_message = f"Invalid data URI for file: {file_data}. Error: {e}"
|
| 120 |
errors.append(error_message)
|
|
@@ -135,14 +135,14 @@ def process_messages_for_gemini(messages, use_system_prompt=False):
|
|
| 135 |
continue
|
| 136 |
if gemini_history and gemini_history[-1]['role'] == role_to_use:
|
| 137 |
gemini_history[-1]['parts'].extend(parts)
|
| 138 |
-
|
| 139 |
else:
|
| 140 |
gemini_history.append({"role": role_to_use, "parts": parts})
|
| 141 |
-
|
| 142 |
|
| 143 |
if errors:
|
| 144 |
logger.warning(f"Errors encountered during message processing: {errors}")
|
| 145 |
return gemini_history, {"parts": [{"text": system_instruction_text}]}, (jsonify({'error': errors}), 400)
|
| 146 |
else:
|
| 147 |
-
|
| 148 |
return gemini_history, {"parts": [{"text": system_instruction_text}]}, None
|
|
|
|
| 37 |
errors = []
|
| 38 |
system_instruction_text = ""
|
| 39 |
is_system_phase = use_system_prompt
|
| 40 |
+
|
| 41 |
|
| 42 |
for i, message in enumerate(messages):
|
| 43 |
+
|
| 44 |
role = message.get('role')
|
| 45 |
content = message.get('content')
|
| 46 |
|
| 47 |
if isinstance(content, str):
|
| 48 |
+
|
| 49 |
if is_system_phase and role == 'system':
|
| 50 |
+
|
| 51 |
if system_instruction_text:
|
| 52 |
system_instruction_text += "\n" + content
|
| 53 |
else:
|
| 54 |
system_instruction_text = content
|
| 55 |
+
|
| 56 |
else:
|
| 57 |
is_system_phase = False
|
| 58 |
+
|
| 59 |
|
| 60 |
if role in ['user', 'system']:
|
| 61 |
role_to_use = 'user'
|
|
|
|
| 68 |
|
| 69 |
if gemini_history and gemini_history[-1]['role'] == role_to_use:
|
| 70 |
gemini_history[-1]['parts'].append({"text": content})
|
| 71 |
+
|
| 72 |
else:
|
| 73 |
gemini_history.append({"role": role_to_use, "parts": [{"text": content}]})
|
| 74 |
+
|
| 75 |
|
| 76 |
elif isinstance(content, list):
|
| 77 |
+
|
| 78 |
parts = []
|
| 79 |
for item in content:
|
| 80 |
+
|
| 81 |
if item.get('type') == 'text':
|
| 82 |
parts.append({"text": item.get('text')})
|
| 83 |
+
|
| 84 |
elif item.get('type') == 'image_url':
|
| 85 |
image_data = item.get('image_url', {}).get('url', '')
|
| 86 |
+
|
| 87 |
if image_data.startswith('data:image/'):
|
| 88 |
try:
|
| 89 |
mime_type, base64_data = image_data.split(';')[0].split(':')[1], image_data.split(',')[1]
|
|
|
|
| 93 |
"data": base64_data
|
| 94 |
}
|
| 95 |
})
|
| 96 |
+
|
| 97 |
except (IndexError, ValueError) as e:
|
| 98 |
error_message = f"Invalid data URI for image: {image_data}. Error: {e}"
|
| 99 |
errors.append(error_message)
|
|
|
|
| 104 |
logger.error(error_message)
|
| 105 |
elif item.get('type') == 'file_url':
|
| 106 |
file_data = item.get('file_url', {}).get('url', '')
|
| 107 |
+
|
| 108 |
if file_data.startswith('data:'):
|
| 109 |
try:
|
| 110 |
mime_type, base64_data = file_data.split(';')[0].split(':')[1], file_data.split(',')[1]
|
|
|
|
| 114 |
"data": base64_data
|
| 115 |
}
|
| 116 |
})
|
| 117 |
+
|
| 118 |
except (IndexError, ValueError) as e:
|
| 119 |
error_message = f"Invalid data URI for file: {file_data}. Error: {e}"
|
| 120 |
errors.append(error_message)
|
|
|
|
| 135 |
continue
|
| 136 |
if gemini_history and gemini_history[-1]['role'] == role_to_use:
|
| 137 |
gemini_history[-1]['parts'].extend(parts)
|
| 138 |
+
|
| 139 |
else:
|
| 140 |
gemini_history.append({"role": role_to_use, "parts": parts})
|
| 141 |
+
|
| 142 |
|
| 143 |
if errors:
|
| 144 |
logger.warning(f"Errors encountered during message processing: {errors}")
|
| 145 |
return gemini_history, {"parts": [{"text": system_instruction_text}]}, (jsonify({'error': errors}), 400)
|
| 146 |
else:
|
| 147 |
+
|
| 148 |
return gemini_history, {"parts": [{"text": system_instruction_text}]}, None
|