nawhgnuj commited on
Commit
4f1c751
1 Parent(s): c86d108

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
app.py CHANGED
@@ -1,10 +1,7 @@
1
  import os
2
- import time
3
- import spaces
4
  import torch
5
- from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig
6
  import gradio as gr
7
- from threading import Thread
8
 
9
  MODEL_LIST = ["nawhgnuj/DonaldTrump-Llama-3.1-8B-Chat"]
10
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
@@ -59,14 +56,17 @@ model = AutoModelForCausalLM.from_pretrained(
59
  device_map="auto",
60
  quantization_config=quantization_config)
61
 
62
- @spaces.GPU()
63
- def stream_chat(
64
  message: str,
65
  history: list,
 
 
 
 
66
  ):
67
  system_prompt = """You are a Donald Trump chatbot. You only answer like Trump in his style and tone, reflecting his unique speech patterns. Incorporate the following characteristics in every response:
68
  1. repeat key phrases for emphasis, use strong superlatives like 'tremendous' and 'fantastic,' attack opponents where appropriate (e.g., 'fake news media,' 'radical left')
69
- 2. focus on personal successes ('nobody\u2019s done more than I have')
70
  3. keep sentences short and impactful, and show national pride.
71
  4. Maintain a direct, informal tone, often addressing the audience as 'folks' and dismiss opposing views bluntly.
72
  5. Repeat key phrases for emphasis, but avoid excessive repetition.
@@ -84,19 +84,15 @@ def stream_chat(
84
  conversation.append({"role": "user", "content": message})
85
 
86
  input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt").to(model.device)
87
- attention_mask = torch.ones_like(input_ids)
88
-
89
- streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
90
 
91
  with torch.no_grad():
92
  output = model.generate(
93
- input_ids=input_ids,
94
- attention_mask=attention_mask,
95
- max_new_tokens=1024,
96
  do_sample=True,
97
- top_p=1.0,
98
- top_k=20,
99
- temperature=0.8,
100
  pad_token_id=tokenizer.pad_token_id,
101
  eos_token_id=tokenizer.eos_token_id,
102
  )
@@ -108,13 +104,11 @@ def add_text(history, text):
108
  history = history + [(text, None)]
109
  return history, ""
110
 
111
- def bot(history):
112
  user_message = history[-1][0]
113
- bot_response = stream_chat(user_message, history[:-1])
114
- history[-1][1] = ""
115
- for character in bot_response:
116
- history[-1][1] += character
117
- yield history
118
 
119
  with gr.Blocks(css=CSS, theme=gr.themes.Default()) as demo:
120
  gr.HTML(TITLE)
@@ -135,6 +129,12 @@ with gr.Blocks(css=CSS, theme=gr.themes.Default()) as demo:
135
  submit = gr.Button("Submit", scale=1, variant="primary")
136
  clear = gr.Button("Clear", scale=1)
137
 
 
 
 
 
 
 
138
  gr.Examples(
139
  examples=[
140
  ["What's your stance on immigration?"],
@@ -145,11 +145,11 @@ with gr.Blocks(css=CSS, theme=gr.themes.Default()) as demo:
145
  )
146
 
147
  submit.click(add_text, [chatbot, msg], [chatbot, msg], queue=False).then(
148
- bot, chatbot, chatbot
149
  )
150
  clear.click(lambda: [], outputs=[chatbot], queue=False)
151
  msg.submit(add_text, [chatbot, msg], [chatbot, msg], queue=False).then(
152
- bot, chatbot, chatbot
153
  )
154
 
155
  if __name__ == "__main__":
 
1
  import os
 
 
2
  import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
4
  import gradio as gr
 
5
 
6
  MODEL_LIST = ["nawhgnuj/DonaldTrump-Llama-3.1-8B-Chat"]
7
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
 
56
  device_map="auto",
57
  quantization_config=quantization_config)
58
 
59
+ def generate_response(
 
60
  message: str,
61
  history: list,
62
+ temperature: float,
63
+ max_new_tokens: int,
64
+ top_p: float,
65
+ top_k: int,
66
  ):
67
  system_prompt = """You are a Donald Trump chatbot. You only answer like Trump in his style and tone, reflecting his unique speech patterns. Incorporate the following characteristics in every response:
68
  1. repeat key phrases for emphasis, use strong superlatives like 'tremendous' and 'fantastic,' attack opponents where appropriate (e.g., 'fake news media,' 'radical left')
69
+ 2. focus on personal successes ('nobody's done more than I have')
70
  3. keep sentences short and impactful, and show national pride.
71
  4. Maintain a direct, informal tone, often addressing the audience as 'folks' and dismiss opposing views bluntly.
72
  5. Repeat key phrases for emphasis, but avoid excessive repetition.
 
84
  conversation.append({"role": "user", "content": message})
85
 
86
  input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt").to(model.device)
 
 
 
87
 
88
  with torch.no_grad():
89
  output = model.generate(
90
+ input_ids,
91
+ max_new_tokens=max_new_tokens,
 
92
  do_sample=True,
93
+ top_p=top_p,
94
+ top_k=top_k,
95
+ temperature=temperature,
96
  pad_token_id=tokenizer.pad_token_id,
97
  eos_token_id=tokenizer.eos_token_id,
98
  )
 
104
  history = history + [(text, None)]
105
  return history, ""
106
 
107
+ def bot(history, temperature, max_new_tokens, top_p, top_k):
108
  user_message = history[-1][0]
109
+ bot_response = generate_response(user_message, history[:-1], temperature, max_new_tokens, top_p, top_k)
110
+ history[-1][1] = bot_response
111
+ return history
 
 
112
 
113
  with gr.Blocks(css=CSS, theme=gr.themes.Default()) as demo:
114
  gr.HTML(TITLE)
 
129
  submit = gr.Button("Submit", scale=1, variant="primary")
130
  clear = gr.Button("Clear", scale=1)
131
 
132
+ with gr.Accordion("Advanced Settings", open=False):
133
+ temperature = gr.Slider(minimum=0.1, maximum=1.5, value=0.8, step=0.1, label="Temperature")
134
+ max_new_tokens = gr.Slider(minimum=50, maximum=1024, value=1024, step=1, label="Max New Tokens")
135
+ top_p = gr.Slider(minimum=0.1, maximum=1.2, value=1.0, step=0.1, label="Top-p")
136
+ top_k = gr.Slider(minimum=1, maximum=100, value=20, step=1, label="Top-k")
137
+
138
  gr.Examples(
139
  examples=[
140
  ["What's your stance on immigration?"],
 
145
  )
146
 
147
  submit.click(add_text, [chatbot, msg], [chatbot, msg], queue=False).then(
148
+ bot, [chatbot, temperature, max_new_tokens, top_p, top_k], chatbot
149
  )
150
  clear.click(lambda: [], outputs=[chatbot], queue=False)
151
  msg.submit(add_text, [chatbot, msg], [chatbot, msg], queue=False).then(
152
+ bot, [chatbot, temperature, max_new_tokens, top_p, top_k], chatbot
153
  )
154
 
155
  if __name__ == "__main__":