shigeru saito commited on
Commit
3d9d048
·
1 Parent(s): 168b94f

リクエストごとにインスタンス化、出力メッセージをjsonからフリーテキストに変更、functionsにdescription, examplesを追加、テストコード追加

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +149 -27
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
app.py CHANGED
@@ -5,6 +5,7 @@ import os
5
  import fileinput
6
  from dotenv import load_dotenv
7
  import io
 
8
  import json
9
  from PIL import Image
10
  from stability_sdk import client
@@ -48,6 +49,24 @@ def get_filetext(filename, cache={}):
48
  cache[filename] = text
49
  return text
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  class OpenAI:
52
 
53
  @classmethod
@@ -110,18 +129,20 @@ class OpenAI:
110
  )
111
 
112
  # ChatCompletion APIから返された結果を取得する
113
- content = json.dumps(response.choices[0].message, indent=2)
114
- print(content)
115
 
116
- return [content, None]
117
 
118
  class NajiminoAI:
119
-
120
- @classmethod
121
- def generate_emo_prompt(cls, user_message):
 
 
122
  template = get_filetext(filename="template.md")
123
  prompt = f"""
124
- {user_message}
125
  ---
126
  上記を元に、下記テンプレートを埋めてください。
127
  ---
@@ -129,22 +150,43 @@ class NajiminoAI:
129
  """
130
  return prompt
131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  @classmethod
133
- def generate_emo(cls, user_message):
134
- # prompt = NajiminoAI.generate_emo_prompt(user_message);
135
- # start_with = ""
136
- # result = OpenAI.chat_completion(prompt=prompt, start_with=start_with)
137
- # result = OpenAI.chat_completion(prompt=user_message, start_with=start_with)
138
 
 
 
 
 
 
 
 
139
  constraints = get_filetext(filename = "constraints.md")
140
 
141
  messages = [
142
  {"role": "system", "content": constraints}
143
  ,{"role": "user", "content": user_message}
144
  ]
 
145
  functions = [
146
  {
147
- "name": "set_recipe",
148
  "description": "どんな料理か教えてくれれば,新しいレシピを考えます",
149
  "parameters": {
150
  "type": "object",
@@ -154,11 +196,11 @@ class NajiminoAI:
154
  "lang",
155
  "title",
156
  "description",
 
157
  "instruction",
158
  "comment_feelings_taste",
159
- # "explanation_to_blind_person",
160
- "explanation_to_blind_person"
161
- # "prompt_for_visual_expression_in_en"
162
  ],
163
  "properties": {
164
  "lang": {
@@ -172,47 +214,107 @@ class NajiminoAI:
172
  "title": {
173
  "type": "string",
174
  "default": "",
175
- "title": "The title Schema",
 
176
  "examples": [
177
- "グルテンフリーの香ばしいサバのお好み焼き"
178
  ]
179
  },
180
  "description": {
181
  "type": "string",
182
  "default": "",
183
- "title": "The description Schema"
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  },
185
  "instruction": {
186
  "type": "string",
187
  "default": "",
188
- "title": "The instruction Schema"
 
 
 
 
189
  },
190
  "comment_feelings_taste": {
191
  "type": "string",
192
  "default": "",
193
- "title": "The Schema of comment feelings taste Schema"
 
 
 
 
194
  },
195
  "explanation_to_blind_person": {
196
  "type": "string",
197
  "default": "",
198
- "title": "The Schema of Explanation to blind person"
 
 
 
 
199
  },
200
  "prompt_for_visual_expression_in_en": {
201
  "type": "string",
202
  "default": "",
203
- "title": "The Schema of prompt for visual expression in English"
 
 
 
204
  }
205
  }
206
  }
207
  }
208
  ]
209
 
210
- result = OpenAI.chat_completion_with_function(prompt=user_message, messages=messages, functions=functions)
211
 
212
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
  def main():
215
- iface = gr.Interface(fn=NajiminoAI.generate_emo,
216
  inputs=gr.Textbox(label=inputs_label),
217
  outputs=[gr.Textbox(label=inputs_label), gr.Image(label=visual_outputs_label)],
218
  title=title,
@@ -224,4 +326,24 @@ def main():
224
  iface.launch()
225
 
226
  if __name__ == '__main__':
227
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  import fileinput
6
  from dotenv import load_dotenv
7
  import io
8
+ import sys
9
  import json
10
  from PIL import Image
11
  from stability_sdk import client
 
49
  cache[filename] = text
50
  return text
51
 
52
+ class StabilityAI:
53
+ @classmethod
54
+ def generate_image(cls, visualize_prompt):
55
+
56
+ print("visualize_prompt:"+visualize_prompt)
57
+
58
+ answers = stability_api.generate(
59
+ prompt=visualize_prompt,
60
+ )
61
+
62
+ for resp in answers:
63
+ for artifact in resp.artifacts:
64
+ if artifact.finish_reason == generation.FILTER:
65
+ print("NSFW")
66
+ if artifact.type == generation.ARTIFACT_IMAGE:
67
+ img = Image.open(io.BytesIO(artifact.binary))
68
+ return img
69
+
70
  class OpenAI:
71
 
72
  @classmethod
 
129
  )
