VethaNarayananG commited on
Commit
0f856dc
·
verified ·
1 Parent(s): 1932f44

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
4
+
5
+
6
+ def load_model():
7
+ # 'gpt2' is lightweight and works great on Hugging Face's free tier
8
+ model_name = "gpt2"
9
+
10
+ tokenizer = GPT2Tokenizer.from_pretrained(model_name)
11
+ model = GPT2LMHeadModel.from_pretrained(model_name)
12
+
13
+ tokenizer.pad_token = tokenizer.eos_token
14
+
15
+ # Hugging Face Spaces will automatically detect the runtime environment
16
+ device = "cuda" if torch.cuda.is_available() else "cpu"
17
+ model = model.to(device)
18
+
19
+ return model, tokenizer, device
20
+
21
+
22
+ # Initialize model globally
23
+ model, tokenizer, device = load_model()
24
+
25
+
26
+ def generate_text(prompt, max_length, num_return_sequences):
27
+ if not prompt.strip():
28
+ return "Please enter a valid prompt."
29
+
30
+ inputs = tokenizer(prompt, return_tensors="pt").to(device)
31
+
32
+ with torch.no_grad():
33
+ output_sequences = model.generate(
34
+ input_ids=inputs["input_ids"],
35
+ attention_mask=inputs["attention_mask"],
36
+ max_length=int(max_length),
37
+ num_return_sequences=int(num_return_sequences),
38
+ do_sample=True, # Crucial for returning multiple unique sequences
39
+ temperature=0.7,
40
+ top_k=50,
41
+ top_p=0.95,
42
+ no_repeat_ngram_size=2,
43
+ )
44
+
45
+ # Formats multiple outputs cleanly inside the single text box
46
+ final_output = ""
47
+ for i, seq in enumerate(output_sequences):
48
+ decoded_text = tokenizer.decode(seq, skip_special_tokens=True)
49
+ final_output += f"--- Sequence {i+1} ---\n{decoded_text}\n\n"
50
+
51
+ return final_output
52
+
53
+
54
+ # --- Gradio UI Layout ---
55
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
56
+ gr.Markdown("# 🤖 GPT-2 Text Generator")
57
+ gr.Markdown(
58
+ "Generate text using a pre-trained GPT-2 model. Enter a prompt, choose generation settings, and see the AI complete your text!"
59
+ )
60
+
61
+ with gr.Row():
62
+ with gr.Column(scale=1):
63
+ prompt_input = gr.Textbox(
64
+ label="Prompt",
65
+ placeholder="About Marvel Avengers...",
66
+ lines=4,
67
+ )
68
+ max_length_slider = gr.Slider(
69
+ minimum=20,
70
+ maximum=200,
71
+ value=100,
72
+ step=10,
73
+ label="Max Length of Generated Text",
74
+ )
75
+ sequences_slider = gr.Slider(
76
+ minimum=1,
77
+ maximum=5,
78
+ value=2,
79
+ step=1,
80
+ label="Number of Sequences to Return",
81
+ )
82
+
83
+ with gr.Row():
84
+ clear_btn = gr.Button("Clear")
85
+ submit_btn = gr.Button("Submit", variant="primary")
86
+
87
+ with gr.Column(scale=1):
88
+ output_text = gr.Textbox(
89
+ label="Generated Text", lines=12, interactive=False
90
+ )
91
+
92
+ # Click actions
93
+ submit_btn.click(
94
+ fn=generate_text,
95
+ inputs=[prompt_input, max_length_slider, sequences_slider],
96
+ outputs=output_text,
97
+ )
98
+
99
+ # Clear button action clears both fields
100
+ clear_btn.click(
101
+ fn=lambda: ("", ""), inputs=None, outputs=[prompt_input, output_text]
102
+ )
103
+
104
+ # Launch without share=True (Hugging Face handles the web serving natively)
105
+ demo.launch()