Spaces:
Sleeping
Sleeping
Commit
·
0b3905f
1
Parent(s):
a8d8832
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,63 @@
|
|
1 |
-
import streamlit as st
|
2 |
import torch
|
3 |
-
|
|
|
4 |
|
5 |
-
|
6 |
-
model_name = "distilbert-base-uncased"
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
-
model =
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
return
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
+
import streamlit as st
|
3 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
4 |
|
5 |
+
model_name = "gpt2"
|
|
|
6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
8 |
+
|
9 |
+
generator = pipeline(
|
10 |
+
"text-generation",
|
11 |
+
model=model,
|
12 |
+
tokenizer=tokenizer,
|
13 |
+
device=0 if torch.cuda.is_available() else -1
|
14 |
+
)
|
15 |
+
|
16 |
+
# Function for text generation with filtering of repeated sequences
|
17 |
+
def generate_text(prompt, section, max_length=200, temperature=0.7, top_k=50, repetition_penalty=1.2):
|
18 |
+
return generator(
|
19 |
+
f"{section} - {prompt}",
|
20 |
+
max_length=max_length,
|
21 |
+
do_sample=True,
|
22 |
+
top_k=top_k,
|
23 |
+
temperature=temperature,
|
24 |
+
repetition_penalty=repetition_penalty,
|
25 |
+
num_return_sequences=1,
|
26 |
+
eos_token_id=tokenizer.eos_token_id,
|
27 |
+
)[0]["generated_text"]
|
28 |
+
|
29 |
+
# Streamlit app
|
30 |
+
st.title("AI-Generated Blog Post")
|
31 |
+
|
32 |
+
# Keyword selection input
|
33 |
+
keywords_input = st.text_input("Step 1: Keyword Selection (Separate keywords with commas)","Artificial Intelligence")
|
34 |
+
keywords = [word.strip() for word in keywords_input.split(',')]
|
35 |
+
|
36 |
+
# Display generated content on button click
|
37 |
+
if st.button('Generate Article'):
|
38 |
+
if keywords_input:
|
39 |
+
|
40 |
+
generated_text = " ".join(keywords)
|
41 |
+
intro_text = generate_text(generated_text, "Introduction", max_length=200, temperature=0.7, top_k=50)
|
42 |
+
body_text = generate_text(generated_text, "Body", max_length=500, temperature=0.7, top_k=50)
|
43 |
+
conclusion_text = generate_text(generated_text, "Conclusion", max_length=150, temperature=0.7, top_k=50)
|
44 |
+
|
45 |
+
# Displaying the sections with adjusted parameters
|
46 |
+
st.header("Introduction")
|
47 |
+
st.write(intro_text)
|
48 |
+
|
49 |
+
st.header("Body")
|
50 |
+
st.write(body_text)
|
51 |
+
|
52 |
+
st.header("Conclusion")
|
53 |
+
st.write(conclusion_text)
|
54 |
+
else:
|
55 |
+
st.warning("Please input keywords to generate content.")
|
56 |
+
|
57 |
+
# Sidebar with instructions
|
58 |
+
st.sidebar.title("Instructions")
|
59 |
+
st.sidebar.write(
|
60 |
+
"1. Enter keywords related to the topic you want to generate content about."
|
61 |
+
"\n2. Click 'Generate Article' to create the AI-generated blog post."
|
62 |
+
"\n3. Explore the Introduction, Body, and Conclusion sections of the generated content."
|
63 |
+
)
|