Spaces:
Building
Building
Upload 4 files
Browse files
app.py
CHANGED
@@ -128,10 +128,10 @@ def chat_completions():
|
|
128 |
logger.info(f"\n{model} [r] -> {current_api_key[:11]}...")
|
129 |
|
130 |
gemini_history, user_message, error_response = func.process_messages_for_gemini(messages)
|
131 |
-
|
132 |
if error_response:
|
133 |
print(error_response)
|
134 |
-
|
135 |
genai.configure(api_key=current_api_key)
|
136 |
|
137 |
generation_config = {
|
@@ -169,7 +169,7 @@ def chat_completions():
|
|
169 |
],
|
170 |
'object': 'chat.completion.chunk'
|
171 |
}
|
172 |
-
|
173 |
yield f"data: {json.dumps(data)}\n\n"
|
174 |
data = {
|
175 |
'choices': [
|
@@ -181,11 +181,11 @@ def chat_completions():
|
|
181 |
],
|
182 |
'object': 'chat.completion.chunk'
|
183 |
}
|
184 |
-
|
185 |
yield f"data: {json.dumps(data)}\n\n"
|
186 |
except Exception as e:
|
187 |
logger.error(f"Error during streaming: {str(e)}")
|
188 |
-
|
189 |
data = {
|
190 |
'error': {
|
191 |
'message': str(e),
|
@@ -196,21 +196,21 @@ def chat_completions():
|
|
196 |
|
197 |
return Response(stream_with_context(generate()), mimetype='text/event-stream')
|
198 |
else:
|
199 |
-
|
200 |
if gemini_history:
|
201 |
chat_session = gen_model.start_chat(history=gemini_history)
|
202 |
response = chat_session.send_message(user_message)
|
203 |
else:
|
204 |
response = gen_model.generate_content(user_message)
|
205 |
-
|
206 |
try:
|
207 |
text_content = response.candidates[0].content.parts[0].text
|
208 |
-
|
209 |
except (AttributeError, IndexError, TypeError) as e:
|
210 |
logger.error(f"Error getting text content: {str(e)}")
|
211 |
-
|
212 |
text_content = "Error: Unable to get text content."
|
213 |
-
|
214 |
response_data = {
|
215 |
'id': 'chatcmpl-xxxxxxxxxxxx',
|
216 |
'object': 'chat.completion',
|
@@ -263,6 +263,6 @@ def keep_alive():
|
|
263 |
if __name__ == '__main__':
|
264 |
scheduler = BackgroundScheduler()
|
265 |
scheduler.add_job(keep_alive, 'interval', hours = 12)
|
266 |
-
|
267 |
scheduler.start()
|
268 |
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
|
|
128 |
logger.info(f"\n{model} [r] -> {current_api_key[:11]}...")
|
129 |
|
130 |
gemini_history, user_message, error_response = func.process_messages_for_gemini(messages)
|
131 |
+
|
132 |
if error_response:
|
133 |
print(error_response)
|
134 |
+
|
135 |
genai.configure(api_key=current_api_key)
|
136 |
|
137 |
generation_config = {
|
|
|
169 |
],
|
170 |
'object': 'chat.completion.chunk'
|
171 |
}
|
172 |
+
|
173 |
yield f"data: {json.dumps(data)}\n\n"
|
174 |
data = {
|
175 |
'choices': [
|
|
|
181 |
],
|
182 |
'object': 'chat.completion.chunk'
|
183 |
}
|
184 |
+
|
185 |
yield f"data: {json.dumps(data)}\n\n"
|
186 |
except Exception as e:
|
187 |
logger.error(f"Error during streaming: {str(e)}")
|
188 |
+
|
189 |
data = {
|
190 |
'error': {
|
191 |
'message': str(e),
|
|
|
196 |
|
197 |
return Response(stream_with_context(generate()), mimetype='text/event-stream')
|
198 |
else:
|
199 |
+
|
200 |
if gemini_history:
|
201 |
chat_session = gen_model.start_chat(history=gemini_history)
|
202 |
response = chat_session.send_message(user_message)
|
203 |
else:
|
204 |
response = gen_model.generate_content(user_message)
|
205 |
+
|
206 |
try:
|
207 |
text_content = response.candidates[0].content.parts[0].text
|
208 |
+
|
209 |
except (AttributeError, IndexError, TypeError) as e:
|
210 |
logger.error(f"Error getting text content: {str(e)}")
|
211 |
+
|
212 |
text_content = "Error: Unable to get text content."
|
213 |
+
|
214 |
response_data = {
|
215 |
'id': 'chatcmpl-xxxxxxxxxxxx',
|
216 |
'object': 'chat.completion',
|
|
|
263 |
if __name__ == '__main__':
|
264 |
scheduler = BackgroundScheduler()
|
265 |
scheduler.add_job(keep_alive, 'interval', hours = 12)
|
266 |
+
|
267 |
scheduler.start()
|
268 |
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
|
func.py
CHANGED
@@ -15,7 +15,6 @@ def authenticate_request(request):
|
|
15 |
|
16 |
if not auth_header:
|
17 |
return False, jsonify({'error': '缺少Authorization请求头'}), 401
|
18 |
-
|
19 |
try:
|
20 |
auth_type, pass_word = auth_header.split(' ', 1)
|
21 |
except ValueError:
|
|
|
15 |
|
16 |
if not auth_header:
|
17 |
return False, jsonify({'error': '缺少Authorization请求头'}), 401
|
|
|
18 |
try:
|
19 |
auth_type, pass_word = auth_header.split(' ', 1)
|
20 |
except ValueError:
|