Splashdude commited on
Commit
9b08ee0
·
verified ·
1 Parent(s): 7f8a6b3

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +27 -8
app.py CHANGED
@@ -7,7 +7,8 @@ MODEL_ID = "Splashdude/smollm-chatbot"
7
  SYSTEM_PROMPT = (
8
  "You are a helpful, friendly AI assistant. "
9
  "You give clear, accurate, and conversational answers. "
10
- "Keep responses concise unless the user asks for detail."
 
11
  )
12
 
13
  model = None
@@ -26,14 +27,33 @@ def load_model():
26
  print("Model loaded!")
27
 
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  def build_messages(history, new_message):
30
  """Convert Gradio history to chat messages list."""
31
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
32
  for msg in history:
33
- if msg["role"] == "user":
34
- messages.append({"role": "user", "content": msg["content"]})
35
- elif msg["role"] == "assistant":
36
- messages.append({"role": "assistant", "content": msg["content"]})
37
  messages.append({"role": "user", "content": new_message})
38
  return messages
39
 
@@ -50,15 +70,14 @@ def generate_response(message, history):
50
  yield "Please type a message."
51
  return
52
 
53
- messages = build_messages(history, message)
54
-
55
  try:
 
56
  text = tokenizer.apply_chat_template(
57
  messages, tokenize=False, add_generation_prompt=True
58
  )
59
  inputs = tokenizer(text, return_tensors="pt")
60
  except Exception as e:
61
- yield f"Error: {e}"
62
  return
63
 
64
  streamer = TextIteratorStreamer(
 
7
  SYSTEM_PROMPT = (
8
  "You are a helpful, friendly AI assistant. "
9
  "You give clear, accurate, and conversational answers. "
10
+ "Keep responses concise unless the user asks for detail. "
11
+ "Remember what the user tells you and refer back to it when asked."
12
  )
13
 
14
  model = None
 
27
  print("Model loaded!")
28
 
29
 
30
+ def extract_text(content):
31
+ """Extract plain text from Gradio content (handles str, list, dict)."""
32
+ if isinstance(content, str):
33
+ return content
34
+ if isinstance(content, list):
35
+ parts = []
36
+ for item in content:
37
+ if isinstance(item, str):
38
+ parts.append(item)
39
+ elif isinstance(item, dict):
40
+ parts.append(item.get("text", item.get("content", str(item))))
41
+ else:
42
+ parts.append(str(item))
43
+ return " ".join(parts)
44
+ if isinstance(content, dict):
45
+ return content.get("text", content.get("content", str(content)))
46
+ return str(content)
47
+
48
+
49
  def build_messages(history, new_message):
50
  """Convert Gradio history to chat messages list."""
51
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
52
  for msg in history:
53
+ role = msg.get("role", "")
54
+ content = extract_text(msg.get("content", ""))
55
+ if role in ("user", "assistant") and content:
56
+ messages.append({"role": role, "content": content})
57
  messages.append({"role": "user", "content": new_message})
58
  return messages
59
 
 
70
  yield "Please type a message."
71
  return
72
 
 
 
73
  try:
74
+ messages = build_messages(history, message)
75
  text = tokenizer.apply_chat_template(
76
  messages, tokenize=False, add_generation_prompt=True
77
  )
78
  inputs = tokenizer(text, return_tensors="pt")
79
  except Exception as e:
80
+ yield f"Error building prompt: {e}"
81
  return
82
 
83
  streamer = TextIteratorStreamer(