jakewatson commited on
Commit
922b8c2
·
1 Parent(s): 67d487d

max_tokens

Browse files
Files changed (2) hide show
  1. app.py +25 -17
  2. tests.py +24 -15
app.py CHANGED
@@ -2,11 +2,15 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import torch
4
  from transformers import pipeline
5
- import random
6
 
7
  # Inference client setup
8
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
9
- pipe = pipeline("text-generation", "microsoft/Phi-3-mini-4k-instruct", torch_dtype=torch.bfloat16, device_map="auto")
 
 
 
 
 
10
 
11
  # Global flag to handle cancellation
12
  stop_inference = False
@@ -20,20 +24,23 @@ def respond(
20
  history: list[tuple[str, str]],
21
  system_message_val,
22
  temperature=0.7,
23
- practicality=None,
24
- use_local_model=False,
25
  max_tokens=256,
 
26
  ):
27
  global stop_inference
28
  stop_inference = False # Reset cancellation flag
29
 
30
- if practicality is None:
31
- practicality = round(random.uniform(0,1), 1) # Initialize random practicality score
32
- if practicality > 0.5:
33
- append_message = "Provide actionable advice or direct instructions."
34
- else:
35
- append_message = "Provide theoretical concepts or abstract quotes."
36
- system_message_val = f"{base_message} {append_message}"
 
 
 
37
 
38
  # Initialize history if it's None
39
  if history is None:
@@ -53,7 +60,7 @@ def respond(
53
  output = pipe(
54
  input_text,
55
  temperature=temperature,
56
- max_new_tokens=max_tokens,
57
  do_sample=True,
58
  num_return_sequences=1,
59
  )
@@ -79,7 +86,7 @@ def respond(
79
  messages=messages,
80
  stream=True,
81
  temperature=temperature,
82
- max_tokens=max_tokens
83
  ):
84
  if stop_inference:
85
  response = "Inference cancelled."
@@ -157,7 +164,7 @@ with gr.Blocks(css=custom_css) as demo:
157
  )
158
  use_local_model = gr.Checkbox(label="Use Local Model", value=False)
159
  temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
160
- practicality = gr.Slider(minimum=0.1, maximum=1.0, value=0.5, step=0.05, label="Practicality")
161
 
162
  chat_history = gr.Chatbot(label="Chat")
163
 
@@ -167,11 +174,12 @@ with gr.Blocks(css=custom_css) as demo:
167
  # Adjusted to ensure history is maintained and passed correctly
168
  user_input.submit(
169
  respond,
170
- inputs=[user_input, chat_history, system_message_box, temperature, practicality, use_local_model],
 
171
  outputs=[chat_history, system_message_box]
172
  )
173
 
174
  cancel_button.click(cancel_inference)
175
 
176
  if __name__ == "__main__":
177
- demo.launch(share=True)
 
2
  from huggingface_hub import InferenceClient
3
  import torch
4
  from transformers import pipeline
 
5
 
6
  # Inference client setup
7
+ client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
8
+ pipe = pipeline(
9
+ "text-generation",
10
+ "microsoft/Phi-3-mini-4k-instruct",
11
+ torch_dtype=torch.bfloat16,
12
+ device_map="auto"
13
+ )
14
 
15
  # Global flag to handle cancellation
16
  stop_inference = False
 
24
  history: list[tuple[str, str]],
25
  system_message_val,
26
  temperature=0.7,
27
+ # practicality=None, # Commented out
 
28
  max_tokens=256,
29
+ use_local_model=False,
30
  ):
31
  global stop_inference
32
  stop_inference = False # Reset cancellation flag
33
 
34
+ # if practicality is None:
35
+ # practicality = round(random.uniform(0,1), 1) # Initialize random practicality score
36
+ # if practicality > 0.5:
37
+ # append_message = "Provide actionable advice or direct instructions."
38
+ # else:
39
+ # append_message = "Provide theoretical concepts or abstract quotes."
40
+ # system_message_val = f"{base_message} {append_message}"
41
+
42
+ # Keeping the base message as it is without modifications
43
+ system_message_val = base_message
44
 
45
  # Initialize history if it's None
46
  if history is None:
 
60
  output = pipe(
61
  input_text,
62
  temperature=temperature,
63
+ max_new_tokens=256,
64
  do_sample=True,
65
  num_return_sequences=1,
66
  )
 
86
  messages=messages,
87
  stream=True,
88
  temperature=temperature,
89
+ max_tokens=256
90
  ):
91
  if stop_inference:
92
  response = "Inference cancelled."
 
164
  )
165
  use_local_model = gr.Checkbox(label="Use Local Model", value=False)
