add logging to dataset
Browse files
app.py
CHANGED
|
@@ -1,18 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
with open("system_prompt.txt", "r") as f:
|
| 6 |
SYSTEM_PROMPT = f.read()
|
| 7 |
|
| 8 |
-
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
|
| 9 |
client = InferenceClient(MODEL_NAME)
|
|
|
|
| 10 |
|
| 11 |
-
# ---- Fixed parameters ----
|
| 12 |
MAX_TOKENS = 512
|
| 13 |
TEMPERATURE = 0.7
|
| 14 |
TOP_P = 0.95
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def respond(message, history):
|
| 17 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 18 |
|
|
@@ -37,10 +72,13 @@ def respond(message, history):
|
|
| 37 |
response += token
|
| 38 |
yield response
|
| 39 |
|
| 40 |
-
#
|
|
|
|
|
|
|
|
|
|
| 41 |
demo = gr.ChatInterface(
|
| 42 |
respond,
|
| 43 |
-
title="
|
| 44 |
)
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient, HfApi
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
import uuid
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
+
# ---- Configuration ----
|
| 9 |
+
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
|
| 10 |
+
DATASET_REPO = "frimelle/companion-chat-logs"
|
| 11 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 12 |
+
|
| 13 |
+
# ---- Load system prompt ----
|
| 14 |
with open("system_prompt.txt", "r") as f:
|
| 15 |
SYSTEM_PROMPT = f.read()
|
| 16 |
|
|
|
|
| 17 |
client = InferenceClient(MODEL_NAME)
|
| 18 |
+
api = HfApi()
|
| 19 |
|
| 20 |
+
# ---- Fixed generation parameters ----
|
| 21 |
MAX_TOKENS = 512
|
| 22 |
TEMPERATURE = 0.7
|
| 23 |
TOP_P = 0.95
|
| 24 |
|
| 25 |
+
# ---- Logging function ----
|
| 26 |
+
def log_to_dataset(user_message, assistant_message):
|
| 27 |
+
row = {
|
| 28 |
+
"timestamp": datetime.now().isoformat(),
|
| 29 |
+
"session_id": str(uuid.uuid4()),
|
| 30 |
+
"user": user_message,
|
| 31 |
+
"assistant": assistant_message,
|
| 32 |
+
"system_prompt": SYSTEM_PROMPT,
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
# Save as a JSONL file and upload to the dataset repo
|
| 36 |
+
filename = f"log_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:8]}.jsonl"
|
| 37 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 38 |
+
f.write(json.dumps(row) + "\n")
|
| 39 |
+
|
| 40 |
+
api.upload_file(
|
| 41 |
+
path_or_fileobj=filename,
|
| 42 |
+
path_in_repo=filename,
|
| 43 |
+
repo_id=DATASET_REPO,
|
| 44 |
+
repo_type="dataset",
|
| 45 |
+
token=HF_TOKEN
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
os.remove(filename)
|
| 49 |
+
|
| 50 |
+
# ---- Chatbot function ----
|
| 51 |
def respond(message, history):
|
| 52 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 53 |
|
|
|
|
| 72 |
response += token
|
| 73 |
yield response
|
| 74 |
|
| 75 |
+
# Log after full response is generated
|
| 76 |
+
log_to_dataset(message, response)
|
| 77 |
+
|
| 78 |
+
# ---- Gradio Interface ----
|
| 79 |
demo = gr.ChatInterface(
|
| 80 |
respond,
|
| 81 |
+
title="Zephyr Chatbot",
|
| 82 |
)
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|