File size: 1,308 Bytes
f26aef8
40e41e9
78d8a3e
f26aef8
40e41e9
f26aef8
78d8a3e
40e41e9
 
f26aef8
 
 
 
 
 
 
40e41e9
 
 
 
 
 
f26aef8
 
40e41e9
 
09add94
40e41e9
f26aef8
 
 
40e41e9
1ecd6f3
f26aef8
09add94
40e41e9
 
 
 
09add94
1ecd6f3
40e41e9
f26aef8
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import gradio as gr
import os
from huggingface_hub import InferenceClient

api_token = os.getenv('token')

client = InferenceClient("gpt2", token=api_token)

scenario = "You wake up in a strange forest. Paths lead in all directions."
choices = [
    "Walk north into the dense forest.",
    "Go south back towards a distant town.",
    "Explore east towards the sound of water.",
    "Move west where the land rises."
]

history = []

def generate_continuation(choice):
    global scenario, history
    history.append(choice)
    prompt = f"{scenario} {' '.join(history)}"
    max_length = 50  # Adjust as needed

    response = client(prompt, max_length=max_length, remove_input=True)
    continuation = response[0]['generated_text']

    return continuation

with gr.Blocks() as app:
    with gr.Row():
        story_display = gr.Textbox(value=scenario, lines=7)
        choice_buttons = [gr.Button(choice) for choice in choices]
    
    def handle_choice(choice_index):
        chosen_action = choices[choice_index]
        new_scenario = generate_continuation(chosen_action)
        story_display.update(value=new_scenario)
        return gr.update()

    for i, button in enumerate(choice_buttons):
        button.click(fn=lambda x=i: handle_choice(x), inputs=None, outputs=story_display)

app.launch()