mylesmharrison commited on
Commit
65553b6
·
1 Parent(s): 63559d9
Files changed (5) hide show
  1. README.md +11 -5
  2. app.py +113 -0
  3. examples.txt +6 -0
  4. gradio_data.json +9 -0
  5. requirements.txt +3 -0
README.md CHANGED
@@ -1,12 +1,18 @@
1
  ---
2
- title: Gpt2 Moviedialog
3
- emoji: 🦀
4
- colorFrom: pink
5
- colorTo: gray
6
  sdk: gradio
7
  sdk_version: 3.39.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
1
  ---
2
+ title: gpt2-moviedialog
3
+ emoji: 🎥
4
+ colorFrom: blue
5
+ colorTo: green
6
  sdk: gradio
7
  sdk_version: 3.39.0
8
  app_file: app.py
9
  pinned: false
10
  ---
11
 
12
+ # gpt2-moviedialog
13
+
14
+ This is a Space for the [gpt2-moviedialog](https://huggingface.co/mylesmharrison/gpt2-moviedialog) model. The model is based upon [gpt2](https://huggingface.co/gpt2) and tuned on the [Cornell Movie Dialogs Corpus](https://www.cs.cornell.edu/~cristian/Cornell_Movie-Dialogs_Corpus.html).
15
+
16
+ You can play with the different settings to get varying outputs - setting the temperature higher will give the model more freedom and will result in more "creative" outputs. Conversely, setting Top K or Top P too low will result in more constrained outputs and may result in the model getting stuck in repetition or loops.
17
+
18
+ Try one of the examples below from famous films and click "Submit".
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ from threading import Thread
4
+ import gradio as gr
5
+ import torch
6
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
7
+
8
+
9
+ ##### MODEL AND TOKENIZER SETUP ######
10
+ model_id = "mylesmharrison/gpt2-moviedialog"
11
+ model = AutoModelForCausalLM.from_pretrained(model_id, from_tf=True)
12
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
13
+
14
+ ##### TEXT GENERATION (INFERENCE) ######
15
+ def run_generation(user_text, top_p, temperature, top_k, max_new_tokens):
16
+ # Get the model and tokenizer, and tokenize the user text.
17
+ model_inputs = tokenizer([user_text], return_tensors="pt")
18
+
19
+ # Start generation on a separate thread, so that we don't block the UI. The text is pulled from the streamer
20
+ # in the main thread. Adds timeout to the streamer to handle exceptions in the generation thread.
21
+ streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
22
+ generate_kwargs = dict(
23
+ model_inputs,
24
+ streamer=streamer,
25
+ max_new_tokens=max_new_tokens,
26
+ do_sample=True,
27
+ top_p=top_p,
28
+ temperature=float(temperature),
29
+ top_k=top_k
30
+ )
31
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
32
+ t.start()
33
+
34
+ # Pull the generated text from the streamer, and update the model output.
35
+ model_output = ""
36
+ for new_text in streamer:
37
+ model_output += new_text
38
+ yield model_output
39
+ return model_output
40
+
41
+
42
+ ##### HELPER FUNCTION AND TEXT DATA ######
43
+ def reset_textbox():
44
+ return gr.update(value='')
45
+
46
+ # Read in the text data for examples
47
+ with open("gradio_data.json", "r") as f:
48
+ gr_data = json.loads(f.read())
49
+
50
+ # Read in the markdown data for the header
51
+ with open("README.md", "r") as f:
52
+ desc = ''.join(f.readlines()[11:])
53
+
54
+ theme = gr.themes.Base()
55
+
56
+ ##### UI RENDER ######
57
+ with gr.Blocks(theme=theme) as demo:
58
+
59
+ with gr.Row():
60
+
61
+ with gr.Column():
62
+ ### DESCRIPTION
63
+ gr.Markdown(desc)
64
+
65
+
66
+ with gr.Row():
67
+ with gr.Column():
68
+
69
+ # INPUT FIELD
70
+ user_text = gr.Textbox(
71
+ placeholder="JOHN: I love you.",
72
+ label="User input",
73
+ scale=4,
74
+ lines=10
75
+ )
76
+
77
+ # SUBMIT BUTTON
78
+ with gr.Row():
79
+ with gr.Column():
80
+ button_submit = gr.Button(value="Submit")
81
+ with gr.Column():
82
+ stop = gr.Button(value="Stop")
83
+
84
+ ### TEXT EXAMPLES
85
+ gr.Examples(gr_data['examples'],[user_text])
86
+
87
+ with gr.Column():
88
+
89
+ with gr.Row(scale=4):
90
+ model_output = gr.Textbox(label="Model output", lines=10, interactive=False)
91
+
92
+ with gr.Row(scale=1):
93
+ ### INFERENCE CONTROLS
94
+ max_new_tokens = gr.Slider(
95
+ minimum=1, maximum=500, value=150, step=10, interactive=True, label="Max Tokens",
96
+ )
97
+ top_p = gr.Slider(
98
+ minimum=0.05, maximum=1.0, value=0.95, step=0.05, interactive=True, label="Top-p",
99
+ )
100
+ top_k = gr.Slider(
101
+ minimum=1, maximum=50, value=20, step=1, interactive=True, label="Top-k",
102
+ )
103
+ temperature = gr.Slider(
104
+ minimum=0.1, maximum=5.0, value=1.1, step=0.1, interactive=True, label="Temperature",
105
+ )
106
+
107
+ # RUN
108
+ text_event = user_text.submit(run_generation, [user_text, top_p, temperature, top_k, max_new_tokens], model_output)
109
+ button_event = button_submit.click(run_generation, [user_text, top_p, temperature, top_k, max_new_tokens], model_output)
110
+
111
+ stop.click(fn=None, inputs=None, outputs=None, cancels=[text_event, button_event])
112
+
113
+ demo.queue(max_size=32).launch(enable_queue=True)
examples.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ NEO: I thought it wasn't real.\nMORPHEUS: Your mind makes it real.\nNEO: If you're killed in the matrix, you die here?\nMORPHEUS: The body cannot live without the mind.
2
+ HAN SOLO: Hokey religions and ancient weapons are not a good match for a blaster at your side, kid.\nLUKE SKYWALKER: You don't believe in the Force, do you?
3
+ VIZZINI: He didn't fall? Inconceivable!\nINIGO MONTOYA: You keep using that word. I do not think it means what you think it means.
4
+ TERRY: You don't understand. I coulda had class. I coulda been a contender. I coulda been somebody, instead of a bum, which is what I am, let's face it. It was you, Charley.
5
+ DOROTHY: Toto, I've a feeling we're not in Kansas any more.
6
+ RICK: Who are you really, and what were you before? What did you do and what did you think, huh?\nILSA: We said no questions.\nRICK: ...Here's looking at you, kid.
gradio_data.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "examples": [
3
+ "NEO: I thought it wasn't real.\nMORPHEUS: Your mind makes it real.\nNEO: If you're killed in the matrix, you die here?\nMORPHEUS: The body cannot live without the mind.\n",
4
+ "HAN SOLO: Hokey religions and ancient weapons are not a good match for a blaster at your side, kid.\nLUKE SKYWALKER: You don't believe in the Force, do you?\n",
5
+ "VIZZINI: He didn't fall? Inconceivable!\nINIGO MONTOYA: You keep using that word. I do not think it means what you think it means.\n", "TERRY: You don't understand. I coulda had class. I coulda been a contender. I coulda been somebody, instead of a bum, which is what I am, let's face it. It was you, Charley.\n",
6
+ "DOROTHY: Toto, I've a feeling we're not in Kansas any more.\n",
7
+ "RICK: Who are you really, and what were you before? What did you do and what did you think, huh?\nILSA: We said no questions.\nRICK: ...Here's looking at you, kid.\n"]
8
+
9
+ }
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ torch
3
+ tensorflow