thunder-lord commited on
Commit
7d3d1eb
·
verified ·
1 Parent(s): dc60e83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -7
app.py CHANGED
@@ -144,7 +144,7 @@ def get_styles():
144
  with open("style.json", "r") as style_file:
145
  styles = json.load(style_file)
146
 
147
- return jsonify({"styles": [style["name"] for style in styles]})
148
 
149
 
150
  @app.route('/upload/image', methods=['GET'])
@@ -259,10 +259,12 @@ genai.configure(api_key="AIzaSyBPIdkEyVTDZnmXrBi4ykf0sOfkbOvxAzo")
259
 
260
  DEFAULT_MODELS = ["gemini-1.0-pro", "gemini-1.0-pro-001"]
261
 
262
- @app.route('/gemini', methods=['GET'])
263
  def gemini():
264
- prompt = request.args.get('prompt')
265
- model_name = request.args.get('model')
 
 
266
 
267
  if not prompt:
268
  return jsonify({'error': 'Prompt parameter is required'}), 400
@@ -295,16 +297,55 @@ def gemini():
295
  )
296
 
297
  # Start the conversation and generate response
298
- convo = model.start_chat(history=[])
299
  convo.send_message(prompt)
300
  response = convo.last.text
301
 
302
- return jsonify({'status': 'true', 'response': response})
303
 
304
  except Exception as e:
305
  error_message = str(e)
306
  app.logger.error("Failed to generate content: %s", error_message)
307
- return jsonify({'status': 'false', 'error': 'Failed to generate content.'}), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
308
 
309
 
310
  if __name__ == "__main__":
 
144
  with open("style.json", "r") as style_file:
145
  styles = json.load(style_file)
146
 
147
+ return jsonify({'status': 'success', "styles": [style["name"] for style in styles]})
148
 
149
 
150
  @app.route('/upload/image', methods=['GET'])
 
259
 
260
  DEFAULT_MODELS = ["gemini-1.0-pro", "gemini-1.0-pro-001"]
261
 
262
+ @app.route('/gemini', methods=['POST'])
263
  def gemini():
264
+ data = request.json
265
+ prompt = data.get('prompt')
266
+ model_name = data.get('model')
267
+ messages = data.get('messages', [])
268
 
269
  if not prompt:
270
  return jsonify({'error': 'Prompt parameter is required'}), 400
 
297
  )
298
 
299
  # Start the conversation and generate response
300
+ convo = model.start_chat(history=messages)
301
  convo.send_message(prompt)
302
  response = convo.last.text
303
 
304
+ return jsonify({'status': 'success', 'response': response})
305
 
306
  except Exception as e:
307
  error_message = str(e)
308
  app.logger.error("Failed to generate content: %s", error_message)
309
+ return jsonify({'status': 'error', 'error': 'Failed to generate content.'}), 500
310
+
311
+
312
+
313
+ TOKEN_MESSAGES_TMP = {}
314
+
315
+ def generate_chat_id():
316
+ """Generate a random chat ID."""
317
+ while True:
318
+ chat_id = "Kastg_" + ''.join(random.choices(string.ascii_letters + string.digits, k=random.randint(15, 30)))
319
+ if len(chat_id) > 7:
320
+ return chat_id
321
+
322
+ @app.route('/tmp/chat', methods=['POST'])
323
+ def chat_tmp():
324
+ data = request.json
325
+ messages = data.get('messages')
326
+ chat_id = data.get('chat-id')
327
+
328
+ if not messages:
329
+ return jsonify({'status': 'error', 'error': 'Messages parameter is required'}), 400
330
+
331
+ if not chat_id:
332
+ # Generate a new chat ID
333
+ chat_id = generate_chat_id()
334
+ elif not chat_id.startswith('Kastg_'):
335
+ return jsonify({'status': 'error', 'error': 'Chat ID must start with "Kastg_"'}), 400
336
+ elif len(chat_id) <= 13:
337
+ return jsonify({'status': 'error', 'error': 'Chat ID must have more than 7 characters after Kastg_'}), 400
338
+
339
+ # Save messages under chat ID
340
+ if chat_id not in TOKEN_MESSAGES_TMP:
341
+ TOKEN_MESSAGES_TMP[chat_id] = []
342
+
343
+ for message in messages:
344
+ TOKEN_MESSAGES_TMP[chat_id].append(message)
345
+
346
+ # Return token and saved messages
347
+ response_data = {'creator': 'api.Kastg.com', 'status': 'success', 'chat-id': chat_id, 'messages': TOKEN_MESSAGES_TMP[chat_id]}
348
+ return jsonify(response_data)
349
 
350
 
351
  if __name__ == "__main__":