ochyai commited on
Commit
9d3aeb5
0 Parent(s):

Duplicate from ochyai/ochyai_food

Browse files
Files changed (6) hide show
  1. .gitattributes +34 -0
  2. README.md +13 -0
  3. app.py +137 -0
  4. constraints.md +13 -0
  5. requirements.txt +3 -0
  6. 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: ochyai/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,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ )
30
+ MODEL = "gpt-4"
31
+
32
+ def get_filetext(filename, cache={}):
33
+ if filename in cache:
34
+ # キャッシュに保存されている場合は、キャッシュからファイル内容を取得する
35
+ return cache[filename]
36
+ else:
37
+ if not os.path.exists(filename):
38
+ raise ValueError(f"ファイル '{filename}' が見つかりませんでした")
39
+ with open(filename, "r") as f:
40
+ text = f.read()
41
+ # ファイル内容をキャッシュする
42
+ cache[filename] = text
43
+ return text
44
+
45
+ class OpenAI:
46
+
47
+ @classmethod
48
+ def chat_completion(cls, prompt, start_with=""):
49
+ constraints = get_filetext(filename = "constraints.md")
50
+ template = get_filetext(filename = "template.md")
51
+
52
+ # ChatCompletion APIに渡すデータを定義する
53
+ data = {
54
+ "model": "gpt-4",
55
+ "messages": [
56
+ {"role": "system", "content": constraints}
57
+ ,{"role": "system", "content": template}
58
+ ,{"role": "assistant", "content": "Sure!"}
59
+ ,{"role": "user", "content": prompt}
60
+ ,{"role": "assistant", "content": start_with}
61
+ ],
62
+ }
63
+
64
+ # ChatCompletion APIを呼び出す
65
+ response = requests.post(
66
+ "https://api.openai.com/v1/chat/completions",
67
+ headers={
68
+ "Content-Type": "application/json",
69
+ "Authorization": f"Bearer {openai.api_key}"
70
+ },
71
+ json=data
72
+ )
73
+
74
+ # ChatCompletion APIから返された結果を取得する
75
+ result = response.json()
76
+ print(result)
77
+
78
+ content = result["choices"][0]["message"]["content"].strip()
79
+
80
+ visualize_prompt = content.split("### Prompt for Visual Expression\n\n")[1]
81
+
82
+ #print("split_content:"+split_content)
83
+
84
+ #if len(split_content) > 1:
85
+ # visualize_prompt = split_content[1]
86
+ #else:
87
+ # visualize_prompt = "vacant dish"
88
+
89
+ #print("visualize_prompt:"+visualize_prompt)
90
+
91
+ answers = stability_api.generate(
92
+ prompt=visualize_prompt,
93
+ )
94
+
95
+ for resp in answers:
96
+ for artifact in resp.artifacts:
97
+ if artifact.finish_reason == generation.FILTER:
98
+ print("NSFW")
99
+ if artifact.type == generation.ARTIFACT_IMAGE:
100
+ img = Image.open(io.BytesIO(artifact.binary))
101
+ return [content, img]
102
+
103
+ class NajiminoAI:
104
+
105
+ @classmethod
106
+ def generate_emo_prompt(cls, user_message):
107
+ template = get_filetext(filename="template.md")
108
+ prompt = f"""
109
+ {user_message}
110
+ ---
111
+ 上記を元に、下記テンプレートを埋めてください。
112
+ ---
113
+ {template}
114
+ """
115
+ return prompt
116
+
117
+ @classmethod
118
+ def generate_emo(cls, user_message):
119
+ prompt = NajiminoAI.generate_emo_prompt(user_message);
120
+ start_with = ""
121
+ result = OpenAI.chat_completion(prompt=prompt, start_with=start_with)
122
+ return result
123
+
124
+ def main():
125
+ iface = gr.Interface(fn=NajiminoAI.generate_emo,
126
+ inputs=gr.Textbox(label=inputs_label),
127
+ outputs=[gr.Textbox(label=inputs_label), gr.Image(label=visual_outputs_label)],
128
+ title=title,
129
+ description=description,
130
+ article=article,
131
+ allow_flagging='never'
132
+ )
133
+
134
+ iface.launch()
135
+
136
+ if __name__ == '__main__':
137
+ 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.