130
 
131
  # ChatCompletion APIから返された結果を取得する
132
+ message = response.choices[0].message
133
+ print(json.dumps(message, indent=2))
134
 
135
+ return message
136
 
137
  class NajiminoAI:
138
+
139
+ def __init__(self, user_message):
140
+ self.user_message = user_message
141
+
142
+ def generate_recipe_prompt(self):
143
  template = get_filetext(filename="template.md")
144
  prompt = f"""
145
+ {self.user_message}
146
  ---
147
  上記を元に、下記テンプレートを埋めてください。
148
  ---
 
150
  """
151
  return prompt
152
 
153
+ def format_recipe(self, lang, title, description, ingredients, instruction, comment_feelings_taste, explanation_to_blind_person, prompt_for_visual_expression_in_en):
154
+
155
+ debug_message = f"""
156
+ lang: {lang}
157
+ title: {title}
158
+ description: {description}
159
+ ingredients: {ingredients}
160
+ instruction: {instruction}
161
+ comment_feelings_taste: {comment_feelings_taste}
162
+ explanation_to_blind_person: {explanation_to_blind_person}
163
+ prompt_for_visual_expression_in_en: {prompt_for_visual_expression_in_en}
164
+ """
165
+
166
+ print(debug_message)
167
+
168
+ return debug_message
169
+
170
  @classmethod
171
+ def generate(cls, user_message):
 
 
 
 
172
 
173
+ najiminoai = NajiminoAI(user_message)
174
+
175
+ return najiminoai.generate_recipe()
176
+
177
+ def generate_recipe(self):
178
+
179
+ user_message = self.user_message
180
  constraints = get_filetext(filename = "constraints.md")
181
 
182
  messages = [
183
  {"role": "system", "content": constraints}
184
  ,{"role": "user", "content": user_message}
185
  ]
186
+
187
  functions = [
188
  {
189
+ "name": "format_recipe",
190
  "description": "どんな料理か教えてくれれば,新しいレシピを考えます",
191
  "parameters": {
192
  "type": "object",
 
196
  "lang",
197
  "title",
198
  "description",
199
+ "ingredients",
200
  "instruction",
201
  "comment_feelings_taste",
202
+ "explanation_to_blind_person",
203
+ "prompt_for_visual_expression_in_en"
 
204
  ],
205
  "properties": {
206
  "lang": {
 
214
  "title": {
215
  "type": "string",
216
  "default": "",
217
+ "title": "Title of New Recipe.",
218
+ "description": "Write your title of new recipe.",
219
  "examples": [
220
+ "グルテンフリーサバのお好み焼き"
221
  ]
222
  },
223
  "description": {
224
  "type": "string",
225
  "default": "",
226
+ "title": "Your New Recipe",
227
+ "description": "Write new recipe and brainstorm and work out the details of every aspect of the new recipe.",
228
+ "examples": [
229
+ "サバを使ったお好み焼きのレシピです。グルテンフリー仕様で作られているので、小麦粉を使わずに作ることができます。"
230
+ ]
231
+ },
232
+ "ingredients": {
233
+ "type": "string",
234
+ "default": "",
235
+ "title": "Your Ingredients",
236
+ "description": "Brainstorm the ingredients needed to cook a new recipe and come up with all the ingredients for the new recipe.",
237
+ "examples": [
238
+ "・サバのフィレ 200g\n・卵 2個\n・キャベツ 1/4個\n・もやし 50g\n・紅しょうが(刻んだもの) 2片\n・ネギ(小口切り) 適量\n・酒大さじ2\n・しょうゆ 大さじ2\n・だし汁 100ml\n・片栗粉 大さじ2\n・サラダ油 お好みで"
239
+ ]
240
  },
241
  "instruction": {
242
  "type": "string",
243
  "default": "",
244
+ "title": "Your Instruction",
245
+ "description": "Instruct them to cook a new recipe, brainstorming and working out every aspect and detail of the new recipe.",
246
+ "examples": [
247
+ "1. キャベツは粗みじん切りにし、もやしは石づきを取っておく。\n2. ボウルに卵を割り入れ、よく混ぜる。酒、しょうゆ、だし汁を加えてさらに混ぜる。\n3. キャベツ、もやし、紅しょうが、ネギを卵液に加え、さらに混ぜる。\n4. サバのフィレを食べやすい大きさに切り、片栗粉をまぶす。\n5. フライパンにサラダ油を熱し、サバを並べ入れる。両面をこんがり焼く。\n6. サバの上に卵液を流し入れ、蓋をして約5分ほど蒸し焼きにする。\n7. ひっくり返して、もう片面も蓋をして約5分ほど蒸し焼きにする。\n8. お好み焼きを取り出し、お好みでソースやマヨネーズをかけて完成です。"
248
+ ]
249
  },
250
  "comment_feelings_taste": {
251
  "type": "string",
252
  "default": "",
253
+ "title": "Your Comment and Feelings, taste of new recipe.",
254
+ "description": "Review commnet of new recipe and brainstorm every point of new recipe to fill the details.",
255
+ "examples": [
256
+ "このグルテンフリーサバのお好み焼きは、サバの旨味とキャベツの甘みが絶妙にマッチしています。表面はサクサク、中はもちもちとした食感で、一度食べたらやみつきになる美味しさです。"
257
+ ]
258
  },
259
  "explanation_to_blind_person": {
260
  "type": "string",
261
  "default": "",
262
+ "title": "Your Explanation to Blind Person",
263
+ "description": "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.",
264
+ "examples": [
265
+ "このグルテンフリーサバのお好み焼きは、サバのフィレとキャベツを主な材料としています。まず、キャベツを細かく刻んでおきます。次に、卵をボウルに割り入れ、酒、しょうゆ、だし汁と一緒によく混ぜます。その後、刻んだキャベツ、もやし、紅しょうが、ネギを卵液に加えて混ぜます。サバのフィレは食べやすい大きさに切り、片栗粉をまぶしてから焼きます。フライパンにサラダ油を熱し、サバを焼きます。その上に卵液を流し入れ、蓋をして約5分蒸し焼きにします。ひっくり返して、もう片面も蓋をして約5分蒸し焼きにします。最後にお好み焼きを取り出し、ソースやマヨネーズをかけて完成です。"
266
+ ]
267
  },
268
  "prompt_for_visual_expression_in_en": {
269
  "type": "string",
270
  "default": "",
271
+ "title": "The Schema of prompt for visual expression in English",
272
+ "examples": [
273
+ "Imagine a delicious gluten-free okonomiyaki with mackerel. The okonomiyaki is crispy on the outside and chewy on the inside. It is topped with savory sauce and creamy mayonnaise, creating a mouthwatering visual. The dish is garnished with finely chopped green onions and red pickled ginger, adding a pop of color. The mackerel fillets are beautifully grilled and placed on top of the okonomiyaki, adding a touch of elegance. The dish is served on a traditional Japanese plate, completing the visual presentation."
274
+ ]
275
  }
276
  }
277
  }
278
  }
279
  ]
