Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,29 @@
|
|
1 |
-
import
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
|
4 |
+
# Load pre-trained model and tokenizer
|
5 |
+
model_name = "gpt2"
|
6 |
+
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
7 |
+
model = GPT2LMHeadModel.from_pretrained(model_name)
|
8 |
|
9 |
+
def generate_text(prompt, max_length=50):
|
10 |
+
# Encode the input prompt
|
11 |
+
inputs = tokenizer.encode(prompt, return_tensors="pt")
|
12 |
+
|
13 |
+
# Generate text
|
14 |
+
outputs = model.generate(inputs, max_length=max_length, num_return_sequences=1)
|
15 |
+
|
16 |
+
# Decode the generated text
|
17 |
+
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
18 |
+
return text
|
19 |
|
20 |
+
# Streamlit app
|
21 |
+
st.title("GPT-2 Text Generator")
|
22 |
|
23 |
+
prompt = st.text_area("Input", "Once upon a time...")
|
24 |
+
max_length = st.slider("Max Length", min_value=10, max_value=100, value=50)
|
25 |
+
|
26 |
+
if st.button("Generate"):
|
27 |
+
generated_text = generate_text(prompt, max_length)
|
28 |
+
st.subheader("Generated Text")
|
29 |
+
st.write(generated_text)
|