itsalissonsilva commited on
Commit
09add94
·
verified ·
1 Parent(s): 1ecd6f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -6,7 +6,8 @@ model_name = "distilgpt2"
6
  tokenizer = AutoTokenizer.from_pretrained(model_name)
7
  model = AutoModelForCausalLM.from_pretrained(model_name)
8
 
9
- scenario = "You wake up in a strange forest. Paths lead in all directions."
 
10
  choices = [
11
  "Walk north into the dense forest.",
12
  "Go south back towards a distant town.",
@@ -14,27 +15,36 @@ choices = [
14
  "Move west where the land rises."
15
  ]
16
 
17
- def generate_continuation(choice):
18
- prompt = f"{scenario} You decide to {choice}"
19
  max_length = 50 # Adjust as needed
20
-
21
  input_ids = tokenizer.encode(prompt, return_tensors='pt')
22
  output = model.generate(input_ids, max_length=max_length, num_return_sequences=1)
23
  continuation = tokenizer.decode(output[0], skip_special_tokens=True)
24
-
25
  return continuation
26
 
27
  def update_scenario(choice_index):
 
28
  chosen_action = choices[choice_index]
29
- new_scenario = generate_continuation(chosen_action)
30
- return new_scenario
 
 
 
 
 
 
31
 
32
  with gr.Blocks() as app:
33
  with gr.Row():
34
- story_display = gr.Textbox(value=scenario, show_label=False, lines=7)
35
  choice_buttons = [gr.Button(choice) for choice in choices]
36
 
 
 
 
 
 
37
  for i, button in enumerate(choice_buttons):
38
- button.click(fn=lambda x=i: update_scenario(x), inputs=None, outputs=story_display)
39
 
40
  app.launch()
 
6
  tokenizer = AutoTokenizer.from_pretrained(model_name)
7
  model = AutoModelForCausalLM.from_pretrained(model_name)
8
 
9
+ initial_scenario = "You wake up in a strange forest. Paths lead in all directions."
10
+ history = [initial_scenario]
11
  choices = [
12
  "Walk north into the dense forest.",
13
  "Go south back towards a distant town.",
 
15
  "Move west where the land rises."
16
  ]
17
 
18
+ def generate_continuation(prompt):
 
19
  max_length = 50 # Adjust as needed
 
20
  input_ids = tokenizer.encode(prompt, return_tensors='pt')
21
  output = model.generate(input_ids, max_length=max_length, num_return_sequences=1)
22
  continuation = tokenizer.decode(output[0], skip_special_tokens=True)
 
23
  return continuation
24
 
25
  def update_scenario(choice_index):
26
+ global history, choices
27
  chosen_action = choices[choice_index]
28
+ history.append(chosen_action)
29
+ full_story = " ".join(history)
30
+ new_part = generate_continuation(full_story)
31
+ history.append(new_part) # Update history with the new part of the story
32
+ full_story = " ".join(history)
33
+
34
+ # For simplicity, we keep the same choices here, but you could generate new choices based on the story.
35
+ return full_story
36
 
37
  with gr.Blocks() as app:
38
  with gr.Row():
39
+ story_display = gr.Textbox(value=initial_scenario, show_label=False, lines=10)
40
  choice_buttons = [gr.Button(choice) for choice in choices]
41
 
42
+ def handle_choice(choice_index):
43
+ new_story = update_scenario(choice_index)
44
+ story_display.update(value=new_story)
45
+ return [gr.update() for _ in choices]
46
+
47
  for i, button in enumerate(choice_buttons):
48
+ button.click(fn=lambda x=i: handle_choice(x), outputs=story_display)
49
 
50
  app.launch()