Spaces:
Building
Building
Delete func.py
Browse files
func.py
DELETED
@@ -1,112 +0,0 @@
|
|
1 |
-
from flask import jsonify
|
2 |
-
import logging
|
3 |
-
import os
|
4 |
-
logger = logging.getLogger(__name__)
|
5 |
-
|
6 |
-
request_counts = {}
|
7 |
-
|
8 |
-
password = os.environ['password']
|
9 |
-
|
10 |
-
def authenticate_request(request):
|
11 |
-
auth_header = request.headers.get('Authorization')
|
12 |
-
|
13 |
-
if not auth_header:
|
14 |
-
return False, jsonify({'error': '缺少Authorization请求头'}), 401
|
15 |
-
|
16 |
-
try:
|
17 |
-
auth_type, pass_word = auth_header.split(' ', 1)
|
18 |
-
except ValueError:
|
19 |
-
return False, jsonify({'error': 'Authorization请求头格式错误'}), 401
|
20 |
-
|
21 |
-
if auth_type.lower() != 'bearer':
|
22 |
-
return False, jsonify({'error': 'Authorization类型必须为Bearer'}), 401
|
23 |
-
|
24 |
-
if pass_word != password:
|
25 |
-
return False, jsonify({'error': '未授权'}), 401
|
26 |
-
|
27 |
-
return True, None, None
|
28 |
-
|
29 |
-
def process_messages_for_gemini(messages, use_system_prompt=False):
|
30 |
-
gemini_history = []
|
31 |
-
errors = []
|
32 |
-
system_instruction_text = ""
|
33 |
-
is_system_phase = use_system_prompt
|
34 |
-
for i, message in enumerate(messages):
|
35 |
-
role = message.get('role')
|
36 |
-
content = message.get('content')
|
37 |
-
|
38 |
-
if isinstance(content, str):
|
39 |
-
if is_system_phase and role == 'system':
|
40 |
-
if system_instruction_text:
|
41 |
-
system_instruction_text += "\n" + content
|
42 |
-
else:
|
43 |
-
system_instruction_text = content
|
44 |
-
else:
|
45 |
-
is_system_phase = False
|
46 |
-
|
47 |
-
if role in ['user', 'system']:
|
48 |
-
role_to_use = 'user'
|
49 |
-
elif role == 'assistant':
|
50 |
-
role_to_use = 'model'
|
51 |
-
else:
|
52 |
-
errors.append(f"Invalid role: {role}")
|
53 |
-
continue
|
54 |
-
|
55 |
-
if gemini_history and gemini_history[-1]['role'] == role_to_use:
|
56 |
-
gemini_history[-1]['parts'].append({"text": content})
|
57 |
-
else:
|
58 |
-
gemini_history.append({"role": role_to_use, "parts": [{"text": content}]})
|
59 |
-
|
60 |
-
elif isinstance(content, list):
|
61 |
-
parts = []
|
62 |
-
for item in content:
|
63 |
-
if item.get('type') == 'text':
|
64 |
-
parts.append({"text": item.get('text')})
|
65 |
-
elif item.get('type') == 'image_url':
|
66 |
-
image_data = item.get('image_url', {}).get('url', '')
|
67 |
-
if image_data.startswith('data:image/'):
|
68 |
-
try:
|
69 |
-
mime_type, base64_data = image_data.split(';')[0].split(':')[1], image_data.split(',')[1]
|
70 |
-
parts.append({
|
71 |
-
"inline_data": {
|
72 |
-
"mime_type": mime_type,
|
73 |
-
"data": base64_data
|
74 |
-
}
|
75 |
-
})
|
76 |
-
except (IndexError, ValueError):
|
77 |
-
errors.append(f"Invalid data URI for image: {image_data}")
|
78 |
-
else:
|
79 |
-
errors.append(f"Invalid image URL format for item: {item}")
|
80 |
-
elif item.get('type') == 'file_url':
|
81 |
-
file_data = item.get('file_url', {}).get('url', '')
|
82 |
-
if file_data.startswith('data:'):
|
83 |
-
try:
|
84 |
-
mime_type, base64_data = file_data.split(';')[0].split(':')[1], file_data.split(',')[1]
|
85 |
-
parts.append({
|
86 |
-
"inline_data": {
|
87 |
-
"mime_type": mime_type,
|
88 |
-
"data": base64_data
|
89 |
-
}
|
90 |
-
})
|
91 |
-
except (IndexError, ValueError):
|
92 |
-
errors.append(f"Invalid data URI for file: {file_data}")
|
93 |
-
else:
|
94 |
-
errors.append(f"Invalid file URL format for item: {item}")
|
95 |
-
|
96 |
-
if parts:
|
97 |
-
if role in ['user', 'system']:
|
98 |
-
role_to_use = 'user'
|
99 |
-
elif role == 'assistant':
|
100 |
-
role_to_use = 'model'
|
101 |
-
else:
|
102 |
-
errors.append(f"Invalid role: {role}")
|
103 |
-
continue
|
104 |
-
if gemini_history and gemini_history[-1]['role'] == role_to_use:
|
105 |
-
gemini_history[-1]['parts'].extend(parts)
|
106 |
-
else:
|
107 |
-
gemini_history.append({"role": role_to_use, "parts": parts})
|
108 |
-
|
109 |
-
if errors:
|
110 |
-
return gemini_history, {"parts": [{"text": system_instruction_text}]}, (jsonify({'error': errors}), 400)
|
111 |
-
else:
|
112 |
-
return gemini_history, {"parts": [{"text": system_instruction_text}]}, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|