Spaces:
Sleeping

mathanmithun commited on
Commit
4beb52c
·
verified ·
1 Parent(s): 91f51bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -17,30 +17,24 @@ SYSTEM_PROMPT = {
17
  }
18
 
19
  def build_prompt(history):
20
- """Convert message history into ChatML template format."""
21
  messages = [SYSTEM_PROMPT] + history
22
  return tokenizer.apply_chat_template(
23
  messages, tokenize=False, add_generation_prompt=True
24
  )
25
 
26
-
27
  def respond(message, history):
28
- """Handles user input and model response."""
29
-
30
- # Ensure proper format
31
  if history is None:
32
  history = []
33
 
34
- # Add user message in correct format
35
  history.append({"role": "user", "content": message})
36
 
37
- # Build model prompt
38
  prompt = build_prompt(history)
39
  inputs = tokenizer(prompt, return_tensors="pt")
40
 
41
- # Generate model output
42
  output = model.generate(
43
- **inputs,
44
  max_new_tokens=250,
45
  temperature=0.7,
46
  do_sample=True
@@ -48,23 +42,25 @@ def respond(message, history):
48
 
49
  reply = tokenizer.decode(output[0], skip_special_tokens=True)
50
 
51
- # Clean response
52
  if "Assistant:" in reply:
53
  reply = reply.split("Assistant:")[-1].strip()
54
 
55
- # Add assistant reply to context
56
  history.append({"role": "assistant", "content": reply})
57
 
58
- # Return history as required by Gradio
59
- return history, history
 
 
 
 
60
 
61
 
62
  def clear_chat():
63
  return [], []
64
 
65
 
66
- # ---------------- UI Layout ---------------- #
67
-
68
  with gr.Blocks() as demo:
69
  gr.Markdown("<h2 style='text-align:center;'>🤖 DhiGrowth AI Assistant</h2>")
70
 
 
17
  }
18
 
19
  def build_prompt(history):
 
20
  messages = [SYSTEM_PROMPT] + history
21
  return tokenizer.apply_chat_template(
22
  messages, tokenize=False, add_generation_prompt=True
23
  )
24
 
 
25
  def respond(message, history):
 
 
 
26
  if history is None:
27
  history = []
28
 
29
+ # Store user query
30
  history.append({"role": "user", "content": message})
31
 
32
+ # Build prompt with ChatML
33
  prompt = build_prompt(history)
34
  inputs = tokenizer(prompt, return_tensors="pt")
35
 
 
36
  output = model.generate(
37
+ **inputs,
38
  max_new_tokens=250,
39
  temperature=0.7,
40
  do_sample=True
 
42
 
43
  reply = tokenizer.decode(output[0], skip_special_tokens=True)
44
 
 
45
  if "Assistant:" in reply:
46
  reply = reply.split("Assistant:")[-1].strip()
47
 
48
+ # Store assistant response
49
  history.append({"role": "assistant", "content": reply})
50
 
51
+ # Only show user + assistant in UI.
52
+ ui_safe_messages = [
53
+ msg for msg in history if msg["role"] in ("user", "assistant")
54
+ ]
55
+
56
+ return ui_safe_messages, history
57
 
58
 
59
  def clear_chat():
60
  return [], []
61
 
62
 
63
+ # ---------------- UI ---------------- #
 
64
  with gr.Blocks() as demo:
65
  gr.Markdown("<h2 style='text-align:center;'>🤖 DhiGrowth AI Assistant</h2>")
66