166
  temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
167
+ # practicality = gr.Slider(minimum=0.1, maximum=1.0, value=0.5, step=0.05, label="Practicality") # Commented out
168
 
169
  chat_history = gr.Chatbot(label="Chat")
170
 
 
174
  # Adjusted to ensure history is maintained and passed correctly
175
  user_input.submit(
176
  respond,
177
+ # Removed practicality from inputs
178
+ inputs=[user_input, chat_history, system_message_box, temperature, use_local_model],
179
  outputs=[chat_history, system_message_box]
180
  )
181
 
182
  cancel_button.click(cancel_inference)
183
 
184
  if __name__ == "__main__":
185
+ demo.launch(share=False)
tests.py CHANGED
@@ -1,4 +1,4 @@
1
- from app import respond, base_message
2
  import time
3
 
4
  def test_api():
@@ -13,22 +13,20 @@ def test_api():
13
 
14
  start_time = time.time()
15
 
16
- # Call the respond function
17
  result_generator = respond(
18
  message=message,
19
  history=history,
20
  system_message_val=system_message_val,
21
  temperature=temperature,
22
  practicality=practicality,
23
- max_tokens=max_tokens,
24
  use_local_model=use_local_model
25
  )
26
 
27
- # Since respond is a generator, we need to iterate over it to get the final output
 
28
  for result in result_generator:
29
  final_history, final_system_message = result
30
- # Optionally, print intermediate results
31
- # print("Intermediate history:", final_history)
32
 
33
  end_time = time.time()
34
  runtime = end_time - start_time
@@ -40,31 +38,42 @@ def test_api():
40
 
41
  def test_local():
42
  # Set up input parameters
43
- message = "What is the meaning of life"
44
  history = []
45
  system_message_val = base_message
46
  temperature = 0.7
47
  practicality = 0.8 # This should modify the system message to provide actionable advice
48
- max_tokens = 256
49
- use_local_model = True
50
 
51
  start_time = time.time()
 
52
 
53
- # Call the respond function
54
- result = list(respond(
55
  message=message,
56
  history=history,
57
  system_message_val=system_message_val,
58
  temperature=temperature,
59
  practicality=practicality,
60
- max_tokens=max_tokens,#test
61
- use_local_model=use_local_model
62
- ))
 
 
 
 
 
 
 
63
 
64
  end_time = time.time()
65
  runtime = end_time - start_time
66
 
67
- final_history, final_system_message = result[-1]
68
  print("Local Runtime: ", runtime)
69
  print("Local Final conversation history:", final_history)
70
  print("Local Final system message:", final_system_message)
 
 
 
 
 
 
1
+ from app import respond, base_message
2
  import time
3
 
4
  def test_api():
 
13
 
14
  start_time = time.time()
15
 
16
+ # Call the respond function (generator)
17
  result_generator = respond(
18
  message=message,
19
  history=history,
20
  system_message_val=system_message_val,
21
  temperature=temperature,
22
  practicality=practicality,
 
23
  use_local_model=use_local_model
24
  )
25
 
26
+ # Iterate over the generator to get the final output
27
+ final_history, final_system_message = None, None
28
  for result in result_generator:
29
  final_history, final_system_message = result
 
 
30
 
31
  end_time = time.time()
32
  runtime = end_time - start_time
 
38
 
39
  def test_local():
40
  # Set up input parameters
41
+ message = "What is the meaning of life?"
42
  history = []
43
  system_message_val = base_message
44
  temperature = 0.7
45
  practicality = 0.8 # This should modify the system message to provide actionable advice
46
+ use_local_model = True # Set to True to use the local model
 
47
 
48
  start_time = time.time()
49
+ print("start time: ", start_time)
50
 
51
+ # Call the respond function (generator)
52
+ result_generator = respond(
53
  message=message,
54
  history=history,
55
  system_message_val=system_message_val,
56
  temperature=temperature,
57
  practicality=practicality,
58
+ use_local_model=use_local_model,
59
+ max_tokens=256
60
+ )
61
+
62
+ # Iterate over the generator to get the final output
63
+ final_history, final_system_message = None, None
64
+ print("Iterating over results...")
65
+ for result in result_generator:
66
+ print("Result: ", result)
67
+ final_history, final_system_message = result
68
 
69
  end_time = time.time()
70
  runtime = end_time - start_time
71
 
 
72
  print("Local Runtime: ", runtime)
73
  print("Local Final conversation history:", final_history)
74
  print("Local Final system message:", final_system_message)
75
+
76
+ # Run the tests
77
+ if __name__ == "__main__":
78
+ # test_api()
79
+ test_local()