Asilbek14 commited on
Commit
0a2169b
·
verified ·
1 Parent(s): 5f4efa7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -11
app.py CHANGED
@@ -17,14 +17,18 @@ client = InferenceClient(MODEL_REPO)
17
 
18
  # ---------------- CHAT FUNCTION ----------------
19
  def stream_response(message, chat_history, system_message, max_tokens, temperature, top_p, response_style):
20
- if response_style == "concise":
21
  system_message += " Keep answers short and direct."
22
- elif response_style == "detailed":
23
  system_message += " Provide more explanation and context when helpful."
24
- elif response_style == "essay":
25
  system_message += " Write long, structured, essay-style responses."
26
 
27
  messages = [{"role": "system", "content": system_message}]
 
 
 
 
28
 
29
  response = ""
30
  for msg in client.chat_completion(
@@ -50,8 +54,8 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet", secondary_hue="pink"))
50
  )
51
 
52
  chatbot = gr.Chatbot(
 
53
  height=500,
54
- bubble_full_width=False,
55
  show_copy_button=True,
56
  label="Chat"
57
  )
@@ -71,22 +75,27 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet", secondary_hue="pink"))
71
  value=SYSTEM_PROMPT_DEFAULT,
72
  lines=3
73
  )
 
 
 
 
 
74
  temperature = gr.Slider(0.1, 1.5, value=TEMP_DEFAULT, step=0.1, label="Temperature")
75
  top_p = gr.Slider(0.1, 1.0, value=TOP_P_DEFAULT, step=0.05, label="Top-p")
76
  max_tokens = gr.Slider(32, 2048, value=MAX_NEW_TOKENS_DEFAULT, step=16, label="Max new tokens")
77
 
78
  # Events (streaming response)
79
  send_btn.click(
80
- stream_response,
81
- [msg, chatbot, system_prompt, max_tokens, temperature, top_p, response_style],
82
- [msg, chatbot]
83
  )
84
  msg.submit(
85
- stream_response,
86
- [msg, chatbot, system_prompt, max_tokens, temperature, top_p, response_style],
87
- [msg, chatbot]
88
  )
89
  clear_btn.click(lambda: None, None, chatbot, queue=False)
90
 
91
  if __name__ == "__main__":
92
- demo.launch()
 
17
 
18
  # ---------------- CHAT FUNCTION ----------------
19
  def stream_response(message, chat_history, system_message, max_tokens, temperature, top_p, response_style):
20
+ if response_style == "Concise":
21
  system_message += " Keep answers short and direct."
22
+ elif response_style == "Detailed":
23
  system_message += " Provide more explanation and context when helpful."
24
+ elif response_style == "Essay":
25
  system_message += " Write long, structured, essay-style responses."
26
 
27
  messages = [{"role": "system", "content": system_message}]
28
+ for user, bot in chat_history:
29
+ messages.append({"role": "user", "content": user})
30
+ messages.append({"role": "assistant", "content": bot})
31
+ messages.append({"role": "user", "content": message})
32
 
33
  response = ""
34
  for msg in client.chat_completion(
 
54
  )
55
 
56
  chatbot = gr.Chatbot(
57
+ type="messages", # ✅ new format
58
  height=500,
 
59
  show_copy_button=True,
60
  label="Chat"
61
  )
 
75
  value=SYSTEM_PROMPT_DEFAULT,
76
  lines=3
77
  )
78
+ response_style = gr.Dropdown(
79
+ ["Concise", "Detailed", "Essay"],
80
+ value="Concise",
81
+ label="Response Style"
82
+ )
83
  temperature = gr.Slider(0.1, 1.5, value=TEMP_DEFAULT, step=0.1, label="Temperature")
84
  top_p = gr.Slider(0.1, 1.0, value=TOP_P_DEFAULT, step=0.05, label="Top-p")
85
  max_tokens = gr.Slider(32, 2048, value=MAX_NEW_TOKENS_DEFAULT, step=16, label="Max new tokens")
86
 
87
  # Events (streaming response)
88
  send_btn.click(
89
+ stream_response,
90
+ [msg, chatbot, system_prompt, max_tokens, temperature, top_p, response_style],
91
+ [msg, chatbot]
92
  )
93
  msg.submit(
94
+ stream_response,
95
+ [msg, chatbot, system_prompt, max_tokens, temperature, top_p, response_style],
96
+ [msg, chatbot]
97
  )
98
  clear_btn.click(lambda: None, None, chatbot, queue=False)
99
 
100
  if __name__ == "__main__":
101
+ demo.launch()