Spaces:
Sleeping
Sleeping
Sahil commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,13 +2,13 @@ from flask import Flask, request, jsonify, send_file
|
|
| 2 |
from flask_cors import CORS
|
| 3 |
from datasets import Dataset
|
| 4 |
from huggingface_hub import HfApi
|
| 5 |
-
import
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
CORS(app)
|
| 9 |
|
| 10 |
-
DATASET_NAME = "
|
| 11 |
-
MAX_ENTRIES = 50
|
| 12 |
|
| 13 |
@app.route("/")
|
| 14 |
def index():
|
|
@@ -19,37 +19,33 @@ def save():
|
|
| 19 |
data = request.get_json()
|
| 20 |
query = data.get("prompt")
|
| 21 |
response = data.get("response")
|
| 22 |
-
summary = data.get("summary", None)
|
| 23 |
|
| 24 |
if not query or not response:
|
| 25 |
-
return jsonify({"error": "Missing
|
| 26 |
|
| 27 |
os.makedirs("data", exist_ok=True)
|
| 28 |
path = "data/memory.json"
|
| 29 |
if not os.path.exists(path):
|
| 30 |
-
with open(path, "w") as f:
|
| 31 |
-
json.dump([], f)
|
| 32 |
|
| 33 |
with open(path, "r+") as f:
|
| 34 |
memory = json.load(f)
|
| 35 |
|
| 36 |
-
# add new entry as fresh memory
|
| 37 |
memory.append({
|
| 38 |
"query": query,
|
| 39 |
"response": response,
|
| 40 |
-
"summary":
|
| 41 |
"archived": False,
|
| 42 |
"level": 1
|
| 43 |
})
|
| 44 |
|
| 45 |
-
#
|
| 46 |
if len(memory) > MAX_ENTRIES:
|
| 47 |
archive_old(memory)
|
| 48 |
|
| 49 |
f.seek(0)
|
| 50 |
json.dump(memory, f, indent=2)
|
| 51 |
|
| 52 |
-
# push to hub
|
| 53 |
hf_token = os.environ.get("HF_TOKEN")
|
| 54 |
if hf_token:
|
| 55 |
try:
|
|
@@ -67,12 +63,10 @@ def save():
|
|
| 67 |
|
| 68 |
|
| 69 |
def summarize(q, r):
|
| 70 |
-
"
|
| 71 |
-
|
| 72 |
-
return f"{q[:30]} → {short}..."
|
| 73 |
|
| 74 |
def archive_old(memory):
|
| 75 |
-
"""compress older entries when exceeding limit"""
|
| 76 |
fresh = [m for m in memory if m["level"] == 1]
|
| 77 |
if len(fresh) > MAX_ENTRIES:
|
| 78 |
for m in fresh[:-MAX_ENTRIES]:
|
|
|
|
| 2 |
from flask_cors import CORS
|
| 3 |
from datasets import Dataset
|
| 4 |
from huggingface_hub import HfApi
|
| 5 |
+
import os, json
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
CORS(app)
|
| 9 |
|
| 10 |
+
DATASET_NAME = "sahil5112/ContinuumGPT-Dataset" # your dataset name
|
| 11 |
+
MAX_ENTRIES = 50
|
| 12 |
|
| 13 |
@app.route("/")
|
| 14 |
def index():
|
|
|
|
| 19 |
data = request.get_json()
|
| 20 |
query = data.get("prompt")
|
| 21 |
response = data.get("response")
|
|
|
|
| 22 |
|
| 23 |
if not query or not response:
|
| 24 |
+
return jsonify({"error": "Missing data"}), 400
|
| 25 |
|
| 26 |
os.makedirs("data", exist_ok=True)
|
| 27 |
path = "data/memory.json"
|
| 28 |
if not os.path.exists(path):
|
| 29 |
+
with open(path, "w") as f: json.dump([], f)
|
|
|
|
| 30 |
|
| 31 |
with open(path, "r+") as f:
|
| 32 |
memory = json.load(f)
|
| 33 |
|
|
|
|
| 34 |
memory.append({
|
| 35 |
"query": query,
|
| 36 |
"response": response,
|
| 37 |
+
"summary": summarize(query, response),
|
| 38 |
"archived": False,
|
| 39 |
"level": 1
|
| 40 |
})
|
| 41 |
|
| 42 |
+
# archive older ones if exceeding limit
|
| 43 |
if len(memory) > MAX_ENTRIES:
|
| 44 |
archive_old(memory)
|
| 45 |
|
| 46 |
f.seek(0)
|
| 47 |
json.dump(memory, f, indent=2)
|
| 48 |
|
|
|
|
| 49 |
hf_token = os.environ.get("HF_TOKEN")
|
| 50 |
if hf_token:
|
| 51 |
try:
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
def summarize(q, r):
|
| 66 |
+
return f"{q[:40]} → {r[:100]}..."
|
| 67 |
+
|
|
|
|
| 68 |
|
| 69 |
def archive_old(memory):
|
|
|
|
| 70 |
fresh = [m for m in memory if m["level"] == 1]
|
| 71 |
if len(fresh) > MAX_ENTRIES:
|
| 72 |
for m in fresh[:-MAX_ENTRIES]:
|