Abigail45 commited on
Commit
afbc8a3
·
verified ·
1 Parent(s): ccdf647

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -16
app.py CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  import warnings
2
  warnings.filterwarnings("ignore", category=ResourceWarning)
3
  warnings.filterwarnings("ignore", category=RuntimeWarning)
@@ -33,16 +37,14 @@ def generate_chat(user_message, history, max_tokens, temperature, top_p, top_k):
33
  if history is None:
34
  history = []
35
 
36
- # Build prompt with conversation history
37
- prompt = "<|system|>You are Shay, a highly intelligent, unbiased, emotionless AI assistant.\n"
38
- for user_msg, bot_msg in history:
39
- prompt += f"<|user|>{user_msg}<|end|>\n<|assistant|>{bot_msg}<|end|>\n"
40
  prompt += f"<|user|>{user_message}<|end|>\n<|assistant|>"
41
 
42
- # Tokenize
43
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
44
 
45
- # Generate response
46
  output = model.generate(
47
  **inputs,
48
  max_new_tokens=int(max_tokens),
@@ -53,8 +55,7 @@ def generate_chat(user_message, history, max_tokens, temperature, top_p, top_k):
53
  do_sample=True
54
  )
55
 
56
- reply = tokenizer.decode(output[0], skip_special_tokens=True)
57
- reply = reply.split("<|assistant|>")[-1].strip()
58
  history.append((user_message, reply))
59
  return history
60
 
@@ -79,13 +80,13 @@ button { background-color: #333; color: #eee; border: 1px solid #555; }
79
  """
80
 
81
  # -----------------------------
82
- # Gradio Interface
83
  # -----------------------------
84
  with gr.Blocks() as app:
85
  gr.Markdown(
86
  "## Shay — Ultra Reliable AI Assistant\n"
87
  "Unbiased, emotionless, and able to converse on any topic.\n\n"
88
- "**Adjust generation parameters below.**"
89
  )
90
 
91
  chatbot = gr.Chatbot(height=600)
@@ -96,12 +97,12 @@ with gr.Blocks() as app:
96
  copy_all_btn = gr.Button("Copy Full History", variant="secondary")
97
 
98
  # Sliders for generation parameters
99
- max_tokens_slider = gr.Slider(minimum=32, maximum=1024, value=256, step=32, label="Max New Tokens")
100
- temperature_slider = gr.Slider(minimum=0.1, maximum=1.5, value=0.7, step=0.05, label="Temperature")
101
- top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.01, label="Top-p")
102
- top_k_slider = gr.Slider(minimum=1, maximum=200, value=50, step=1, label="Top-k")
103
 
104
- # Bind actions
105
  user_input.submit(
106
  generate_chat,
107
  [user_input, chatbot, max_tokens_slider, temperature_slider, top_p_slider, top_k_slider],
@@ -116,6 +117,10 @@ with gr.Blocks() as app:
116
  copy_last_btn.click(copy_last, chatbot, None)
117
  copy_all_btn.click(copy_all, chatbot, None)
118
 
119
- # Queue and launch
120
  app.queue(max_size=64)
 
 
 
 
121
  app.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False, css=DARK_CSS)
 
1
+ # =============================
2
+ # Shay Chatbot — Hugging Face Space
3
+ # =============================
4
+
5
  import warnings
6
  warnings.filterwarnings("ignore", category=ResourceWarning)
7
  warnings.filterwarnings("ignore", category=RuntimeWarning)
 
37
  if history is None:
38
  history = []
39
 
40
+ # Build prompt including full conversation
41
+ prompt = "<|system|>You are Shay, an intelligent, unbiased, emotionless AI assistant.\n"
42
+ for u, b in history:
43
+ prompt += f"<|user|>{u}<|end|>\n<|assistant|>{b}<|end|>\n"
44
  prompt += f"<|user|>{user_message}<|end|>\n<|assistant|>"
45
 
 
46
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
47
 
 
48
  output = model.generate(
49
  **inputs,
50
  max_new_tokens=int(max_tokens),
 
55
  do_sample=True
56
  )
57
 
58
+ reply = tokenizer.decode(output[0], skip_special_tokens=True).split("<|assistant|>")[-1].strip()
 
59
  history.append((user_message, reply))
60
  return history
61
 
 
80
  """
81
 
82
  # -----------------------------
83
+ # Gradio UI
84
  # -----------------------------
85
  with gr.Blocks() as app:
86
  gr.Markdown(
87
  "## Shay — Ultra Reliable AI Assistant\n"
88
  "Unbiased, emotionless, and able to converse on any topic.\n\n"
89
+ "**Adjust generation parameters below:**"
90
  )
91
 
92
  chatbot = gr.Chatbot(height=600)
 
97
  copy_all_btn = gr.Button("Copy Full History", variant="secondary")
98
 
99
  # Sliders for generation parameters
100
+ max_tokens_slider = gr.Slider(32, 1024, value=256, step=32, label="Max New Tokens")
101
+ temperature_slider = gr.Slider(0.1, 1.5, value=0.7, step=0.05, label="Temperature")
102
+ top_p_slider = gr.Slider(0.1, 1.0, value=0.9, step=0.01, label="Top-p")
103
+ top_k_slider = gr.Slider(1, 200, value=50, step=1, label="Top-k")
104
 
105
+ # Chat actions
106
  user_input.submit(
107
  generate_chat,
108
  [user_input, chatbot, max_tokens_slider, temperature_slider, top_p_slider, top_k_slider],
 
117
  copy_last_btn.click(copy_last, chatbot, None)
118
  copy_all_btn.click(copy_all, chatbot, None)
119
 
120
+ # Queue to handle multiple users efficiently
121
  app.queue(max_size=64)
122
+
123
+ # -----------------------------
124
+ # Launch App (Single Launch)
125
+ # -----------------------------
126
  app.launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False, css=DARK_CSS)