-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-modal-content-generation.py
220 lines (170 loc) · 8.56 KB
/
multi-modal-content-generation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from dotenv import load_dotenv
load_dotenv() # load all env. variables
import streamlit as st
import os
import google.generativeai as genai
import replicate
from PIL import Image
import time
# set api key
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
# REPLICATE_API_TOKEN=os.getenv("REPLICATE_API_TOKEN")
# initialize our models
vis_model = genai.GenerativeModel("gemini-pro-vision")
langugae_model = genai.GenerativeModel("gemini-pro")
# function to load gemini pro vision model and get responses
def get_gemini_response(input, image):
# if there present any text input besides image, then generate both
if (input != "") and (image != ""):
response = vis_model.generate_content([input, image])
elif (input != "") and (image == ""):
response = langugae_model.generate_content(input)
else:
response = vis_model.generate_content(image)
return response.text
def stream_data(prompt, image):
sentences = get_gemini_response(prompt, image).split(". ")
for sentence in sentences:
for word in sentence.split():
yield word + " "
time.sleep(0.02)
# initialize our streamlit app
st.set_page_config(
page_title="Multimodal Content Generation",
page_icon="⚡️",
layout="wide"
)
# give title
st.sidebar.title(":rainbow[MULTIMODAL CONTENT GENERATION]")
st.sidebar.write("Built by [jaiminjariwala](https://github.com/jaiminjariwala) 😀")
st.sidebar.divider()
# Multimodals Options
multimodal_options = st.sidebar.radio(
"**Select What To Do...**",
options=["Chat and Image Summarization", "Text 2 Image"],
index=0,
horizontal=False,
)
st.sidebar.divider()
# =======================================================================================
if multimodal_options == "Chat and Image Summarization":
# New chat button, to get the fresh chat page
if st.sidebar.button("Get **New Chat** Fresh Page"):
st.session_state["messages"] = [] # Clear chat history
st.experimental_rerun() # Trigger page reload
# create image upload option in sidebar
with st.expander("**Wanna Upload an Image?**"):
uploaded_file = st.file_uploader("Choose an image for **Image Summarizer** task...",
type=["jpg", "jpeg", "png"])
image=""
if uploaded_file is not None:
image=Image.open(uploaded_file)
st.image(image, caption="Uploaded Image.", use_column_width=True)
# Create a container to hold the entire chat history
chat_container = st.container()
# initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with chat_container:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# create input prompt (textbox)
if prompt := st.chat_input("Type here..."):
# display user message in chat message container
with chat_container:
with st.chat_message("user"):
st.markdown(prompt)
# add user message to chat history
st.session_state.messages.append({"role" : "user",
"content" : prompt})
# display assistant message in chat message container
with chat_container:
with st.chat_message("assistant"):
should_format_as_code = any(keyword in prompt.lower() for keyword in ["code", "python", "java", "javascript", "c++", "c", "program", "react", "reactjs", "node", "nodejs", "html", "css", "javascript", "js"])
if should_format_as_code:
st.code(get_gemini_response(prompt, image))
else:
st.write_stream(stream_data(prompt, image))
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": get_gemini_response(prompt, image)})
# =============================================================================
def generate_and_display_image(submitted: bool, width: int, height: int, num_outputs: int, scheduler: str, num_inference_steps: int, prompt_strength: float, prompt: str):
"""
Generates an image using the specified prompt and parameters.
"""
if REPLICATE_API_TOKEN.startswith('r8_') and submitted and prompt:
with st.status('Generating your image...', expanded=True) as status:
try:
# Only call the API if the "Submit" button was pressed
if submitted:
all_images = [] # List to store all generated images
# calling the replicate API
output = replicate.run(
"stability-ai/sdxl:39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b",
input={
"prompt": prompt,
"width": width,
"height": height,
"num_outputs": num_outputs,
"scheduler": "K_EULER",
"num_inference_steps": num_inference_steps,
"guidance_scale": 7.5,
"prompt_stregth": prompt_strength,
"negative_prompt": "the absolute worst quality, distorted features",
"refine": "expert_ensemble_refiner",
"high_noise_frac": 0.8
}
)
if output:
st.toast(
'Your image has been generated!', icon='😍')
# Save generated image to session state
st.session_state.generated_image = output
# Displaying the image
for image in st.session_state.generated_image:
with st.container():
st.image(image, caption="Generated Image ❄️",
use_column_width=True)
# Add image to the list
all_images.append(image)
# Save all generated images to session state
st.session_state.all_images = all_images
except replicate.exceptions.ReplicateError as e:
st.error(f"Error generating image: {e}")
# if not submitted
elif not prompt:
st.toast("Please input some prompt!", icon="⚠️")
def refine_output():
"""
Provides options for users to refine output parameters and returns them.
"""
with st.expander("**Refine your output if you want...**"):
width = st.number_input("Width of output image", value=1024)
height = st.number_input("Height of output image", value=1024)
num_outputs = st.slider("Number of images to output", value=1, min_value=1, max_value=4)
scheduler = st.selectbox('Scheduler', ('DDIM', 'DPMSolverMultistep', 'HeunDiscrete', 'KarrasDPM', 'K_EULER_ANCESTRAL', 'K_EULER', 'PNDM'))
num_inference_steps = st.slider(
"Number of denoising steps", value=50, min_value=1, max_value=500)
prompt_strength = st.slider(
"Prompt strength when using img2img/inpaint (1.0 corresponds to full destruction of information in image)", value=0.8, max_value=1.0, step=0.1)
# prompt input
prompt = st.text_input("Enter your prompt for the image:",
value="Dog and cat dancing on moon")
# Submit button to trigger image generation
submitted = st.button("Generate")
return submitted, width, height, num_outputs, scheduler, num_inference_steps, prompt_strength, prompt
# Prompt input and image generation logic
if multimodal_options == "Text 2 Image":
REPLICATE_API_TOKEN=st.sidebar.text_input(
"Enter your REPLICATE API TOKEN",
placeholder="Paste token here...",
type="password"
)
os.environ["REPLICATE_API_TOKEN"]=REPLICATE_API_TOKEN
if not REPLICATE_API_TOKEN.startswith('r8_'):
st.warning('Please enter your REPLICATE API KEY in **Sidebar**!', icon='⚠')
width, height, num_outputs, scheduler, num_inference_steps, prompt_strength, prompt, submitted = refine_output()
generate_and_display_image(width, height, num_outputs, scheduler, num_inference_steps, prompt_strength, prompt, submitted)
# ============================================================================