Spaces:
Sleeping
Sleeping
Commit
·
6deba4a
1
Parent(s):
4428b56
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,23 @@
|
|
1 |
-
import torch
|
2 |
import streamlit as st
|
|
|
3 |
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
4 |
from diffusers import StableDiffusionPipeline
|
5 |
-
from
|
6 |
|
7 |
-
text_model = "gpt2"
|
8 |
-
tokenizer = AutoTokenizer.from_pretrained(text_model)
|
9 |
-
model = AutoModelForCausalLM.from_pretrained(text_model)
|
10 |
|
11 |
-
image_model = "runwayml/stable-diffusion-v1-5"
|
12 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
-
|
14 |
-
generator = pipeline(
|
15 |
-
"text-generation",
|
16 |
-
model=model,
|
17 |
-
tokenizer=tokenizer,
|
18 |
-
device=0 if torch.cuda.is_available() else -1
|
19 |
-
)
|
20 |
|
21 |
def generate_text(prompt, temperature=0.7, top_k=50, repetition_penalty=1.2, max_length=None, min_length=10):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
return generator(
|
23 |
prompt,
|
24 |
max_length=max_length,
|
@@ -32,71 +31,111 @@ def generate_text(prompt, temperature=0.7, top_k=50, repetition_penalty=1.2, max
|
|
32 |
)[0]["generated_text"]
|
33 |
|
34 |
def generate_image(prompt):
|
|
|
|
|
35 |
pipe = StableDiffusionPipeline.from_pretrained(image_model, torch_dtype=torch.float32)
|
36 |
pipe = pipe.to(device)
|
37 |
image = pipe(prompt).images[0]
|
38 |
return image
|
39 |
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
title = st.text_input("Topic of the Article")
|
43 |
-
keywords_input = st.text_input("Enter Some Keywords About The Topic (Separate keywords with commas)")
|
44 |
-
keywords = [word.strip() for word in keywords_input.split(',')]
|
45 |
-
keywords.append(title)
|
46 |
|
47 |
-
if st.button('Generate Article'):
|
48 |
-
if keywords:
|
49 |
-
generated_text = " ".join(keywords)
|
50 |
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
st.markdown(
|
54 |
-
f"<h1 style='text-align: center; color: blue; font-size: 70px;'>{formatted_title}</h1>",
|
55 |
-
unsafe_allow_html=True
|
56 |
-
)
|
57 |
|
58 |
-
generated_image1 = generate_image(generated_text)
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
body_text2 = generate_text(generated_text, min_length=300, max_length=400)
|
68 |
-
conclusion_text = generate_text(generated_text, min_length=100, max_length=200)
|
69 |
|
70 |
-
st.header("Introduction")
|
71 |
-
st.write(intro_text)
|
72 |
-
modified_prompt = generated_text + ' bright'
|
73 |
-
generated_image2 = generate_image(modified_prompt)
|
74 |
|
75 |
-
|
76 |
-
|
|
|
|
|
77 |
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
st.
|
88 |
-
st.image(generated_image3, use_column_width=True)
|
89 |
|
90 |
-
|
|
|
91 |
|
92 |
-
st.header("Conclusion")
|
93 |
-
st.write(conclusion_text)
|
94 |
-
else:
|
95 |
-
st.warning("Please input keywords to generate content.")
|
96 |
|
97 |
st.sidebar.title("Instructions")
|
98 |
st.sidebar.write(
|
99 |
-
"1. Enter keywords related to the topic you want to generate content about."
|
100 |
"\n2. Click 'Generate Article' to create the AI-generated blog post."
|
101 |
"\n3. Explore the Introduction, Body, and Conclusion sections of the generated content."
|
102 |
)
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import torch
|
3 |
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
4 |
from diffusers import StableDiffusionPipeline
|
5 |
+
from nltk.corpus import wordnet
|
6 |
|
|
|
|
|
|
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def generate_text(prompt, temperature=0.7, top_k=50, repetition_penalty=1.2, max_length=None, min_length=10):
|
10 |
+
text_model = "gpt2"
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(text_model)
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(text_model)
|
13 |
+
|
14 |
+
generator = pipeline(
|
15 |
+
"text-generation",
|
16 |
+
model=model,
|
17 |
+
tokenizer=tokenizer,
|
18 |
+
device=0 if torch.cuda.is_available() else -1
|
19 |
+
)
|
20 |
+
|
21 |
return generator(
|
22 |
prompt,
|
23 |
max_length=max_length,
|
|
|
31 |
)[0]["generated_text"]
|
32 |
|
33 |
def generate_image(prompt):
|
34 |
+
image_model = "runwayml/stable-diffusion-v1-5"
|
35 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
36 |
pipe = StableDiffusionPipeline.from_pretrained(image_model, torch_dtype=torch.float32)
|
37 |
pipe = pipe.to(device)
|
38 |
image = pipe(prompt).images[0]
|
39 |
return image
|
40 |
|
41 |
+
def get_synonyms(word):
|
42 |
+
synonyms = set()
|
43 |
+
for syn in wordnet.synsets(word):
|
44 |
+
for lemma in syn.lemmas():
|
45 |
+
synonyms.add(lemma.name().replace('_', ' '))
|
46 |
+
return list(synonyms)
|
47 |
+
|
48 |
+
st.title(":black[_AI-Generated Blog Post_]")
|
49 |
|
50 |
title = st.text_input("Topic of the Article")
|
|
|
|
|
|
|
51 |
|
|
|
|
|
|
|
52 |
|
53 |
+
keywords_selection = st.selectbox('Do you want to select Keywords Manually or Automatic',['','Manually','Automatic'])
|
54 |
+
|
55 |
+
if keywords_selection == 'Manually' :
|
56 |
+
keywords_input = st.text_input("Enter Some Keywords About The Topic (Separate keywords with commas)")
|
57 |
+
keywords = [word.strip() for word in keywords_input.split(',')]
|
58 |
+
keywords.append(title)
|
59 |
+
|
60 |
+
if keywords_selection == 'Automatic' :
|
61 |
+
keywords = get_synonyms(title)
|
62 |
+
st.write(f'Your keywords Are {keywords}')
|
63 |
+
|
64 |
+
try :
|
65 |
+
if st.button('Generate Article'):
|
66 |
+
if keywords:
|
67 |
+
generated_text = " ".join(keywords)
|
68 |
+
formatted_title = title.capitalize()
|
69 |
+
|
70 |
+
st.markdown(
|
71 |
+
f"<h1 style='text-align: center; color: blue; font-size: 70px;'>{formatted_title}</h1>",
|
72 |
+
unsafe_allow_html=True
|
73 |
+
)
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
78 |
+
with col2:
|
79 |
+
generated_image1 = generate_image(generated_text)
|
80 |
+
new_image1 = generated_image1.resize((700, 500))
|
81 |
+
st.image(new_image1, use_column_width=True)
|
82 |
+
|
83 |
+
intro_text = generate_text(f'introduction about : {generated_text}', min_length=100, max_length=200)
|
84 |
+
intro_text = intro_text.replace(f"introduction about : {generated_text}", "")
|
85 |
+
st.write(intro_text.strip()) # Display the generated introduction text
|
86 |
+
|
87 |
+
body_text1 = generate_text(f'article about : {generated_text}', min_length=100, max_length=150)
|
88 |
+
body_text1 = body_text1.replace(f"article about : {generated_text}", "")
|
89 |
+
st.write(body_text1.strip()) # Display the generated introduction text
|
90 |
+
|
91 |
+
body_text2 = generate_text(f'article about : {generated_text}', min_length=200, max_length=300)
|
92 |
+
body_text2 = body_text2.replace(f"{generated_text}", "")
|
93 |
+
st.write(body_text2.strip()) # Display the generated introduction text
|
94 |
+
|
95 |
+
conclusion_text = generate_text(f'conclusion about : {generated_text}', min_length=100, max_length=200)
|
96 |
+
conclusion_text = conclusion_text.replace(f"conclusion about : {generated_text}", "")
|
97 |
+
st.write(conclusion_text.strip()) # Display the generated introduction text
|
98 |
|
|
|
|
|
|
|
|
|
99 |
|
|
|
100 |
|
101 |
+
st.subheader("Introduction")
|
102 |
+
st.write(intro_text)
|
103 |
+
modified_prompt = generated_text + 'bright'
|
104 |
+
generated_image2 = generate_image(modified_prompt)
|
105 |
|
106 |
+
new_image2 = generated_image2.resize((700, 300))
|
107 |
+
st.image(new_image2, use_column_width=True)
|
|
|
|
|
108 |
|
|
|
|
|
|
|
|
|
109 |
|
110 |
+
col1, col2 = st.columns(2)
|
111 |
+
with col1:
|
112 |
+
st.subheader("Body")
|
113 |
+
st.write(body_text1)
|
114 |
|
115 |
+
with col2:
|
116 |
+
modified_prompt2 = generated_text + 'shade'
|
117 |
+
generated_image3 = generate_image(modified_prompt2)
|
118 |
+
st.markdown("<br><br><br><br>", unsafe_allow_html=True)
|
119 |
+
st.image(generated_image3, use_column_width=True)
|
120 |
|
121 |
+
st.write(body_text2)
|
122 |
+
modified_prompt3 = generated_text + title
|
123 |
+
generated_image4 = generate_image(modified_prompt3)
|
124 |
+
new_image3 = generated_image4.resize((700, 300))
|
125 |
+
st.image(new_image3, use_column_width=True)
|
126 |
|
127 |
+
st.subheader("Conclusion")
|
128 |
+
st.write(conclusion_text)
|
129 |
+
else:
|
130 |
+
st.warning("Please input keywords to generate content.")
|
|
|
131 |
|
132 |
+
except :
|
133 |
+
st.warning('Please Enter Title and Keywords')
|
134 |
|
|
|
|
|
|
|
|
|
135 |
|
136 |
st.sidebar.title("Instructions")
|
137 |
st.sidebar.write(
|
138 |
+
"1. Enter title and keywords related to the topic you want to generate content about."
|
139 |
"\n2. Click 'Generate Article' to create the AI-generated blog post."
|
140 |
"\n3. Explore the Introduction, Body, and Conclusion sections of the generated content."
|
141 |
)
|