tmzh commited on
Commit
b084b77
1 Parent(s): d2a7e23
Files changed (1) hide show
  1. app.py +36 -37
app.py CHANGED
@@ -34,6 +34,17 @@ EXAMPLE_CLUES = [
34
  (['ARCTIC', 'SHUT', 'STAMP'], 'SEAL', 'such as the Arctic being home to seals, or shutting a seal on an envelope, or a stamp being a type of seal'),
35
  ]
36
 
 
 
 
 
 
 
 
 
 
 
 
37
  def create_example_groupings(clues: List[Tuple[List[str], str, str]], num_groups: int = 5) -> List[Tuple[List[str], str]]:
38
  """Creates example groupings from the given clues."""
39
  merged = create_random_word_groups(clues, num_groups)
@@ -51,17 +62,6 @@ def create_example_groupings(clues: List[Tuple[List[str], str, str]], num_groups
51
 
52
  EXAMPLE_GROUPINGS = create_example_groupings(EXAMPLE_CLUES)
53
 
54
- def create_random_word_groups(clues: List[Tuple[List[str], str, str]], num_groups: int = 10) -> List[Tuple[List[str], List[int]]]:
55
- """Creates random groups of words from the given clues."""
56
- word_groups = []
57
- while len(word_groups) < num_groups:
58
- group_size = random.choice([3, 4])
59
- selected_indices = random.sample(range(len(clues)), group_size)
60
- words = [word for row in [clues[i][0] for i in selected_indices] for word in row]
61
- if len(words) in [8, 9]:
62
- word_groups.append((words, selected_indices))
63
- return word_groups
64
-
65
  def render_template(template: str, system_prompt: str, history: List[Tuple], query: str) -> str:
66
  """Renders a Jinja2 template with the given parameters."""
67
  env = Environment(loader=FileSystemLoader('.'))
@@ -149,36 +149,35 @@ def generate_clue_callback(group: List[str]) -> gr.update:
149
  return gr.update(value=clue['clue'], info=clue['explanation'])
150
 
151
 
152
- if __name__ == "__main__":
153
- # UI Setup
154
- with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
155
- gr.Markdown("# *Codenames* Clue Generator")
156
- gr.Markdown("Provide a list of words to generate clues")
157
 
158
- with gr.Row():
159
- game_image = gr.Image(type="pil")
160
- word_list_input = gr.Dropdown(label="Detected words", choices=[], multiselect=True, interactive=True)
161
 
162
- with gr.Row():
163
- detect_words_button = gr.Button("Detect Words")
164
- group_words_button = gr.Button("Group Words")
165
 
166
- group_inputs, clue_buttons, clue_outputs = [], [], []
167
 
168
- for i in range(4):
169
- with gr.Row():
170
- group_input = gr.Dropdown(label=f"Group {i + 1}", choices=[], allow_custom_value=True, multiselect=True, interactive=True)
171
- clue_button = gr.Button("Generate Clue", size='sm')
172
- clue_output = gr.Textbox(label=f"Clue {i + 1}")
173
- group_inputs.append(group_input)
174
- clue_buttons.append(clue_button)
175
- clue_outputs.append(clue_output)
176
 
177
- # Event handlers
178
- detect_words_button.click(fn=process_image, inputs=game_image, outputs=[word_list_input])
179
- group_words_button.click(fn=group_words_callback, inputs=word_list_input, outputs=group_inputs)
180
 
181
- for i in range(4):
182
- clue_buttons[i].click(generate_clue_callback, inputs=group_inputs[i], outputs=clue_outputs[i])
183
 
184
- demo.launch(share=True, debug=True)
 
34
  (['ARCTIC', 'SHUT', 'STAMP'], 'SEAL', 'such as the Arctic being home to seals, or shutting a seal on an envelope, or a stamp being a type of seal'),
35
  ]
36
 
37
+ def create_random_word_groups(clues: List[Tuple[List[str], str, str]], num_groups: int = 10) -> List[Tuple[List[str], List[int]]]:
38
+ """Creates random groups of words from the given clues."""
39
+ word_groups = []
40
+ while len(word_groups) < num_groups:
41
+ group_size = random.choice([3, 4])
42
+ selected_indices = random.sample(range(len(clues)), group_size)
43
+ words = [word for row in [clues[i][0] for i in selected_indices] for word in row]
44
+ if len(words) in [8, 9]:
45
+ word_groups.append((words, selected_indices))
46
+ return word_groups
47
+
48
  def create_example_groupings(clues: List[Tuple[List[str], str, str]], num_groups: int = 5) -> List[Tuple[List[str], str]]:
49
  """Creates example groupings from the given clues."""
50
  merged = create_random_word_groups(clues, num_groups)
 
62
 
63
  EXAMPLE_GROUPINGS = create_example_groupings(EXAMPLE_CLUES)
64
 
 
 
 
 
 
 
 
 
 
 
 
65
  def render_template(template: str, system_prompt: str, history: List[Tuple], query: str) -> str:
66
  """Renders a Jinja2 template with the given parameters."""
67
  env = Environment(loader=FileSystemLoader('.'))
 
149
  return gr.update(value=clue['clue'], info=clue['explanation'])
150
 
151
 
152
+ # UI Setup
153
+ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
154
+ gr.Markdown("# *Codenames* Clue Generator")
155
+ gr.Markdown("Provide a list of words to generate clues")
 
156
 
157
+ with gr.Row():
158
+ game_image = gr.Image(type="pil")
159
+ word_list_input = gr.Dropdown(label="Detected words", choices=[], multiselect=True, interactive=True)
160
 
161
+ with gr.Row():
162
+ detect_words_button = gr.Button("Detect Words")
163
+ group_words_button = gr.Button("Group Words")
164
 
165
+ group_inputs, clue_buttons, clue_outputs = [], [], []
166
 
167
+ for i in range(4):
168
+ with gr.Row():
169
+ group_input = gr.Dropdown(label=f"Group {i + 1}", choices=[], allow_custom_value=True, multiselect=True, interactive=True)
170
+ clue_button = gr.Button("Generate Clue", size='sm')
171
+ clue_output = gr.Textbox(label=f"Clue {i + 1}")
172
+ group_inputs.append(group_input)
173
+ clue_buttons.append(clue_button)
174
+ clue_outputs.append(clue_output)
175
 
176
+ # Event handlers
177
+ detect_words_button.click(fn=process_image, inputs=game_image, outputs=[word_list_input])
178
+ group_words_button.click(fn=group_words_callback, inputs=word_list_input, outputs=group_inputs)
179
 
180
+ for i in range(4):
181
+ clue_buttons[i].click(generate_clue_callback, inputs=group_inputs[i], outputs=clue_outputs[i])
182
 
183
+ demo.launch(share=True, debug=True)