add logging
Browse files
app.py
CHANGED
@@ -1,17 +1,29 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
3 |
|
|
|
4 |
with open("system_prompt.txt", "r") as f:
|
5 |
SYSTEM_PROMPT = f.read()
|
6 |
|
7 |
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
|
8 |
-
|
9 |
-
"""
|
10 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
11 |
-
"""
|
12 |
client = InferenceClient(MODEL_NAME)
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
|
|
15 |
def respond(
|
16 |
message,
|
17 |
history: list[tuple[str, str]],
|
@@ -32,38 +44,32 @@ def respond(
|
|
32 |
|
33 |
response = ""
|
34 |
|
35 |
-
for
|
36 |
messages,
|
37 |
max_tokens=max_tokens,
|
38 |
stream=True,
|
39 |
temperature=temperature,
|
40 |
top_p=top_p,
|
41 |
):
|
42 |
-
token =
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
|
47 |
-
|
48 |
-
"""
|
49 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
50 |
-
"""
|
51 |
demo = gr.ChatInterface(
|
52 |
respond,
|
53 |
additional_inputs=[
|
54 |
gr.Textbox(value=SYSTEM_PROMPT, label="System message"),
|
55 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
56 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
57 |
-
gr.Slider(
|
58 |
-
minimum=0.1,
|
59 |
-
maximum=1.0,
|
60 |
-
value=0.95,
|
61 |
-
step=0.05,
|
62 |
-
label="Top-p (nucleus sampling)",
|
63 |
-
),
|
64 |
],
|
|
|
65 |
)
|
66 |
|
67 |
-
|
68 |
if __name__ == "__main__":
|
69 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
from datetime import datetime
|
4 |
+
import os
|
5 |
+
import uuid
|
6 |
|
7 |
+
# ---- System Prompt ----
|
8 |
with open("system_prompt.txt", "r") as f:
|
9 |
SYSTEM_PROMPT = f.read()
|
10 |
|
11 |
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta"
|
|
|
|
|
|
|
|
|
12 |
client = InferenceClient(MODEL_NAME)
|
13 |
|
14 |
+
# ---- Setup logging ----
|
15 |
+
LOG_DIR = "chat_logs"
|
16 |
+
os.makedirs(LOG_DIR, exist_ok=True)
|
17 |
+
session_id = str(uuid.uuid4())
|
18 |
+
|
19 |
+
def log_chat(session_id, user_msg, bot_msg):
|
20 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
21 |
+
log_path = os.path.join(LOG_DIR, f"{session_id}.txt")
|
22 |
+
with open(log_path, "a", encoding="utf-8") as f:
|
23 |
+
f.write(f"[{timestamp}] User: {user_msg}\n")
|
24 |
+
f.write(f"[{timestamp}] Bot: {bot_msg}\n\n")
|
25 |
|
26 |
+
# ---- Respond Function with Logging ----
|
27 |
def respond(
|
28 |
message,
|
29 |
history: list[tuple[str, str]],
|
|
|
44 |
|
45 |
response = ""
|
46 |
|
47 |
+
for chunk in client.chat_completion(
|
48 |
messages,
|
49 |
max_tokens=max_tokens,
|
50 |
stream=True,
|
51 |
temperature=temperature,
|
52 |
top_p=top_p,
|
53 |
):
|
54 |
+
token = chunk.choices[0].delta.content
|
55 |
+
if token:
|
56 |
+
response += token
|
57 |
+
yield response
|
58 |
|
59 |
+
# Save full message after stream ends
|
60 |
+
log_chat(session_id, message, response)
|
61 |
|
62 |
+
# ---- Gradio Interface ----
|
|
|
|
|
|
|
63 |
demo = gr.ChatInterface(
|
64 |
respond,
|
65 |
additional_inputs=[
|
66 |
gr.Textbox(value=SYSTEM_PROMPT, label="System message"),
|
67 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
68 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
69 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
],
|
71 |
+
title="BoundrAI"
|
72 |
)
|
73 |
|
|
|
74 |
if __name__ == "__main__":
|
75 |
+
demo.launch()
|