DylanZimmer commited on
Commit
2207b59
·
1 Parent(s): dbf3c9a

Clean 700M

Browse files
Files changed (1) hide show
  1. app.py +18 -26
app.py CHANGED
@@ -1,31 +1,32 @@
 
1
  import torch
2
  from transformers import pipeline
3
 
4
- # Set up the text-generation pipeline
5
  model_name = "amusktweewt/tiny-model-700M-chat"
 
6
  chatbot = pipeline(
7
  "text-generation",
8
  model=model_name,
9
  device=0 if torch.cuda.is_available() else -1
10
  )
11
 
12
- # Ensure that bos_token and eos_token are explicitly set as strings
13
- chatbot.tokenizer.bos_token = "<sos>"
14
- chatbot.tokenizer.eos_token = "<|endoftext|>"
15
-
16
- system_prompt = "You are a highly intelligent and helpful AI assistant named Tiny Chat, developed by amusktweewt. Always refer to yourself like that. Your responses should be clear, concise, and accurate. Always prioritize user needs, provide well-structured answers, and maintain a friendly yet professional tone. Adapt to the user's preferences and communication style. When needed, ask clarifying questions to ensure the best response. Be honest about limitations and avoid making assumptions. Keep interactions engaging, informative, and efficient."})
 
 
 
17
 
18
- def chat_fxn_caller(message, history, system_prompt="", temperature=0.6, top_p=0.95, max_tokens=32768):
19
  messages = []
20
-
21
  if system_prompt.strip():
22
  messages.append({"role": "system", "content": system_prompt})
23
-
24
  for human_msg, assistant_msg in history:
25
  messages.append({"role": "user", "content": human_msg})
26
  if assistant_msg:
27
  messages.append({"role": "assistant", "content": assistant_msg})
28
-
29
  messages.append({"role": "user", "content": message})
30
 
31
  prompt = chatbot.tokenizer.apply_chat_template(messages, tokenize=False)
@@ -33,32 +34,23 @@ def chat_fxn_caller(message, history, system_prompt="", temperature=0.6, top_p=0
33
  response = chatbot(
34
  prompt,
35
  do_sample=True,
36
- max_new_tokens=512,
37
  top_k=50,
38
- temperature=0.6,
39
  num_return_sequences=1,
40
  repetition_penalty=1.1,
41
- pad_token_id=chatbot.tokenizer.eos_token_id,
42
- min_new_tokens=20
43
  )
44
 
45
- full_text = response[0]["generated_text"]
46
- response = full_text[len(demo = gr.ChatInterface(
47
- chat_fxn_caller,
48
- type="messages",
49
- additional_inputs=[
50
- gr.Textbox(prompt, label="System Prompt"),
51
- ],
52
- )
53
-
54
- demo.launch(share=True)prompt):].strip()
55
- return response
56
 
57
  demo = gr.ChatInterface(
58
  chat_fxn_caller,
59
  type="messages",
60
  additional_inputs=[
61
- gr.Textbox(system_prompt, label="System Prompt"),
62
  ],
63
  )
64
 
 
1
+ import gradio as gr
2
  import torch
3
  from transformers import pipeline
4
 
 
5
  model_name = "amusktweewt/tiny-model-700M-chat"
6
+
7
  chatbot = pipeline(
8
  "text-generation",
9
  model=model_name,
10
  device=0 if torch.cuda.is_available() else -1
11
  )
12
 
13
+ system_prompt_default = (
14
+ "You are a highly intelligent and helpful AI assistant named Tiny Chat, "
15
+ "developed by amusktweewt. Always refer to yourself like that. "
16
+ "Your responses should be clear, concise, and accurate. "
17
+ "Always prioritize user needs, provide well-structured answers, "
18
+ "and maintain a friendly yet professional tone. "
19
+ "Adapt to the user's preferences and communication style."
20
+ )
21
 
22
+ def chat_fxn_caller(message, history, system_prompt=system_prompt_default, temperature=0.6, top_p=0.95, max_tokens=512):
23
  messages = []
 
24
  if system_prompt.strip():
25
  messages.append({"role": "system", "content": system_prompt})
 
26
  for human_msg, assistant_msg in history:
27
  messages.append({"role": "user", "content": human_msg})
28
  if assistant_msg:
29
  messages.append({"role": "assistant", "content": assistant_msg})
 
30
  messages.append({"role": "user", "content": message})
31
 
32
  prompt = chatbot.tokenizer.apply_chat_template(messages, tokenize=False)
 
34
  response = chatbot(
35
  prompt,
36
  do_sample=True,
37
+ max_new_tokens=max_tokens,
38
  top_k=50,
39
+ temperature=temperature,
40
  num_return_sequences=1,
41
  repetition_penalty=1.1,
42
+ pad_token_id=chatbot.tokenizer.eos_token_id
 
43
  )
44
 
45
+ # Extract only the new text after the prompt
46
+ generated = response[0]["generated_text"][len(prompt):].strip()
47
+ return generated
 
 
 
 
 
 
 
 
48
 
49
  demo = gr.ChatInterface(
50
  chat_fxn_caller,
51
  type="messages",
52
  additional_inputs=[
53
+ gr.Textbox(system_prompt_default, label="System Prompt"),
54
  ],
55
  )
56