Spaces:
Sleeping
Sleeping
Anastasia Zimina commited on
Commit ·
fd625e2
1
Parent(s): 943c1dc
adding open-ai api
Browse files- app.py +47 -21
- requirements.txt +1 -2
app.py
CHANGED
|
@@ -3,11 +3,13 @@
|
|
| 3 |
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
-
import
|
| 7 |
-
import
|
|
|
|
| 8 |
import json
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
def load_input_fields(filepath):
|
| 13 |
"""
|
|
@@ -57,9 +59,21 @@ def format_to_markdown(objects):
|
|
| 57 |
]
|
| 58 |
return '\n'.join(formatted_list)
|
| 59 |
|
| 60 |
-
|
| 61 |
def enhance_subject(subject, details):
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
input_fields = load_input_fields("input_fields.json")
|
| 64 |
# Models
|
| 65 |
models = input_fields.get("models")
|
|
@@ -124,6 +138,7 @@ def convert_to_temp_format(temperature):
|
|
| 124 |
|
| 125 |
with gr.Blocks() as demo:
|
| 126 |
isFrog = gr.State(False)
|
|
|
|
| 127 |
gr.Markdown(
|
| 128 |
"""
|
| 129 |
# Let's combine a prompt for you!
|
|
@@ -143,14 +158,19 @@ with gr.Blocks() as demo:
|
|
| 143 |
)
|
| 144 |
|
| 145 |
def enhance_pipeline(isFrog, subject, details):
|
| 146 |
-
if isFrog:
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
| 148 |
else:
|
| 149 |
-
return [gr.Textbox(visible=
|
|
|
|
| 150 |
|
| 151 |
-
def authenticate(pwd_input,
|
| 152 |
-
if pwd_input == "
|
| 153 |
-
|
|
|
|
| 154 |
else:
|
| 155 |
raise gr.Error("You are not from our pond! Use your own LLM to add some juice to your prompt.")
|
| 156 |
|
|
@@ -165,19 +185,22 @@ with gr.Blocks() as demo:
|
|
| 165 |
with gr.Column():
|
| 166 |
pwd_input = gr.Textbox(label="Do you know the magic word?", visible=False)
|
| 167 |
confirm_btn = gr.Button(value="Confirm Pwd", visible=False)
|
| 168 |
-
enhanced_prompt = gr.TextArea(label="
|
| 169 |
|
| 170 |
enhance_button.click(
|
| 171 |
enhance_pipeline,
|
| 172 |
inputs=[isFrog, ui_subject, ui_details],
|
| 173 |
outputs=[pwd_input, confirm_btn, enhanced_prompt]
|
| 174 |
)
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
confirm_btn.click(
|
| 177 |
authenticate,
|
| 178 |
-
inputs=[pwd_input,
|
| 179 |
outputs=[enhanced_prompt,isFrog, pwd_input, confirm_btn]
|
| 180 |
-
)
|
| 181 |
|
| 182 |
gr.Markdown(
|
| 183 |
"""
|
|
@@ -293,14 +316,16 @@ with gr.Blocks() as demo:
|
|
| 293 |
with gr.Column(scale=3):
|
| 294 |
result_prompt=gr.TextArea(label="Compiled Prompt", show_copy_button=True, interactive=True)
|
| 295 |
|
| 296 |
-
def compile_prompt(photo_style, subject, weather, time, mood, details, camera_type, camera_angle, shot_type, lens_filter, lighting, model, aspect_ratio, temperature):
|
| 297 |
resulting_prompt = ""
|
| 298 |
if photo_style and photo_style != "None":
|
| 299 |
-
resulting_prompt += f"{photo_style} "
|
| 300 |
-
|
| 301 |
-
if subject:
|
| 302 |
resulting_prompt += f"{subject}, "
|
| 303 |
-
if
|
|
|
|
|
|
|
|
|
|
| 304 |
resulting_prompt += "set "
|
| 305 |
if weather and weather != "None":
|
| 306 |
resulting_prompt += f" on a {weather} day "
|
|
@@ -308,7 +333,7 @@ with gr.Blocks() as demo:
|
|
| 308 |
resulting_prompt += f"at {time} "
|
| 309 |
if mood and mood != "None":
|
| 310 |
resulting_prompt += f"with a {mood} mood. "
|
| 311 |
-
if details:
|
| 312 |
resulting_prompt += f"Include {details}. "
|
| 313 |
if camera_type and camera_type != "None":
|
| 314 |
resulting_prompt += f"Shot on {camera_type} "
|
|
@@ -331,6 +356,7 @@ with gr.Blocks() as demo:
|
|
| 331 |
inputs=[
|
| 332 |
ui_photography_style,
|
| 333 |
ui_subject,
|
|
|
|
| 334 |
ui_weather,
|
| 335 |
ui_time,
|
| 336 |
ui_mood,
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
+
import os
|
| 7 |
+
import langchain_openai
|
| 8 |
+
import langchain_core
|
| 9 |
import json
|
| 10 |
+
from langchain_openai import ChatOpenAI
|
| 11 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 12 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 13 |
|
| 14 |
def load_input_fields(filepath):
|
| 15 |
"""
|
|
|
|
| 59 |
]
|
| 60 |
return '\n'.join(formatted_list)
|
| 61 |
|
|
|
|
| 62 |
def enhance_subject(subject, details):
|
| 63 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 64 |
+
("system", "Generate a clear and concise subject, then provide additional details using descriptive language. Ensure the response is specific and avoids ambiguity or contradictions. The subject should inspire an engaging photo that tells a story. Remove any unnecessary information and don't add any punctuation at the end of the subject."),
|
| 65 |
+
("user", "The main subject is {subject} {details}.")
|
| 66 |
+
])
|
| 67 |
+
output_parser = StrOutputParser()
|
| 68 |
+
model = ChatOpenAI(model="gpt-3.5-turbo")
|
| 69 |
+
chain = ( prompt
|
| 70 |
+
| model
|
| 71 |
+
| output_parser
|
| 72 |
+
)
|
| 73 |
+
result = chain.invoke({"subject": subject, "details": details})
|
| 74 |
+
return result
|
| 75 |
+
|
| 76 |
+
|
| 77 |
input_fields = load_input_fields("input_fields.json")
|
| 78 |
# Models
|
| 79 |
models = input_fields.get("models")
|
|
|
|
| 138 |
|
| 139 |
with gr.Blocks() as demo:
|
| 140 |
isFrog = gr.State(False)
|
| 141 |
+
|
| 142 |
gr.Markdown(
|
| 143 |
"""
|
| 144 |
# Let's combine a prompt for you!
|
|
|
|
| 158 |
)
|
| 159 |
|
| 160 |
def enhance_pipeline(isFrog, subject, details):
|
| 161 |
+
if isFrog and (subject or details):
|
| 162 |
+
result = enhance_subject(subject, details)
|
| 163 |
+
return [gr.Textbox(visible=False), gr.Button(visible=False), gr.TextArea(visible=True, value=result)]
|
| 164 |
+
elif (subject or details) and not isFrog:
|
| 165 |
+
return [gr.Textbox(visible=True), gr.Button(visible=True), gr.TextArea(visible=False)]
|
| 166 |
else:
|
| 167 |
+
return [gr.Textbox(visible=False), gr.Button(visible=False), gr.TextArea(visible=False)]
|
| 168 |
+
|
| 169 |
|
| 170 |
+
def authenticate(pwd_input, subject, details):
|
| 171 |
+
if pwd_input == os.environ.get("MAGIC_WORD"):
|
| 172 |
+
result = enhance_subject(subject, details)
|
| 173 |
+
return [gr.TextArea(visible=True, value=result), True, gr.Textbox(visible=False), gr.Button(visible=False)]
|
| 174 |
else:
|
| 175 |
raise gr.Error("You are not from our pond! Use your own LLM to add some juice to your prompt.")
|
| 176 |
|
|
|
|
| 185 |
with gr.Column():
|
| 186 |
pwd_input = gr.Textbox(label="Do you know the magic word?", visible=False)
|
| 187 |
confirm_btn = gr.Button(value="Confirm Pwd", visible=False)
|
| 188 |
+
enhanced_prompt = gr.TextArea(label="Enhanced Prompt", show_copy_button=True, interactive=True, visible=False, lines=3)
|
| 189 |
|
| 190 |
enhance_button.click(
|
| 191 |
enhance_pipeline,
|
| 192 |
inputs=[isFrog, ui_subject, ui_details],
|
| 193 |
outputs=[pwd_input, confirm_btn, enhanced_prompt]
|
| 194 |
)
|
| 195 |
+
|
| 196 |
+
def clearInput():
|
| 197 |
+
return ""
|
| 198 |
|
| 199 |
confirm_btn.click(
|
| 200 |
authenticate,
|
| 201 |
+
inputs=[pwd_input, ui_subject, ui_details],
|
| 202 |
outputs=[enhanced_prompt,isFrog, pwd_input, confirm_btn]
|
| 203 |
+
).then(clearInput, outputs=pwd_input)
|
| 204 |
|
| 205 |
gr.Markdown(
|
| 206 |
"""
|
|
|
|
| 316 |
with gr.Column(scale=3):
|
| 317 |
result_prompt=gr.TextArea(label="Compiled Prompt", show_copy_button=True, interactive=True)
|
| 318 |
|
| 319 |
+
def compile_prompt(photo_style, subject, enhanced_prompt, weather, time, mood, details, camera_type, camera_angle, shot_type, lens_filter, lighting, model, aspect_ratio, temperature):
|
| 320 |
resulting_prompt = ""
|
| 321 |
if photo_style and photo_style != "None":
|
| 322 |
+
resulting_prompt += f"{photo_style} of "
|
| 323 |
+
if subject and (enhanced_prompt is None or enhanced_prompt == ""):
|
|
|
|
| 324 |
resulting_prompt += f"{subject}, "
|
| 325 |
+
if enhanced_prompt and enhanced_prompt != "":
|
| 326 |
+
resulting_prompt += f"{enhanced_prompt}, "
|
| 327 |
+
if weather != "None" or time != "None":
|
| 328 |
+
print(weather, time)
|
| 329 |
resulting_prompt += "set "
|
| 330 |
if weather and weather != "None":
|
| 331 |
resulting_prompt += f" on a {weather} day "
|
|
|
|
| 333 |
resulting_prompt += f"at {time} "
|
| 334 |
if mood and mood != "None":
|
| 335 |
resulting_prompt += f"with a {mood} mood. "
|
| 336 |
+
if details and (enhanced_prompt is None or enhanced_prompt == ""):
|
| 337 |
resulting_prompt += f"Include {details}. "
|
| 338 |
if camera_type and camera_type != "None":
|
| 339 |
resulting_prompt += f"Shot on {camera_type} "
|
|
|
|
| 356 |
inputs=[
|
| 357 |
ui_photography_style,
|
| 358 |
ui_subject,
|
| 359 |
+
enhanced_prompt,
|
| 360 |
ui_weather,
|
| 361 |
ui_time,
|
| 362 |
ui_mood,
|
requirements.txt
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
gradio>=3.0,<4.0
|
| 2 |
-
|
| 3 |
-
langchain
|
| 4 |
langchain_openai
|
|
|
|
| 1 |
gradio>=3.0,<4.0
|
| 2 |
+
langchain_core
|
|
|
|
| 3 |
langchain_openai
|