280
 
281
+ message = OpenAI.chat_completion_with_function(prompt=user_message, messages=messages, functions=functions)
282
 
283
+ # resultからfunction_callを取り出す
284
+ # message = result["function_call"]
285
+ print(json.dumps(message, indent=2))
286
+
287
+ if message.get("function_call"):
288
+ function_name = message["function_call"]["name"]
289
+
290
+ args = json.loads(message["function_call"]["arguments"])
291
+
292
+ lang=args.get("lang")
293
+ title=args.get("title")
294
+ description=args.get("description")
295
+ ingredients=args.get("ingredients")
296
+ instruction=args.get("instruction")
297
+ comment_feelings_taste=args.get("comment_feelings_taste")
298
+ explanation_to_blind_person=args.get("explanation_to_blind_person")
299
+ prompt_for_visual_expression_in_en=args.get("prompt_for_visual_expression_in_en")
300
+
301
+ function_response = self.format_recipe(
302
+ lang=lang,
303
+ title=title,
304
+ description=description,
305
+ ingredients=ingredients,
306
+ instruction=instruction,
307
+ comment_feelings_taste=comment_feelings_taste,
308
+ explanation_to_blind_person=explanation_to_blind_person,
309
+ prompt_for_visual_expression_in_en=prompt_for_visual_expression_in_en
310
+ )
311
+
312
+ answers = StabilityAI.generate_image(prompt_for_visual_expression_in_en)
313
+
314
+ return [function_response, answers]
315
 
316
  def main():
317
+ iface = gr.Interface(fn=NajiminoAI.generate,
318
  inputs=gr.Textbox(label=inputs_label),
319
  outputs=[gr.Textbox(label=inputs_label), gr.Image(label=visual_outputs_label)],
320
  title=title,
 
326
  iface.launch()
327
 
328
  if __name__ == '__main__':
329
+ function = ''
330
+ if len(sys.argv) > 1:
331
+ function = sys.argv[1]
332
+
333
+ if function == 'generate':
334
+ NajiminoAI.generate("グルテンフリーの香ばしいサバのお好み焼き")
335
+
336
+ elif function == 'generate_image':
337
+ answers = StabilityAI.generate_image("Imagine a delicious gluten-free okonomiyaki with mackerel. The okonomiyaki is crispy on the outside and chewy on the inside. It is topped with savory sauce and creamy mayonnaise, creating a mouthwatering visual. The dish is garnished with finely chopped green onions and red pickled ginger, adding a pop of color. The mackerel fillets are beautifully grilled and placed on top of the okonomiyaki, adding a touch of elegance. The dish is served on a traditional Japanese plate, completing the visual presentation.")
338
+ print(answers)
339
+ # <PIL.PngImagePlugin.PngImageFile image mode=RGB size=512x512 at 0x139900430>
340
+
341
+ import PIL
342
+
343
+ # answersが何のクラス確認する
344
+ if type(answers) == PIL.PngImagePlugin.PngImageFile:
345
+ #save image
346
+ answers.save("image.png")
347
+
348
+ else:
349
+ main()