CedricZ commited on
Commit
5ba1135
β€’
1 Parent(s): 0d2ec57

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +17 -13
  2. final_app.py +48 -0
  3. mockup.py +12 -0
  4. mockup_v2.py +22 -0
README.md CHANGED
@@ -1,13 +1,17 @@
1
- ---
2
- title: Demo Gpt2
3
- emoji: πŸ†
4
- colorFrom: yellow
5
- colorTo: gray
6
- sdk: gradio
7
- sdk_version: 4.28.3
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
1
+ #### Name:
2
+ **Zishi Zhang**
3
+
4
+ ******
5
+
6
+ ### Student ID:
7
+ **23-741-390**
8
+
9
+ ******
10
+
11
+ ### Collaborators:
12
+ **Yuhao Fan**
13
+
14
+ ******
15
+
16
+ ### URL:
17
+ **<https://huggingface.co/spaces/CedricZ/demo_gpt2>**
final_app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline, GenerationConfig
3
+
4
+ #Load pipeline
5
+ generator = pipeline("text-generation", model="gpt2")
6
+ config = GenerationConfig.from_pretrained("gpt2")
7
+
8
+ def greet(prompt, temperature, max_lenth, top_p, samples):
9
+ #Enable greedy decoding if temperature is 0
10
+ config.do_sample = True if temperature != 0 else False
11
+
12
+ #Takes temperature and top_p value
13
+ config.temperature = temperature
14
+ config.top_p = top_p
15
+
16
+ #Samples
17
+ sample_input = {'Sample 1': 'Hi, this is a demo prompt for you',
18
+ 'Sample 2': 'Alber Einstein is a famous physicist graduated from',
19
+ 'Sample 3': 'University of Zurich located in'}
20
+
21
+ #Choose between samples
22
+ if samples and prompt == '':
23
+ prompt = sample_input[samples]
24
+
25
+ #Streaming the output
26
+ for i in range(max_lenth):
27
+ a = generator(prompt, max_new_tokens=1, generation_config=config)
28
+ prompt=a[0]['generated_text']
29
+ yield a[0]['generated_text']
30
+
31
+ demo = gr.Interface(
32
+ fn=greet,
33
+ inputs=[gr.Textbox(placeholder = "Write a tagline for an ice cream shop.", label="prompt", lines=5),
34
+ gr.Slider(value=1, minimum=0, maximum=2, label='temperature',
35
+ info='''Temperature controls the randomness of the text generation.
36
+ 1.0 makes the model more likely to generate diverse and sometimes more unexpected outputs.
37
+ 0.0 makes the model's responses more deterministic and predictable.'''),
38
+ gr.Slider(value=16, minimum=1, maximum=256, step=1, label='max_lenth',
39
+ info='''Maximum number of tokens that the model will generate in the output.'''),
40
+ gr.Slider(value=1, minimum=0, maximum=1, label='top_p',
41
+ info='''Top-p controls the model's focus during text generation.
42
+ It allows only the most probable tokens to be considered for generation, where the cumulative probability of these tokens must exceed this value.'''),
43
+ gr.Dropdown(['Sample 1', 'Sample 2', 'Sample 3'], label="Sample Prompts",
44
+ info='''Some sample Prompts for you!''')],
45
+ outputs=[gr.Textbox(label='Output texts')],
46
+ )
47
+
48
+ demo.launch()
mockup.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ def greet(prompt, temperature, max_lenth, top_p):
4
+ return "Hello, world!"
5
+
6
+ demo = gr.Interface(
7
+ fn=greet,
8
+ inputs=["text", "slider", "slider", "slider"],
9
+ outputs=["text"],
10
+ )
11
+
12
+ demo.launch()
mockup_v2.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ def greet(prompt, temperature, max_lenth, top_p):
4
+ return f"Hello, world! {temperature}"
5
+
6
+ demo = gr.Interface(
7
+ fn=greet,
8
+ inputs=[gr.Textbox(placeholder = "Write a tagline for an ice cream shop.", label="prompt", lines=5),
9
+ gr.Slider(value=1, minimum=0, maximum=2, label='temperature',
10
+ info='''Temperature controls the randomness of the text generation.
11
+ 1.0 makes the model more likely to generate diverse and sometimes more unexpected outputs.
12
+ 0.0 makes the model's responses more deterministic and predictable.'''),
13
+ gr.Slider(value=16, minimum=1, maximum=256, step=1, label='max_lenth',
14
+ info='''Maximum number of tokens that the model will generate in the output.'''),
15
+ gr.Slider(value=1, minimum=0, maximum=1, label='top_p',
16
+ info='''Top-p controls the model's focus during text generation.
17
+ It allows only the most probable tokens to be considered for generation, where the cumulative probability of these tokens must exceed this value.''')
18
+ ],
19
+ outputs=[gr.Textbox(label='Output texts')],
20
+ )
21
+
22
+ demo.launch()