Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,7 @@ import os
|
|
| 13 |
import re
|
| 14 |
import uuid
|
| 15 |
from gtts import gTTS
|
|
|
|
| 16 |
|
| 17 |
# app = FastAPI()
|
| 18 |
app = Flask(__name__)
|
|
@@ -481,6 +482,56 @@ def save_donation():
|
|
| 481 |
return {"error": f"An error occurred: {str(e)}"}, 500
|
| 482 |
|
| 483 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
|
| 485 |
if __name__ == "__main__":
|
| 486 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 13 |
import re
|
| 14 |
import uuid
|
| 15 |
from gtts import gTTS
|
| 16 |
+
from bson import ObjectId
|
| 17 |
|
| 18 |
# app = FastAPI()
|
| 19 |
app = Flask(__name__)
|
|
|
|
| 482 |
return {"error": f"An error occurred: {str(e)}"}, 500
|
| 483 |
|
| 484 |
|
| 485 |
+
@app.get('/chats/<username>')
|
| 486 |
+
def list_chats(username):
|
| 487 |
+
items = list(chat_collection.find({'username': username}, {'_id':1,'user_data':1,'bot_data':1,'timestamp':1,'category':1}).sort('timestamp', -1))
|
| 488 |
+
out = []
|
| 489 |
+
for it in items:
|
| 490 |
+
out.append({
|
| 491 |
+
'id': str(it['_id']),
|
| 492 |
+
'title': (it.get('user_data') or 'Chat')[:40],
|
| 493 |
+
'timestamp': it.get('timestamp'),
|
| 494 |
+
'category': it.get('category','General')
|
| 495 |
+
})
|
| 496 |
+
return jsonify(out), 200
|
| 497 |
+
|
| 498 |
+
@app.patch('/chat/<chat_id>/rename')
|
| 499 |
+
def rename_chat(chat_id):
|
| 500 |
+
title = (request.get_json() or {}).get('title')
|
| 501 |
+
if not title: return jsonify({'error':'title required'}), 400
|
| 502 |
+
chat_collection.update_one({'_id': ObjectId(chat_id)}, {'$set': {'title': title}})
|
| 503 |
+
return jsonify({'ok': True}), 200
|
| 504 |
+
|
| 505 |
+
@app.delete('/chat/<chat_id>')
|
| 506 |
+
def delete_chat(chat_id):
|
| 507 |
+
chat_collection.delete_one({'_id': ObjectId(chat_id)})
|
| 508 |
+
return jsonify({'ok': True}), 200
|
| 509 |
+
|
| 510 |
+
# Simple memory K/V
|
| 511 |
+
mem = client['user_memory_db']
|
| 512 |
+
mem_col = mem['memory']
|
| 513 |
+
|
| 514 |
+
@app.post('/memory/<username>')
|
| 515 |
+
def save_memory(username):
|
| 516 |
+
data = request.get_json() or {}
|
| 517 |
+
key, value = data.get('key'), data.get('value')
|
| 518 |
+
if not key: return jsonify({'error':'key required'}), 400
|
| 519 |
+
mem_col.update_one({'username': username, 'key': key},
|
| 520 |
+
{'$set': {'value': value, 'updated': datetime.datetime.now()}},
|
| 521 |
+
upsert=True)
|
| 522 |
+
return jsonify({'ok': True}), 200
|
| 523 |
+
|
| 524 |
+
@app.get('/memory/<username>')
|
| 525 |
+
def get_memory(username):
|
| 526 |
+
items = list(mem_col.find({'username': username}, {'_id':0}))
|
| 527 |
+
return jsonify(items), 200
|
| 528 |
+
|
| 529 |
+
@app.delete('/memory/<username>/<key>')
|
| 530 |
+
def delete_memory(username, key):
|
| 531 |
+
mem_col.delete_one({'username': username, 'key': key})
|
| 532 |
+
return jsonify({'ok': True}), 200
|
| 533 |
+
|
| 534 |
+
|
| 535 |
|
| 536 |
if __name__ == "__main__":
|
| 537 |
app.run(host="0.0.0.0", port=7860)
|