frimelle HF Staff commited on
Commit
474c39f
·
1 Parent(s): b6dc8c1

another reset

Browse files
Files changed (1) hide show
  1. app.py +24 -34
app.py CHANGED
@@ -1,33 +1,17 @@
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
 
13
- DATASET_REPO = "frimelle/companion-chat-logs"
14
- HF_TOKEN = os.environ.get("HF_TOKEN") # set in Space secrets
15
-
16
  client = InferenceClient(MODEL_NAME)
17
 
18
- # ---- Setup logging ----
19
- LOG_DIR = "chat_logs"
20
- os.makedirs(LOG_DIR, exist_ok=True)
21
- session_id = str(uuid.uuid4())
22
-
23
- def log_chat(session_id, user_msg, bot_msg):
24
- timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
25
- log_path = os.path.join(LOG_DIR, f"{session_id}.txt")
26
- with open(log_path, "a", encoding="utf-8") as f:
27
- f.write(f"[{timestamp}] User: {user_msg}\n")
28
- f.write(f"[{timestamp}] Bot: {bot_msg}\n\n")
29
 
30
- # ---- Respond Function with Logging ----
31
  def respond(
32
  message,
33
  history: list[tuple[str, str]],
@@ -48,32 +32,38 @@ def respond(
48
 
49
  response = ""
50
 
51
- for chunk in client.chat_completion(
52
  messages,
53
  max_tokens=max_tokens,
54
  stream=True,
55
  temperature=temperature,
56
  top_p=top_p,
57
  ):
58
- token = chunk.choices[0].delta.content
59
- if token:
60
- response += token
61
- yield response
62
 
63
- # Save full message after stream ends
64
- log_chat(session_id, message, response)
65
 
66
- # ---- Gradio Interface ----
 
 
 
67
  demo = gr.ChatInterface(
68
  respond,
69
- #additional_inputs=[
70
- # gr.Textbox(value=SYSTEM_PROMPT, label="System message"),
71
- # gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
72
- # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
73
- # gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
74
- #],
75
- title="BoundrAI"
 
 
 
 
 
76
  )
77
 
 
78
  if __name__ == "__main__":
79
  demo.launch()
 
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
 
33
  response = ""
34
 
35
+ for message in client.chat_completion(
36
  messages,
37
  max_tokens=max_tokens,
38
  stream=True,
39
  temperature=temperature,
40
  top_p=top_p,
41
  ):
42
+ token = message.choices[0].delta.content
 
 
 
43
 
44
+ response += token
45
+ yield response
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()