MarkChenX commited on
Commit
cfa4dea
·
verified ·
1 Parent(s): 1a0b8c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -52
app.py CHANGED
@@ -1,69 +1,107 @@
 
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
4
 
5
  def respond(
6
- message,
7
  history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- 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
16
- """
17
- client = InferenceClient(token=hf_token.token, model="MarkChenX/lfm2-quantum-128m-sft")
18
 
19
- messages = [{"role": "system", "content": system_message}]
 
 
20
 
21
- messages.extend(history)
 
 
 
 
 
 
 
22
 
23
- messages.append({"role": "user", "content": message})
 
 
 
24
 
25
  response = ""
26
 
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
- with gr.Blocks() as demo:
63
  with gr.Sidebar():
 
64
  gr.LoginButton()
65
- chatbot.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
 
68
  if __name__ == "__main__":
69
- demo.launch(share=True)
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from collections.abc import Generator
4
+
5
  import gradio as gr
6
  from huggingface_hub import InferenceClient
7
 
8
+ MODEL_ID = "MarkChenX/lfm2-quantum-128m-sft"
9
+
10
 
11
  def respond(
12
+ message: str,
13
  history: list[dict[str, str]],
14
+ system_message: str,
15
+ max_tokens: int,
16
+ temperature: float,
17
+ top_p: float,
18
+ hf_token: gr.OAuthToken | None,
19
+ ) -> Generator[str, None, None]:
20
+ """Generate and stream a response from the Hugging Face model."""
 
 
 
21
 
22
+ if hf_token is None:
23
+ yield "Please sign in with Hugging Face before sending a message."
24
+ return
25
 
26
+ messages = [
27
+ {
28
+ "role": "system",
29
+ "content": system_message.strip() or "You are a friendly chatbot.",
30
+ },
31
+ *history,
32
+ {"role": "user", "content": message},
33
+ ]
34
 
35
+ client = InferenceClient(
36
+ model=MODEL_ID,
37
+ token=hf_token.token,
38
+ )
39
 
40
  response = ""
41
 
42
+ try:
43
+ for chunk in client.chat_completion(
44
+ messages=messages,
45
+ max_tokens=int(max_tokens),
46
+ temperature=float(temperature),
47
+ top_p=float(top_p),
48
+ stream=True,
49
+ ):
50
+ if not chunk.choices:
51
+ continue
52
+
53
+ token = chunk.choices[0].delta.content or ""
54
+ response += token
55
+ yield response
56
+
57
+ except Exception as exc:
58
+ # Keep the existing partial response visible if generation fails.
59
+ error_message = f"\n\nGeneration failed: {exc}"
60
+ yield response + error_message
61
+
62
+
63
+ with gr.Blocks(title="LFM2 Quantum Chat") as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  with gr.Sidebar():
65
+ gr.Markdown("### Authentication")
66
  gr.LoginButton()
67
+
68
+ gr.ChatInterface(
69
+ fn=respond,
70
+ additional_inputs=[
71
+ gr.Textbox(
72
+ value="You are a friendly chatbot.",
73
+ label="System message",
74
+ lines=3,
75
+ ),
76
+ gr.Slider(
77
+ minimum=1,
78
+ maximum=2048,
79
+ value=512,
80
+ step=1,
81
+ label="Max new tokens",
82
+ ),
83
+ gr.Slider(
84
+ minimum=0.1,
85
+ maximum=4.0,
86
+ value=0.7,
87
+ step=0.1,
88
+ label="Temperature",
89
+ ),
90
+ gr.Slider(
91
+ minimum=0.1,
92
+ maximum=1.0,
93
+ value=0.95,
94
+ step=0.05,
95
+ label="Top-p",
96
+ ),
97
+ ],
98
+ )
99
 
100
 
101
  if __name__ == "__main__":
102
+ # Spaces already exposes the application publicly.
103
+ # Disabling SSR avoids launching the experimental Node.js SSR process.
104
+ demo.launch(
105
+ share=False,
106
+ ssr_mode=False,
107
+ )