itsalissonsilva commited on
Commit
40e41e9
·
verified ·
1 Parent(s): 4d7b676

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -27
app.py CHANGED
@@ -1,13 +1,12 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
 
3
 
4
- # Initialize the model and tokenizer
5
- model_name = "microsoft/phi-2"
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,36 +14,31 @@ choices = [
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()
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceApi
3
+ import os
4
 
5
+ api_token = os.getenv('token')
 
 
 
6
 
7
+ client = InferenceApi("gpt2", api_token=api_token)
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
  "Move west where the land rises."
15
  ]
16
 
17
+ history = []
18
+
19
+ def generate_continuation(choice):
20
+ global scenario, history
21
+ history.append(choice)
22
+ prompt = f"{scenario} {' '.join(history)}"
23
  max_length = 50 # Adjust as needed
 
 
 
 
24
 
25
+ response = client(prompt, max_length=max_length, remove_input=True)
26
+ continuation = response[0]['generated_text']
 
 
 
 
 
 
27
 
28
+ return continuation
 
29
 
30
  with gr.Blocks() as app:
31
  with gr.Row():
32
+ story_display = gr.Textbox(value=scenario, lines=7)
33
  choice_buttons = [gr.Button(choice) for choice in choices]
34
 
35
  def handle_choice(choice_index):
36
+ chosen_action = choices[choice_index]
37
+ new_scenario = generate_continuation(chosen_action)
38
+ story_display.update(value=new_scenario)
39
+ return gr.update()
40
 
41
  for i, button in enumerate(choice_buttons):
42
+ button.click(fn=lambda x=i: handle_choice(x), inputs=None, outputs=story_display)
43
 
44
  app.launch()