Spaces:
Sleeping
Sleeping
shigel
commited on
Commit
•
728fc97
0
Parent(s):
Duplicate from shigel/ochyai_food
Browse files- .gitattributes +34 -0
- README.md +13 -0
- app.py +139 -0
- constraints.md +13 -0
- requirements.txt +3 -0
- template.md +23 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: ochyai_food
|
3 |
+
emoji: 🍛
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: red
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.19.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: shigel/ochyai_food
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import requests
|
4 |
+
import os
|
5 |
+
import fileinput
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
import io
|
8 |
+
from PIL import Image
|
9 |
+
from stability_sdk import client
|
10 |
+
import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
|
11 |
+
|
12 |
+
title="ochyAI recipe generator"
|
13 |
+
inputs_label="どんな料理か教えてくれれば,新しいレシピを考えます"
|
14 |
+
outputs_label="ochyAIが返信をします"
|
15 |
+
visual_outputs_label="料理のイメージ"
|
16 |
+
description="""
|
17 |
+
- ※入出力の文字数は最大1000文字程度までを目安に入力してください。解答に120秒くらいかかります.エラーが出た場合はログを開いてエラーメッセージを送ってくれるとochyAIが喜びます
|
18 |
+
"""
|
19 |
+
|
20 |
+
article = """
|
21 |
+
"""
|
22 |
+
|
23 |
+
load_dotenv()
|
24 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
25 |
+
os.environ['STABILITY_HOST'] = 'grpc.stability.ai:443'
|
26 |
+
stability_api = client.StabilityInference(
|
27 |
+
key=os.getenv('STABILITY_KEY'),
|
28 |
+
verbose=True,
|
29 |
+
engine="stable-diffusion-xl-beta-v2-2-2",
|
30 |
+
)
|
31 |
+
# MODEL = "gpt-4"
|
32 |
+
MODEL = "gpt-3.5-turbo-16k"
|
33 |
+
|
34 |
+
def get_filetext(filename, cache={}):
|
35 |
+
if filename in cache:
|
36 |
+
# キャッシュに保存されている場合は、キャッシュからファイル内容を取得する
|
37 |
+
return cache[filename]
|
38 |
+
else:
|
39 |
+
if not os.path.exists(filename):
|
40 |
+
raise ValueError(f"ファイル '{filename}' が見つかりませんでした")
|
41 |
+
with open(filename, "r") as f:
|
42 |
+
text = f.read()
|
43 |
+
# ファイル内容をキャッシュする
|
44 |
+
cache[filename] = text
|
45 |
+
return text
|
46 |
+
|
47 |
+
class OpenAI:
|
48 |
+
|
49 |
+
@classmethod
|
50 |
+
def chat_completion(cls, prompt, start_with=""):
|
51 |
+
constraints = get_filetext(filename = "constraints.md")
|
52 |
+
template = get_filetext(filename = "template.md")
|
53 |
+
|
54 |
+
# ChatCompletion APIに渡すデータを定義する
|
55 |
+
data = {
|
56 |
+
"model": MODEL,
|
57 |
+
"messages": [
|
58 |
+
{"role": "system", "content": constraints}
|
59 |
+
,{"role": "system", "content": template}
|
60 |
+
,{"role": "assistant", "content": "Sure!"}
|
61 |
+
,{"role": "user", "content": prompt}
|
62 |
+
,{"role": "assistant", "content": start_with}
|
63 |
+
],
|
64 |
+
}
|
65 |
+
|
66 |
+
# ChatCompletion APIを呼び出す
|
67 |
+
response = requests.post(
|
68 |
+
"https://api.openai.com/v1/chat/completions",
|
69 |
+
headers={
|
70 |
+
"Content-Type": "application/json",
|
71 |
+
"Authorization": f"Bearer {openai.api_key}"
|
72 |
+
},
|
73 |
+
json=data
|
74 |
+
)
|
75 |
+
|
76 |
+
# ChatCompletion APIから返された結果を取得する
|
77 |
+
result = response.json()
|
78 |
+
print(result)
|
79 |
+
|
80 |
+
content = result["choices"][0]["message"]["content"].strip()
|
81 |
+
|
82 |
+
visualize_prompt = content.split("### Prompt for Visual Expression\n\n")[1]
|
83 |
+
|
84 |
+
#print("split_content:"+split_content)
|
85 |
+
|
86 |
+
#if len(split_content) > 1:
|
87 |
+
# visualize_prompt = split_content[1]
|
88 |
+
#else:
|
89 |
+
# visualize_prompt = "vacant dish"
|
90 |
+
|
91 |
+
#print("visualize_prompt:"+visualize_prompt)
|
92 |
+
|
93 |
+
answers = stability_api.generate(
|
94 |
+
prompt=visualize_prompt,
|
95 |
+
)
|
96 |
+
|
97 |
+
for resp in answers:
|
98 |
+
for artifact in resp.artifacts:
|
99 |
+
if artifact.finish_reason == generation.FILTER:
|
100 |
+
print("NSFW")
|
101 |
+
if artifact.type == generation.ARTIFACT_IMAGE:
|
102 |
+
img = Image.open(io.BytesIO(artifact.binary))
|
103 |
+
return [content, img]
|
104 |
+
|
105 |
+
class NajiminoAI:
|
106 |
+
|
107 |
+
@classmethod
|
108 |
+
def generate_emo_prompt(cls, user_message):
|
109 |
+
template = get_filetext(filename="template.md")
|
110 |
+
prompt = f"""
|
111 |
+
{user_message}
|
112 |
+
---
|
113 |
+
上記を元に、下記テンプレートを埋めてください。
|
114 |
+
---
|
115 |
+
{template}
|
116 |
+
"""
|
117 |
+
return prompt
|
118 |
+
|
119 |
+
@classmethod
|
120 |
+
def generate_emo(cls, user_message):
|
121 |
+
prompt = NajiminoAI.generate_emo_prompt(user_message);
|
122 |
+
start_with = ""
|
123 |
+
result = OpenAI.chat_completion(prompt=prompt, start_with=start_with)
|
124 |
+
return result
|
125 |
+
|
126 |
+
def main():
|
127 |
+
iface = gr.Interface(fn=NajiminoAI.generate_emo,
|
128 |
+
inputs=gr.Textbox(label=inputs_label),
|
129 |
+
outputs=[gr.Textbox(label=inputs_label), gr.Image(label=visual_outputs_label)],
|
130 |
+
title=title,
|
131 |
+
description=description,
|
132 |
+
article=article,
|
133 |
+
allow_flagging='never'
|
134 |
+
)
|
135 |
+
|
136 |
+
iface.launch()
|
137 |
+
|
138 |
+
if __name__ == '__main__':
|
139 |
+
main()
|
constraints.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#constraints
|
2 |
+
|
3 |
+
ALOs(Food):
|
4 |
+
- Ingredients: Identify, Store, Measure, Types, Seasonality, Allergens, Freshness, Quantity
|
5 |
+
- Recipes: Follow, Create, Modify, Types, Cuisine, DietaryRestrictions, Complexity, ServingSize
|
6 |
+
- Cuisine: Appreciate, Discover, Compare, Regions, Traditions, PopularDishes, Authenticity, Popularity
|
7 |
+
- NutritionalValue: Calculate, Optimize, Balance, Macronutrients, Micronutrients, Calories, Healthiness, Satisfaction
|
8 |
+
- PreparationMethods: Master, Improve, Teach, Techniques, Tools, CookingTemperatures, Proficiency, Efficiency
|
9 |
+
- MealTypes: Plan, Organize, Pair, Breakfast, Lunch, Dinner, Snacks, Dessert, Variety, Enjoyment
|
10 |
+
|
11 |
+
Execute ALO(Food) to generate novel, state of the art completely new recipe, instruction for new food, possible voice from the people who ate new recipe, visual representation of dish by words for generative AI that includes photgraphic settings of key image of dish, according to user input food domains and cheracteristics. Generate details as far as you can by brainstorming to fullfill all parameters. Implement linguistic adjustments to prevent and rectify errors.
|
12 |
+
|
13 |
+
#templates
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
openai>=0.27.0
|
2 |
+
python-dotenv
|
3 |
+
stability-sdk
|
template.md
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### Title of New Recipe
|
2 |
+
|
3 |
+
Please write your title of new recipe Here.
|
4 |
+
|
5 |
+
### Your New Recipe Here
|
6 |
+
|
7 |
+
Please write new recipe and brainstorm every point of new recipe to fill the details.
|
8 |
+
|
9 |
+
### Your Instruction Here
|
10 |
+
|
11 |
+
Please write your instruction to cook the dish of new recipe and brainstorm every point of new recipe to fill the details.
|
12 |
+
|
13 |
+
### Your Comment and Feelings, taste of new recipe
|
14 |
+
|
15 |
+
Please write review commnet of new recipe here and brainstorm every point of new recipe to fill the details.
|
16 |
+
|
17 |
+
### Your Explanation to Blind Person
|
18 |
+
|
19 |
+
Please write review commnet of new recipe here to explain to the blind people more concretely in detail. Please brainstorm every point of new recipe to fill the details.
|
20 |
+
|
21 |
+
### Prompt for Visual Expression
|
22 |
+
|
23 |
+
Please write prompt for visual expression in Generative AI for image the visual of the new recipe and brainstorm every point of new recipe to fill the details.
|