Pankaj8922 commited on
Commit
c84bdec
1 Parent(s): 5725936

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -1
app.py CHANGED
@@ -1,3 +1,34 @@
1
  import gradio as gr
 
2
 
3
- gr.load("models/openai-community/gpt2").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ generator = pipeline('text-generation', model="gpt2", pad_token_id=50256)
5
+
6
+ def generate_text(prompt, max_length, temperature, top_k, top_p):
7
+ result = generator(
8
+ prompt,
9
+ max_length=max_length,
10
+ temperature=temperature,
11
+ top_k=top_k,
12
+ top_p=top_p,
13
+ do_sample=True,
14
+ truncation=True
15
+ )
16
+ return result[0]['generated_text']
17
+
18
+ with gr.Blocks() as demo:
19
+ gr.Markdown("# GPT-2 Text Generation with Custom Settings")
20
+
21
+ prompt = gr.Textbox(label="Enter your prompt here")
22
+
23
+ max_length = gr.Slider(label="Max Length", minimum=10, maximum=200, value=50, step=1)
24
+ temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, value=0.7, step=0.1)
25
+ top_k = gr.Slider(label="Top-K Sampling", minimum=0, maximum=100, value=50, step=1)
26
+ top_p = gr.Slider(label="Top-P (Nucleus Sampling)", minimum=0.0, maximum=1.0, value=0.9, step=0.1)
27
+
28
+ output = gr.Textbox(label="Generated Text", interactive=False)
29
+
30
+ generate_button = gr.Button("Generate")
31
+
32
+ generate_button.click(generate_text, inputs=[prompt, max_length, temperature, top_k, top_p], outputs=output)
33
+
34
+ demo.launch()