PlusBIntelligenceLab commited on
Commit
4d2f6f6
Β·
verified Β·
1 Parent(s): ab3bf61

trying to fix deployment [v8]

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -14,14 +14,18 @@ import torch.nn.functional as F
14
  import gradio as gr
15
  from huggingface_hub import hf_hub_download
16
 
17
- # Monkey-patch Gradio's buggy schema parser
 
18
  import gradio_client.utils as _gcu
19
- _orig_get_type = _gcu.get_type
20
- def _patched_get_type(schema):
21
  if isinstance(schema, bool):
22
- return "bool"
23
- return _orig_get_type(schema)
24
- _gcu.get_type = _patched_get_type
 
 
 
25
 
26
  # ╔══════════════════════════════════════════════════════════════════════════╗
27
  # β•‘ Download Model from HuggingFace Hub β•‘
@@ -196,12 +200,14 @@ def respond(message, chat_history):
196
  if not message.strip():
197
  return "", chat_history
198
 
199
- # Build prompt
200
  ids = [BOS]
201
- for user_msg, bot_msg in chat_history:
202
- ids += [USER_START] + enc.encode_ordinary(user_msg) + [USER_END]
203
- if bot_msg:
204
- ids += [ASST_START] + enc.encode_ordinary(bot_msg) + [ASST_END]
 
 
205
  ids += [USER_START] + enc.encode_ordinary(message) + [USER_END]
206
  ids.append(ASST_START)
207
 
@@ -213,7 +219,8 @@ def respond(message, chat_history):
213
  generated.append(token)
214
 
215
  response = enc.decode(generated)
216
- chat_history.append((message, response))
 
217
  return "", chat_history
218
 
219
  with gr.Blocks(
@@ -222,7 +229,7 @@ with gr.Blocks(
222
  ) as demo:
223
  gr.Markdown("# 🌳 Treevik-1 500M Instruct\n**By PlusB Intelligence Lab** β€’ Created by Sayan Chowdhury")
224
 
225
- chatbot = gr.Chatbot(height=500, label="Chat with Treevik-1")
226
  msg = gr.Textbox(placeholder="Type a message and press Enter...", label="Message", lines=1)
227
  clear = gr.Button("πŸ—‘οΈ Clear Chat")
228
 
 
14
  import gradio as gr
15
  from huggingface_hub import hf_hub_download
16
 
17
+ # Monkey-patch Gradio's buggy schema parser β€”
18
+ # _json_schema_to_python_type crashes when additionalProperties is `True` (bool).
19
  import gradio_client.utils as _gcu
20
+ _orig_jstpt = _gcu._json_schema_to_python_type
21
+ def _safe_jstpt(schema, defs=None):
22
  if isinstance(schema, bool):
23
+ return "Any"
24
+ try:
25
+ return _orig_jstpt(schema, defs)
26
+ except _gcu.APIInfoParseError:
27
+ return "Any"
28
+ _gcu._json_schema_to_python_type = _safe_jstpt
29
 
30
  # ╔══════════════════════════════════════════════════════════════════════════╗
31
  # β•‘ Download Model from HuggingFace Hub β•‘
 
200
  if not message.strip():
201
  return "", chat_history
202
 
203
+ # Build prompt from messages-format history
204
  ids = [BOS]
205
+ for entry in chat_history:
206
+ role, content = entry["role"], entry["content"]
207
+ if role == "user":
208
+ ids += [USER_START] + enc.encode_ordinary(content) + [USER_END]
209
+ elif role == "assistant":
210
+ ids += [ASST_START] + enc.encode_ordinary(content) + [ASST_END]
211
  ids += [USER_START] + enc.encode_ordinary(message) + [USER_END]
212
  ids.append(ASST_START)
213
 
 
219
  generated.append(token)
220
 
221
  response = enc.decode(generated)
222
+ chat_history.append({"role": "user", "content": message})
223
+ chat_history.append({"role": "assistant", "content": response})
224
  return "", chat_history
225
 
226
  with gr.Blocks(
 
229
  ) as demo:
230
  gr.Markdown("# 🌳 Treevik-1 500M Instruct\n**By PlusB Intelligence Lab** β€’ Created by Sayan Chowdhury")
231
 
232
+ chatbot = gr.Chatbot(height=500, label="Chat with Treevik-1", type="messages")
233
  msg = gr.Textbox(placeholder="Type a message and press Enter...", label="Message", lines=1)
234
  clear = gr.Button("πŸ—‘οΈ Clear Chat")
235