1ony commited on
Commit
268967c
·
verified ·
1 Parent(s): 29951af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -27
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import logging
 
3
  import gradio as gr
4
  from huggingface_hub import InferenceClient
5
 
@@ -13,6 +14,12 @@ SYSTEM_PROMPT = (
13
  )
14
 
15
 
 
 
 
 
 
 
16
  def chat(message, history):
17
  if not message or not message.strip():
18
  yield "Please enter a message."
@@ -24,38 +31,23 @@ def chat(message, history):
24
  yield "Configuration error: HF_TOKEN is missing from Space Secrets."
25
  return
26
 
27
- messages = [
28
- {"role": "system", "content": SYSTEM_PROMPT}
29
- ]
30
 
31
- # Preserve previous conversation turns.
32
  for item in history or []:
33
  if not isinstance(item, dict):
34
  continue
35
-
36
  role = item.get("role")
37
  content = item.get("content")
38
-
39
  if role in ("user", "assistant") and isinstance(content, str):
40
- messages.append({
41
- "role": role,
42
- "content": content,
43
- })
44
 
45
- messages.append({
46
- "role": "user",
47
- "content": message.strip(),
48
- })
49
 
50
  answer = ""
51
 
52
  try:
53
  logging.info("Starting inference request for %s", MODEL_ID)
54
-
55
- client = InferenceClient(
56
- model=MODEL_ID,
57
- token=token,
58
- )
59
 
60
  stream = client.chat.completions.create(
61
  messages=messages,
@@ -67,9 +59,7 @@ def chat(message, history):
67
  for chunk in stream:
68
  if not chunk.choices:
69
  continue
70
-
71
  content = chunk.choices[0].delta.content
72
-
73
  if isinstance(content, str) and content:
74
  answer += content
75
  yield answer
@@ -84,6 +74,7 @@ def chat(message, history):
84
 
85
  demo = gr.ChatInterface(
86
  fn=chat,
 
87
  title="🧠 Tony-Flash",
88
  description="Chat with DeepSeek V4 Flash",
89
  textbox=gr.Textbox(
@@ -93,9 +84,4 @@ demo = gr.ChatInterface(
93
  )
94
 
95
  if __name__ == "__main__":
96
- demo.queue().launch(
97
- server_name="0.0.0.0",
98
- server_port=7860,
99
- show_error=True,
100
- ssr_mode=False,
101
- )
 
1
  import os
2
  import logging
3
+ import spaces
4
  import gradio as gr
5
  from huggingface_hub import InferenceClient
6
 
 
14
  )
15
 
16
 
17
+ # Required by ZeroGPU: at least one @spaces.GPU function must exist at startup.
18
+ @spaces.GPU
19
+ def _gpu_placeholder():
20
+ return "ok"
21
+
22
+
23
  def chat(message, history):
24
  if not message or not message.strip():
25
  yield "Please enter a message."
 
31
  yield "Configuration error: HF_TOKEN is missing from Space Secrets."
32
  return
33
 
34
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
 
 
35
 
 
36
  for item in history or []:
37
  if not isinstance(item, dict):
38
  continue
 
39
  role = item.get("role")
40
  content = item.get("content")
 
41
  if role in ("user", "assistant") and isinstance(content, str):
42
+ messages.append({"role": role, "content": content})
 
 
 
43
 
44
+ messages.append({"role": "user", "content": message.strip()})
 
 
 
45
 
46
  answer = ""
47
 
48
  try:
49
  logging.info("Starting inference request for %s", MODEL_ID)
50
+ client = InferenceClient(model=MODEL_ID, token=token)
 
 
 
 
51
 
52
  stream = client.chat.completions.create(
53
  messages=messages,
 
59
  for chunk in stream:
60
  if not chunk.choices:
61
  continue
 
62
  content = chunk.choices[0].delta.content
 
63
  if isinstance(content, str) and content:
64
  answer += content
65
  yield answer
 
74
 
75
  demo = gr.ChatInterface(
76
  fn=chat,
77
+ type="messages",
78
  title="🧠 Tony-Flash",
79
  description="Chat with DeepSeek V4 Flash",
80
  textbox=gr.Textbox(
 
84
  )
85
 
86
  if __name__ == "__main__":
87
+ demo.queue().launch(show_error=True, ssr_mode=False)