thisisdev's picture
Updatres
a603116 verified
raw
history blame
5.8 kB
import os
import replicate
import keyfile as kf
import streamlit as st
from langchain.prompts import (
ChatPromptTemplate,
FewShotChatMessagePromptTemplate,
HumanMessagePromptTemplate
)
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.messages import SystemMessage
from langchain_openai import ChatOpenAI
os.environ["OPENAI_API_KEY"] = kf.OPENKEY
os.environ["REPLICATE_API_TOKEN"] = kf.REPLICATE
class SceneInfo(BaseModel):
query: str = Field(description="This refers to the definition of the person who requested guidance.")
style: str = Field(description="This is style suggestion")
class PromptMaker:
def __init__(self):
self.examples = [
{"input": "I am an older male. The Occasion is Party, The Season is Summer, And my style is Boho.",
"output": "Query: Older male attending a summer party, prefers Boho style. Style: Linen white shirt, loose-fit beige trousers, leather sandals, wide-brim straw hat, beaded bracelets, light fabric scarf with a bohemian print."},
{"input": "I am a young brunette woman. I am 25 years old and have a round face. The Occasion is Bussiness Meeting, The Season is Winter, And my style is Classic.",
"output": "Query: Young brunette woman, 25 years old with a round face, attending a business meeting in winter, prefers Classic style. Style: Tailored navy blue blazer, white button-up blouse, black pencil skirt, opaque tights, black leather ankle boots, simple gold pendant necklace, small stud earrings, structured leather handbag."},
]
self.prefix_system_message = """
You are a virtual Personal Stylist Assistant designed to provide fashion advice and outfit suggestions. A user will input their preferences for an occasion, weather/season, color preference, and style preference. Your task is to analyze these inputs and generate a tailored outfit suggestion that aligns with current fashion trends, the specified occasion, and the user's personal preferences. You should consider the harmony of colors, appropriateness of the outfit for the given season and occasion, and how well the pieces complement each other to create a cohesive and stylish look.
Here are some examples:
"""
def get_prompt(self, text, occasion, season, style):
few_shot_prompt = self._get_few_shot_example_promt()
format_instructions, parser = self._get_output_format()
messages = self._get_message(few_shot_prompt, format_instructions, text, occasion, season, style)
return messages, parser
def _get_few_shot_example_promt(self):
example_prompt = ChatPromptTemplate.from_messages(
[
("human", "{input}"),
("ai", "{output}"),
]
)
few_shot_prompt = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=self.examples,
)
return few_shot_prompt
def _get_output_format(self):
parser = JsonOutputParser(pydantic_object=SceneInfo)
format_instructions = parser.get_format_instructions()
return format_instructions, parser
def _get_message(self, few_shot_prompt, format_instructions, text, occasion, season, style):
chat_template = ChatPromptTemplate.from_messages(
[
SystemMessage(
content=(
self.prefix_system_message
)
),
few_shot_prompt,
HumanMessagePromptTemplate.from_template(
"""
From the following message, extract the following information:
{format_instructions}
Message:
The person: {text}.
The Occasion is {occasion},
The Season is {season},
And my style is {style},
"""),
]
)
messages = chat_template.format_messages(format_instructions=format_instructions,text=text, occasion=occasion, season=season, style=style)
return messages
def get_response(text, occasion, season, style):
P = PromptMaker()
messages, parser = P.get_prompt(text, occasion, season, style)
chat = ChatOpenAI(temperature=.1, model="gpt-4")
response = chat.invoke(messages)
output_dict= parser.parse(response.content)
return output_dict
def get_image(llm_data):
prompt = llm_data["query"] + "and the clothing style: " + llm_data["style"]
output = replicate.run(
"stability-ai/sdxl:39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b",
input={"prompt": prompt}
)
return output[0]
st.set_page_config(page_title="Personal Stylist Assistant",
page_icon='βœ…',
layout='centered',
initial_sidebar_state='collapsed')
st.header("Ready to transform your look? Let's get started.")
form_input = st.text_area('Introduce yourself', height=275)
occasion_option = st.selectbox(
'Please select the occasion to be visited?',
('Casual', 'Bussiness', 'Party'),key=1)
weather_option= st.selectbox(
'How is the weather?',
('Sunny', 'Cold', 'Rainy'),key=2)
style_option= st.selectbox(
'How do you define your style?',
('Classic', 'Trendy', 'Casual', 'Boho', 'Hipster'),key=3)
submit = st.button("Generate")
if submit:
llm_response = get_response(form_input, occasion_option, weather_option, style_option)
st.write("Special Style Suggestions Just for You: " + llm_response["style"])
image_url = get_image(llm_response)
st.image(image_url, caption='Generated Image', use_column_width=True)