Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,8 +6,27 @@ import gradio as gr
|
|
| 6 |
import spaces
|
| 7 |
import torch
|
| 8 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 9 |
-
import sqlite3
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
MAX_MAX_NEW_TOKENS = 2048
|
|
@@ -29,20 +48,6 @@ if torch.cuda.is_available():
|
|
| 29 |
tokenizer.use_default_system_prompt = False
|
| 30 |
|
| 31 |
|
| 32 |
-
_TABLE = """
|
| 33 |
-
CREATE TABLE IF NOT EXISTS Item(
|
| 34 |
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 35 |
-
question TEXT NOT NULL,
|
| 36 |
-
answer TEXT NOT NULL,
|
| 37 |
-
timestamp TIMESTAMP DEFAULT (DATETIME('now', 'localtime'))
|
| 38 |
-
);
|
| 39 |
-
"""
|
| 40 |
-
|
| 41 |
-
INSERT = """
|
| 42 |
-
INSERT INTO Item(question, answer) VALUES(?, ?);
|
| 43 |
-
"""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
connect = sqlite3.connect("history.db", check_same_thread=False)
|
| 47 |
cursor = connect.cursor()
|
| 48 |
cursor.execute(_TABLE)
|
|
@@ -93,9 +98,9 @@ def generate(
|
|
| 93 |
outputs.append(text)
|
| 94 |
yield "".join(outputs)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
|
| 101 |
chat_interface = gr.ChatInterface(
|
|
|
|
| 6 |
import spaces
|
| 7 |
import torch
|
| 8 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
|
|
|
| 9 |
|
| 10 |
+
from huggingface_hub import CommitScheduler
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
JSON_DATASET_DIR = Path("json_dataset")
|
| 14 |
+
JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
|
| 15 |
+
|
| 16 |
+
JSON_DATASET_PATH = JSON_DATASET_DIR / f"train-{uuid4()}.json"
|
| 17 |
+
|
| 18 |
+
scheduler = CommitScheduler(
|
| 19 |
+
repo_id="example-space-to-dataset-json",
|
| 20 |
+
repo_type="dataset",
|
| 21 |
+
folder_path=JSON_DATASET_DIR,
|
| 22 |
+
path_in_repo="data",
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
def save_json(role: str, content: str) -> None:
|
| 26 |
+
with scheduler.lock:
|
| 27 |
+
with JSON_DATASET_PATH.open("a") as f:
|
| 28 |
+
json.dump({"role": role, "content": content, "datetime": datetime.now().isoformat()}, f)
|
| 29 |
+
f.write("\n")
|
| 30 |
|
| 31 |
|
| 32 |
MAX_MAX_NEW_TOKENS = 2048
|
|
|
|
| 48 |
tokenizer.use_default_system_prompt = False
|
| 49 |
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
connect = sqlite3.connect("history.db", check_same_thread=False)
|
| 52 |
cursor = connect.cursor()
|
| 53 |
cursor.execute(_TABLE)
|
|
|
|
| 98 |
outputs.append(text)
|
| 99 |
yield "".join(outputs)
|
| 100 |
|
| 101 |
+
save_json("user", message)
|
| 102 |
+
save_json("assistant", "".join(outputs))
|
| 103 |
+
|
| 104 |
|
| 105 |
|
| 106 |
chat_interface = gr.ChatInterface(
|