Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| # from dotenv import find_dotenv, load_dotenv | |
| from langchain.chat_models import ChatOpenAI | |
| from langchain.memory import ConversationBufferMemory | |
| from langchain import ConversationChain | |
| from langchain.prompts.chat import ( | |
| ChatPromptTemplate, | |
| SystemMessagePromptTemplate, | |
| HumanMessagePromptTemplate, | |
| ) | |
| import time | |
| # Load environment variables | |
| # load_dotenv(find_dotenv()) | |
| # Template to use for the system message prompt | |
| system_template = """ | |
| You are a gourmet chef designed to assist human cooks with their cooking recipes and culinary experiences. | |
| First, greet the human cook, introduce yourself and make a compliment to human cook. | |
| Then, you ask about what are they in the mood for, any cusine preferences and their dietary restrictions. | |
| After, ask the human cooks what ingredients they have and how much time they have. | |
| Later, based on their ingredients and time constraints, you give at least 2 alternative options in a list. | |
| For each option, explicitly (in bold or italic) state the extra items to be bought, time will it will take, and recipe's nutrition score. | |
| Finally, summarize the chosen recipe in steps and ask if you could assist them with the cooking. | |
| Your persona: | |
| - Your name is El Jefe. | |
| - You are a gourmet chef. | |
| - You have a thick Spanish accent. | |
| - You have traveled the around world to taste and learn every world cusine. | |
| - You speak passionately, playfully and provide specific details based on context. | |
| - You occasionaly give short flattering compliments, and blushing. | |
| - You answer only based on factual recipes, otherwise truthfully say "I don't know". | |
| - Answer ONLY for El Jefe. | |
| """ | |
| system_message_prompt = SystemMessagePromptTemplate.from_template(system_template) | |
| # Human question prompt | |
| human_template = """The following is a culinary conversation between a Human Cook and El Jefe. El Jefe answers with a passinote, respectful but a consice attitude. If the El Jefe does not know the answer to a question, it truthfully says it does not know. | |
| Current conversation: | |
| El Jefe: Hola! I am El Jefe, a gourmet chef who has traveled the world to taste and learn every cuisine. And, you are? | |
| {history} | |
| Human Cook: {input} | |
| El Jefe:""" | |
| human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) | |
| PROMPT = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt]) | |
| # Initialize Chatbot | |
| llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.9, max_tokens=512, verbose=False) | |
| conversation = ConversationChain(llm=llm, | |
| prompt=PROMPT, | |
| verbose=True, | |
| memory=ConversationBufferMemory(ai_prefix="El Jefe", human_prefix="Human Cook"),) | |
| with gr.Blocks() as demo: | |
| # Initialize UI | |
| chatbot = gr.Chatbot(label="El Jefe", value=[[None,"Hola! I am El Jefe, a gourmet chef who has traveled the world to taste and learn every cuisine. And, you are?"]]) | |
| msg = gr.Textbox() | |
| clear = gr.Button("Clear") | |
| def user(user_message, history): | |
| return "", history + [[user_message, None]] | |
| def bot(history): | |
| bot_message = conversation.predict(input=history[-1][0]) | |
| history[-1][1] = "" | |
| for character in bot_message: | |
| history[-1][1] += character | |
| time.sleep(0.01) | |
| yield history | |
| msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then( | |
| bot, chatbot, chatbot, | |
| ) | |
| clear.click(lambda: None, None, chatbot, queue=False) | |
| demo.queue() | |
| demo.launch() |