cafierom commited on
Commit
f76ba2f
·
verified ·
1 Parent(s): 617e44f

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +3 -2
  2. inflections_funcs.py +7 -4
app.py CHANGED
@@ -63,7 +63,7 @@ def generate_beam_html(index, all_beams_data, dark_mode):
63
  @spaces.GPU
64
  def predict(prompt, dark_mode, temperature):
65
  """
66
- Generates responses for 5 beams and returns the first beam's visualization and visibility for controls.
67
  """
68
  # Generate beams
69
  generated_dicts, transcription = make_beams(model, processor, prompt, temperature=temperature)
@@ -95,7 +95,7 @@ def switch_beam(current_index, all_beams_data, dark_mode):
95
  if all_beams_data is None:
96
  return None, None, 0
97
 
98
- new_index = (current_index + 1) % 5
99
  main_html, detail_html = generate_beam_html(new_index, all_beams_data, dark_mode)
100
  return main_html, detail_html, new_index
101
 
@@ -111,6 +111,7 @@ with gr.Blocks() as demo:
111
  - Display the first output beam, highlighting any word or parts of words (tokens) with a probability score < 0.6 in red.
112
  - These unconfident words/tokens can be considered inflection points, or places where the output could change easily.
113
  - Provide a toggle button to view detailed word/token probabilities for the beam.
 
114
  ''')
115
 
116
  with gr.Column():
 
63
  @spaces.GPU
64
  def predict(prompt, dark_mode, temperature):
65
  """
66
+ Generates responses for 6 beams and returns the first beam's visualization and visibility for controls.
67
  """
68
  # Generate beams
69
  generated_dicts, transcription = make_beams(model, processor, prompt, temperature=temperature)
 
95
  if all_beams_data is None:
96
  return None, None, 0
97
 
98
+ new_index = (current_index + 1) % 6
99
  main_html, detail_html = generate_beam_html(new_index, all_beams_data, dark_mode)
100
  return main_html, detail_html, new_index
101
 
 
111
  - Display the first output beam, highlighting any word or parts of words (tokens) with a probability score < 0.6 in red.
112
  - These unconfident words/tokens can be considered inflection points, or places where the output could change easily.
113
  - Provide a toggle button to view detailed word/token probabilities for the beam.
114
+ - Switch between beams to see different possible outputs and their associated token probabilities.
115
  ''')
116
 
117
  with gr.Column():
inflections_funcs.py CHANGED
@@ -19,7 +19,7 @@ def start_model(model_id: str = "google/gemma-4-31B-it"):
19
 
20
  def make_beams(model: AutoModelForCausalLM, processor: AutoProcessor, initial_prompt: str, temperature: float = 1.0) -> Tuple[Any, List[str]]:
21
  '''
22
- Generates 5 beams in response to a prompt.
23
  '''
24
  messages = [
25
  {"role": "system", "content": "You are a helpful assistant."},
@@ -38,12 +38,15 @@ def make_beams(model: AutoModelForCausalLM, processor: AutoProcessor, initial_pr
38
  # Generate output
39
  generated_dicts = model.generate(**inputs,
40
  max_new_tokens=1024,
41
- num_beams=5, # Enables beam search with 5 active beams
42
- num_return_sequences=5, # Returns the top result
 
 
43
  return_dict_in_generate=True,
44
  output_scores=True,
45
  temperature=temperature,
46
- do_sample=True if temperature > 0 else False)
 
47
 
48
  transcription = processor.batch_decode(generated_dicts.sequences, skip_special_tokens=True)
49
 
 
19
 
20
  def make_beams(model: AutoModelForCausalLM, processor: AutoProcessor, initial_prompt: str, temperature: float = 1.0) -> Tuple[Any, List[str]]:
21
  '''
22
+ Generates 6 diverse beams in response to a prompt.
23
  '''
24
  messages = [
25
  {"role": "system", "content": "You are a helpful assistant."},
 
38
  # Generate output
39
  generated_dicts = model.generate(**inputs,
40
  max_new_tokens=1024,
41
+ num_beams=6, # Enables diverse beam search with 6 active beams
42
+ num_beam_groups=3, # Divide beams into 3 groups for diversity
43
+ diversity_penalty=1.0, # Penalty to encourage diversity between groups
44
+ num_return_sequences=6, # Returns the top 6 results
45
  return_dict_in_generate=True,
46
  output_scores=True,
47
  temperature=temperature,
48
+ do_sample=True if temperature > 0 else False,
49
+ trust_remote_code=True)
50
 
51
  transcription = processor.batch_decode(generated_dicts.sequences, skip_special_tokens=True)
52