write into one file
Browse files
app.py
CHANGED
@@ -17,36 +17,32 @@ with open("system_prompt.txt", "r") as f:
|
|
17 |
client = InferenceClient(MODEL_NAME)
|
18 |
api = HfApi()
|
19 |
|
20 |
-
# ----
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
|
25 |
-
# ---- Logging
|
26 |
-
def
|
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=
|
42 |
-
path_in_repo=
|
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}]
|
@@ -62,18 +58,18 @@ def respond(message, history):
|
|
62 |
|
63 |
for chunk in client.chat_completion(
|
64 |
messages,
|
65 |
-
max_tokens=
|
66 |
stream=True,
|
67 |
-
temperature=
|
68 |
-
top_p=
|
69 |
):
|
70 |
token = chunk.choices[0].delta.content
|
71 |
if token:
|
72 |
response += token
|
73 |
yield response
|
74 |
|
75 |
-
#
|
76 |
-
|
77 |
|
78 |
# ---- Gradio Interface ----
|
79 |
demo = gr.ChatInterface(
|
|
|
17 |
client = InferenceClient(MODEL_NAME)
|
18 |
api = HfApi()
|
19 |
|
20 |
+
# ---- Session ID and local file setup ----
|
21 |
+
SESSION_ID = str(uuid.uuid4())
|
22 |
+
LOCAL_LOG_PATH = f"chatlog_{SESSION_ID}.jsonl"
|
23 |
+
REMOTE_LOG_PATH = f"sessions/{SESSION_ID}.jsonl" # stored in a folder in the dataset
|
24 |
|
25 |
+
# ---- Logging per session ----
|
26 |
+
def append_to_session_log(user_message, assistant_message):
|
27 |
row = {
|
28 |
"timestamp": datetime.now().isoformat(),
|
|
|
29 |
"user": user_message,
|
30 |
"assistant": assistant_message,
|
31 |
"system_prompt": SYSTEM_PROMPT,
|
32 |
+
"session_id": SESSION_ID
|
33 |
}
|
34 |
+
with open(LOCAL_LOG_PATH, "a", encoding="utf-8") as f:
|
|
|
|
|
|
|
35 |
f.write(json.dumps(row) + "\n")
|
36 |
|
37 |
+
# Push to HF dataset
|
38 |
api.upload_file(
|
39 |
+
path_or_fileobj=LOCAL_LOG_PATH,
|
40 |
+
path_in_repo=REMOTE_LOG_PATH,
|
41 |
repo_id=DATASET_REPO,
|
42 |
repo_type="dataset",
|
43 |
token=HF_TOKEN
|
44 |
)
|
45 |
|
|
|
|
|
46 |
# ---- Chatbot function ----
|
47 |
def respond(message, history):
|
48 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
|
58 |
|
59 |
for chunk in client.chat_completion(
|
60 |
messages,
|
61 |
+
max_tokens=512,
|
62 |
stream=True,
|
63 |
+
temperature=0.7,
|
64 |
+
top_p=0.95,
|
65 |
):
|
66 |
token = chunk.choices[0].delta.content
|
67 |
if token:
|
68 |
response += token
|
69 |
yield response
|
70 |
|
71 |
+
# Save after each message pair
|
72 |
+
append_to_session_log(message, response)
|
73 |
|
74 |
# ---- Gradio Interface ----
|
75 |
demo = gr.ChatInterface(
|