Spaces:
Paused
Paused
import os | |
import openai | |
import gradio as gr | |
openai.api_key = os.environ["OpenAI_Key"] | |
HUGGINGFACE_TOKEN = os.environ["HF_Key"] | |
# huggingface-cli login --token $HUGGINGFACE_TOKEN | |
from transformers import TFGPT2LMHeadModel, AutoTokenizer | |
tokenizer = AutoTokenizer.from_pretrained('Gracoy/ingredients_compatibility_GPT2_S') | |
model = TFGPT2LMHeadModel.from_pretrained('Gracoy/ingredients_compatibility_GPT2_S') | |
def assistant(assist_prompt): | |
response = openai.ChatCompletion.create( | |
model='gpt-3.5-turbo', | |
messages=assist_prompt, | |
temperature=0.7) | |
return response["choices"][0]["message"]["content"].strip() | |
def proper_ingredient(ingredients, meal): | |
if not ingredients: | |
return | |
assist_prompt1 = [{"role": "user", "content": f"列出三道使用{ingredients}裡食材適合當{meal}的料理,列舉即可不須詳細說明"}] | |
prompt = [ | |
{"role": "system", "content": "你是一個烹飪經驗豐富的廚師,知道哪些食材適合拿來搭配來製作平易近人且美味的料理"}, | |
{"role": "assistant", "content": assistant(assist_prompt1)}, | |
{"role": "user", "content": f"請給我三組在{ingredients}中適合一起烹飪的食材組合,不需要料理名稱只要寫出食材和為何他們適合搭配在一起就好,\ | |
列出的食材組合需至少包含一樣原本提供的食材,###輸出格式:至少一樣原本食材+新增食材:為何適合搭配在一起###"} | |
] | |
response = openai.ChatCompletion.create( | |
model='gpt-3.5-turbo', | |
messages=prompt, | |
temperature=0.7) | |
return response["choices"][0]["message"]["content"].strip() | |
def get_ingre(ingredients): | |
final_ingre = ingredients | |
return final_ingre | |
def gen_recipe(final_ingredients, flavor, meal): | |
global step | |
global round | |
global history | |
step = 9 | |
round = 0 | |
history = [] | |
if not final_ingredients: | |
return | |
assist_prompt1 = [{"role": "user", "content": f"請用三句話說明料理中的{flavor}風味,列出{flavor}風味常使用的香料及調味料,列舉即可不須詳細說明"}] | |
assist_prompt2 = [{"role": "user", "content": f"列出三樣以{final_ingredients}裡的食材烹調出適合當{meal}的{flavor}風味料理,列舉即可不須詳細說明"}] | |
prompt = [ | |
{"role": "system", "content": "你是一個烹飪經驗豐富的廚師,擅長使用手邊的食材和簡潔的烹飪步驟來製作平易近人的家庭料理"}, | |
{"role": "assistant", "content": assistant(assist_prompt1)}, | |
{"role": "assistant", "content": assistant(assist_prompt2)}, | |
{"role": "user", "content": f"請在上述料理中選擇一個,並針對此料理給我一份步驟數在{step}步以內的食譜,烹飪方式不要用炒的,食材用量要詳細,\ | |
食譜要包含用到的調味料,烹飪步驟要簡潔;###使用食材:{final_ingredients},料理風味:{flavor},餐點:{meal}\ | |
###;###輸出格式:料理名稱,使用食材,使用調味料,烹飪步驟,分成三個區塊顯示###"} | |
] | |
response = openai.ChatCompletion.create( | |
model='gpt-3.5-turbo', | |
messages=prompt, | |
temperature=0.7) | |
round += 1 | |
history.append(response["choices"][0]["message"]["content"].strip()) | |
return response["choices"][0]["message"]["content"].strip() | |
def another_recipe(final_ingredients, flavor, meal): | |
global step | |
global round | |
global history | |
if round == 0 or (not final_ingredients): | |
return | |
step = 9 | |
assist_prompt1 = [{"role": "user", "content": f"請用三句話說明料理中的{flavor}風味,列出{flavor}風味常使用的香料及調味料,列舉即可不須詳細說明"}] | |
assist_prompt2 = [{"role": "user", "content": f"你給我的{history[round-1]}料理名稱與食譜我都不喜歡,\ | |
請列出其他三樣以{final_ingredients}裡的食材烹調出適合當{meal}的{flavor}風味料理,列舉即可不須詳細說明"}] | |
prompt = [ | |
{"role": "system", "content": "你是一個烹飪經驗豐富的家庭主婦,擅長使用簡潔的烹飪步驟來製作平易近人的料理"}, | |
{"role": "assistant", "content": assistant(assist_prompt1)}, | |
{"role": "assistant", "content": assistant(assist_prompt2)}, | |
{"role": "user", "content": f"請在上述料理中給我一份料理名稱不在{history}裡且步驟數在{step}步以內的食譜,烹飪方式不要用炒的, \ | |
食材用量要詳細,食譜要包含用到的調味料,烹飪步驟的描述要簡潔;###使用食材:{final_ingredients},料理風味:{flavor},餐點:{meal}\ | |
###;###輸出格式:料理名稱,使用食材,使用調味料,烹飪步驟,分成三個區塊顯示###"} | |
] | |
response = openai.ChatCompletion.create( | |
model='gpt-3.5-turbo', | |
messages=prompt, | |
temperature=0.5) | |
round += 1 | |
history.append(response["choices"][0]["message"]["content"].strip()) | |
return response["choices"][0]["message"]["content"].strip() | |
def simpler_recipe(final_ingredients, flavor, meal): | |
global step | |
global round | |
global history | |
if round == 0 or (not final_ingredients): | |
return | |
step = step - 2 if step > 5 else step | |
assist_prompt1 = [{"role": "user", "content": f"請用三句話說明料理中的{flavor}風味,列出{flavor}風味常使用的香料及調味料,列舉即可不須詳細說明"}] | |
assist_prompt2 = [{"role": "user", "content": f"你給我的{history[round-1]}食譜做法太難了,請列出其他三樣以{final_ingredients}裡的食材,\ | |
烹調出適合當{meal}的{flavor}風味料理,列舉即可不須詳細說明"}] | |
prompt = [ | |
{"role": "system", "content": "你是一個烹飪技巧不熟練的新手廚師,只能製作出簡單常見的料理"}, | |
{"role": "assistant", "content": assistant(assist_prompt1)}, | |
{"role": "assistant", "content": assistant(assist_prompt2)}, | |
{"role": "user", "content": f"請給我一份料理名稱不在{history}裡且烹飪步驟在{step}步以內的食譜,先用一句話說明你對{history[round-1]}食譜做了哪些調整,烹飪方式不要用炒的,\ | |
食材用量要詳細,最多只能使用三種調味料,烹飪步驟要簡潔;###使用食材:{final_ingredients},料理風味:{flavor},餐點:{meal}\ | |
###;###輸出格式:料理名稱,使用食材,使用調味料,烹飪步驟,分成三個區塊顯示###"} | |
] | |
response = openai.ChatCompletion.create( | |
model='gpt-3.5-turbo', | |
messages=prompt, | |
temperature=0.7) | |
round += 1 | |
history.append(response["choices"][0]["message"]["content"].strip()) | |
return response["choices"][0]["message"]["content"].strip() | |
def compatibility_predict(ingredients): | |
response = openai.ChatCompletion.create( | |
model='gpt-3.5-turbo', | |
messages=[{"role": "user", "content": f"請把以下輸入翻譯成英文:{ingredients}"}] | |
) | |
translated = response['choices'][0]['message']['content'].strip().lower().replace('.', "") | |
prefix = 'I want to cook, please select the suitable ingredients from the following list: ' | |
prompt = prefix + translated+ '.' | |
input = tokenizer('<BOS>' + prompt, return_tensors='tf') | |
encoded_outputs = model.generate(input['input_ids'], max_length=128, num_beams=3, num_return_sequences=1, early_stopping=True) | |
mid = 'The best combinations are: ' | |
suffix = '. Enjoy your dish!' | |
res = [] | |
for output in encoded_outputs: | |
ans = tokenizer.decode(output, skip_special_tokens=True) | |
ans = ans.replace(prompt, "").strip() | |
ans = ans.replace(mid, "").strip() | |
ans = ans.replace(suffix, "").strip() | |
ans = set(ans.split(', ')) | |
res.append(list(ans)) | |
response = openai.ChatCompletion.create( | |
model='gpt-3.5-turbo', | |
messages=[{"role": "user", "content": f"請把以下輸入翻譯成繁體中文,翻譯時請以台灣(Taiwan)的習慣用詞為準:{', '.join(ans)}"}] | |
) | |
return response["choices"][0]["message"]["content"].strip() | |
with gr.Blocks(css="footer{display: none !important;}", theme=gr.themes.Soft(primary_hue="zinc", neutral_hue="sky")) as demo1: | |
gr.Markdown(""" # 今晚我想來點 """) | |
with gr.Row(): | |
with gr.Column(): | |
ingredients = gr.Textbox(label='手邊有哪些其他食材呢?') | |
#meal = gr.Textbox(label='現在要吃的是早餐,午餐,點心,晚餐,還是宵夜?') | |
meal = gr.Radio(["早餐", "午餐", "晚餐", "點心", "宵夜"], label="你現在要烹煮的是哪一餐?") | |
submit_bnt = gr.Button('請推薦食材組合給我') | |
proper_set = gr.Textbox(label='推薦的食材組合') | |
with gr.Column(): | |
final_ingredients = gr.Textbox(label="最後想使用的食材有哪些?", info="預設帶入原本輸入的食材,可再自行增減") | |
flavor = gr.Dropdown(['中式','日式','美式','韓式','義式','泰式','都可以'],label='你希望成品帶有哪些風味?', info="請從選單中任選一個") | |
submit_bnt1 = gr.Button('產生食譜吧!') | |
recipe = gr.Textbox(label='食譜') | |
with gr.Row(): | |
simpler_btn = gr.Button('太難了') | |
another_btn = gr.Button('不喜歡') | |
submit_bnt.click(fn=compatibility_predict, inputs=[ingredients], outputs=[proper_set]) # 這邊把原本的proper_ingredient換成我們model的API, 其他按鈕都沒改 | |
submit_bnt.click(fn=get_ingre, inputs=[ingredients], outputs=[final_ingredients]) | |
submit_bnt1.click(fn=gen_recipe, inputs=[final_ingredients, flavor, meal], outputs=[recipe]) | |
simpler_btn.click(fn=simpler_recipe, inputs=[final_ingredients, flavor, meal], outputs=[recipe]) | |
another_btn.click(fn=another_recipe, inputs=[final_ingredients, flavor, meal], outputs=[recipe]) | |
demo1.launch() | |