profplate commited on
Commit
4edb4d1
·
verified ·
1 Parent(s): fc93662

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ TITLE = "News Draft Generator"
5
+ DESCRIPTION = (
6
+ "Experiment with news-style writing using distilgpt2. "
7
+ "Try headlines, article openings, research summaries, and briefings."
8
+ )
9
+
10
+ generator = pipeline("text-generation", model="distilgpt2")
11
+
12
+
13
+ def generate_text(prompt: str, temperature: float, top_p: float, max_length: int) -> str:
14
+ if not prompt or not prompt.strip():
15
+ return "Please enter a prompt."
16
+
17
+ # Clamp temperature to avoid division-by-zero-style issues in downstream sampling
18
+ temperature = max(0.01, float(temperature))
19
+
20
+ outputs = generator(
21
+ prompt,
22
+ do_sample=True,
23
+ temperature=temperature,
24
+ top_p=float(top_p),
25
+ max_length=int(max_length),
26
+ num_return_sequences=1,
27
+ pad_token_id=generator.tokenizer.eos_token_id,
28
+ )
29
+
30
+ return outputs[0]["generated_text"]
31
+
32
+
33
+ examples = [
34
+ ["Breaking news: scientists discovered that", 0.5, 0.9, 100],
35
+ ["A new report released today found that students", 0.5, 0.9, 100],
36
+ ["The conference on artificial intelligence announced", 0.5, 0.9, 100],
37
+ ["Researchers at the university published a study showing", 0.5, 0.9, 100],
38
+ ["This week in technology: the biggest story is", 0.5, 0.9, 100],
39
+ ]
40
+
41
+ with gr.Blocks(title=TITLE) as demo:
42
+ gr.Markdown(f"# {TITLE}")
43
+ gr.Markdown(DESCRIPTION)
44
+
45
+ with gr.Row():
46
+ with gr.Column():
47
+ prompt = gr.Textbox(
48
+ label="Prompt",
49
+ placeholder="Enter a headline, opening sentence, or briefing prompt...",
50
+ lines=4,
51
+ )
52
+
53
+ temperature = gr.Slider(
54
+ minimum=0.1,
55
+ maximum=2.0,
56
+ value=0.5,
57
+ step=0.1,
58
+ label="Temperature",
59
+ info="Controls how creative/wild the writing is",
60
+ )
61
+
62
+ top_p = gr.Slider(
63
+ minimum=0.1,
64
+ maximum=1.0,
65
+ value=0.9,
66
+ step=0.05,
67
+ label="Top-p",
68
+ info="Controls word diversity",
69
+ )
70
+
71
+ max_length = gr.Slider(
72
+ minimum=20,
73
+ maximum=200,
74
+ value=100,
75
+ step=1,
76
+ label="Max Length",
77
+ info="Controls how much text it generates",
78
+ )
79
+
80
+ generate_button = gr.Button("Generate")
81
+
82
+ with gr.Column():
83
+ output = gr.Textbox(
84
+ label="Generated Text",
85
+ lines=16,
86
+ )
87
+
88
+ generate_button.click(
89
+ fn=generate_text,
90
+ inputs=[prompt, temperature, top_p, max_length],
91
+ outputs=output,
92
+ )
93
+
94
+ gr.Examples(
95
+ examples=examples,
96
+ inputs=[prompt, temperature, top_p, max_length],
97
+ outputs=output,
98
+ fn=generate_text,
99
+ cache_examples=False,
100
+ )
101
+
102
+ if __name__ == "__main__":
103
+ demo.launch()