SkyNetWalker commited on
Commit
e528476
·
verified ·
1 Parent(s): 3bf4da9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -15
app.py CHANGED
@@ -40,8 +40,8 @@ def respond(
40
  print(f"Max tokens: {max_tokens}, Temperature: {temperature}, Top-P: {top_p}")
41
  print(f"Selected model: {model_name}")
42
 
43
- # Prepare messages for the Hugging Face API
44
  messages = [{"role": "system", "content": system_message}]
 
45
  for val in history:
46
  if val[0]:
47
  messages.append({"role": "user", "content": val[0]})
@@ -51,21 +51,21 @@ def respond(
51
  print(f"Added assistant message to context: {val[1]}")
52
 
53
  messages.append({"role": "user", "content": message})
 
54
  response = ""
55
- print("Sending request to Hugging Face API.")
56
 
57
- # Stream response from Hugging Face API
58
- completion = client.chat.completions.create(
 
59
  model=model_name,
60
  messages=messages,
61
  max_tokens=max_tokens,
62
  temperature=temperature,
63
  top_p=top_p,
64
- stream=True,
65
- )
66
-
67
- for message in completion:
68
- token = message.delta.get("content", "")
69
  print(f"Received token: {token}")
70
  response += token
71
  yield response
@@ -73,16 +73,16 @@ def respond(
73
  print("Completed response generation.")
74
 
75
  models = [
76
- "meta-llama/Llama-3.2-3B-Instruct",
77
  "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",
78
  "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
79
  "deepseek-ai/DeepSeek-R1-Distill-Qwen-14B",
 
 
80
  "PowerInfer/SmallThinker-3B-Preview",
81
  "NovaSky-AI/Sky-T1-32B-Preview",
82
  "Qwen/QwQ-32B-Preview",
83
  "Qwen/Qwen2.5-Coder-32B-Instruct",
84
  "microsoft/Phi-3-mini-128k-instruct",
85
- "microsoft/phi-4"
86
  ]
87
 
88
  with gr.Blocks() as demo:
@@ -95,7 +95,6 @@ with gr.Blocks() as demo:
95
  label="Select Model:"
96
  )
97
 
98
- # Create the chat components separately
99
  chatbot = gr.Chatbot(height=500)
100
  msg = gr.Textbox(
101
  show_label=False,
@@ -104,7 +103,6 @@ with gr.Blocks() as demo:
104
  )
105
  clear = gr.Button("Clear")
106
 
107
- # Additional inputs
108
  with gr.Accordion("Configuration", open=False):
109
  preset_prompt = gr.Dropdown(
110
  choices=list(SYSTEM_PROMPTS.keys()),
@@ -138,7 +136,6 @@ with gr.Blocks() as demo:
138
  label="Top-P:"
139
  )
140
 
141
- # Set up the chat functionality
142
  def user(user_message, history):
143
  return "", history + [[user_message, None]]
144
 
@@ -182,4 +179,4 @@ print("Gradio interface initialized.")
182
 
183
  if __name__ == "__main__":
184
  print("Launching the demo application.")
185
- demo.launch()
 
40
  print(f"Max tokens: {max_tokens}, Temperature: {temperature}, Top-P: {top_p}")
41
  print(f"Selected model: {model_name}")
42
 
 
43
  messages = [{"role": "system", "content": system_message}]
44
+
45
  for val in history:
46
  if val[0]:
47
  messages.append({"role": "user", "content": val[0]})
 
51
  print(f"Added assistant message to context: {val[1]}")
52
 
53
  messages.append({"role": "user", "content": message})
54
+
55
  response = ""
 
56
 
57
+ print("Sending request to Hugging Face API.")
58
+
59
+ for chunk in client.chat.completions.create(
60
  model=model_name,
61
  messages=messages,
62
  max_tokens=max_tokens,
63
  temperature=temperature,
64
  top_p=top_p,
65
+ stream=True
66
+ ):
67
+ # Correctly access the delta content from Hugging Face's response format
68
+ token = chunk.choices[0].delta.content or ""
 
69
  print(f"Received token: {token}")
70
  response += token
71
  yield response
 
73
  print("Completed response generation.")
74
 
75
  models = [
 
76
  "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",
77
  "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
78
  "deepseek-ai/DeepSeek-R1-Distill-Qwen-14B",
79
+ "ngxson/MiniThinky-v2-1B-Llama-3.2",
80
+ "meta-llama/Llama-3.2-3B-Instruct",
81
  "PowerInfer/SmallThinker-3B-Preview",
82
  "NovaSky-AI/Sky-T1-32B-Preview",
83
  "Qwen/QwQ-32B-Preview",
84
  "Qwen/Qwen2.5-Coder-32B-Instruct",
85
  "microsoft/Phi-3-mini-128k-instruct",
 
86
  ]
87
 
88
  with gr.Blocks() as demo:
 
95
  label="Select Model:"
96
  )
97
 
 
98
  chatbot = gr.Chatbot(height=500)
99
  msg = gr.Textbox(
100
  show_label=False,
 
103
  )
104
  clear = gr.Button("Clear")
105
 
 
106
  with gr.Accordion("Configuration", open=False):
107
  preset_prompt = gr.Dropdown(
108
  choices=list(SYSTEM_PROMPTS.keys()),
 
136
  label="Top-P:"
137
  )
138
 
 
139
  def user(user_message, history):
140
  return "", history + [[user_message, None]]
141
 
 
179
 
180
  if __name__ == "__main__":
181
  print("Launching the demo application.")
182
+ demo.launch()