itsalissonsilva commited on
Commit
f26aef8
·
verified ·
1 Parent(s): 6c5ba8a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+
4
+ # Initialize the model and tokenizer
5
+ model_name = "distilgpt2"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForCausalLM.from_pretrained(model_name)
8
+
9
+ # Sample starting scenario and choices
10
+ scenario = "You wake up in a strange forest. Paths lead in all directions."
11
+ choices = [
12
+ "Walk north into the dense forest.",
13
+ "Go south back towards a distant town.",
14
+ "Explore east towards the sound of water.",
15
+ "Move west where the land rises."
16
+ ]
17
+
18
+ def generate_continuation(choice):
19
+ prompt = f"{scenario} You decide to {choice}"
20
+ max_length = 50 # Adjust as needed
21
+
22
+ input_ids = tokenizer.encode(prompt, return_tensors='pt')
23
+ output = model.generate(input_ids, max_length=max_length, num_return_sequences=1)
24
+ continuation = tokenizer.decode(output[0], skip_special_tokens=True)
25
+
26
+ return continuation
27
+
28
+ def update_scenario(choice_index):
29
+ chosen_action = choices[choice_index]
30
+ new_scenario = generate_continuation(chosen_action)
31
+ return new_scenario
32
+
33
+ # Gradio app
34
+ with gr.Blocks() as app:
35
+ with gr.Row():
36
+ story_display = gr.Textbox(value=scenario, show_label=False, readonly=True, lines=7)
37
+ choices_buttons = [gr.Button(choice, elem_id=f"choice_{i}") for i, choice in enumerate(choices)]
38
+
39
+ def handle_choice(choice_index):
40
+ new_story = update_scenario(choice_index)
41
+ story_display.update(value=new_story)
42
+ return gr.update()
43
+
44
+ for i, button in enumerate(choices_buttons):
45
+ button.click(fn=handle_choice, inputs=i, outputs=story_display)
46
+
47
+ app.launch()