Spaces:
Sleeping
Sleeping
moprei21
commited on
Commit
•
825e780
1
Parent(s):
c3997ed
Add app
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline, GenerationConfig
|
3 |
+
|
4 |
+
|
5 |
+
|
6 |
+
def greet(prompt, temperature,max_length, top_p,):
|
7 |
+
return "Hello, World!"
|
8 |
+
|
9 |
+
def language_model(prompt, temperature, max_length, top_p, model):
|
10 |
+
|
11 |
+
generator = pipeline("text-generation", model=model)
|
12 |
+
config = GenerationConfig.from_pretrained(model)
|
13 |
+
|
14 |
+
# add params to config
|
15 |
+
config.top_p = top_p
|
16 |
+
config.temperature = temperature
|
17 |
+
config.do_sample = True
|
18 |
+
|
19 |
+
if temperature == 0:
|
20 |
+
config.do_sample = False
|
21 |
+
|
22 |
+
|
23 |
+
# generate text
|
24 |
+
data = generator(prompt, max_new_tokens=max_length, generation_config=config)
|
25 |
+
return data[0]["generated_text"]
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
demo = gr.Interface(
|
30 |
+
fn=language_model,
|
31 |
+
inputs=[gr.Textbox(placeholder="Write a nice poem ! ", lines=5, label="Input Text"),
|
32 |
+
gr.Slider(minimum=0, maximum=2, value=1, label="Temperature", info="Controls randomness: Lowering results in less random completions."),
|
33 |
+
gr.Slider(minimum=1, maximum=256, value=16, label="Maximum Tokens",info="The maximum number of tokens to generate."),
|
34 |
+
gr.Slider(minimum=0, maximum=1, value=1, label="Top-p", info="Controls diversity via nucleus sampling."),
|
35 |
+
gr.Dropdown(choices=["distilgpt2","gpt2", "gpt2-large"], value="gpt2",label="Model", info="Model to choose from Huggingface")],
|
36 |
+
outputs=[gr.Textbox(placeholder="Hello, World!")],
|
37 |
+
)
|
38 |
+
demo.launch(share=True)
|