Spaces:
Runtime error
Runtime error
shigeru saito commited on
Commit ·
aad7469
1
Parent(s): d1165c1
html形式で描画するように修正
Browse files- app.py +46 -20
- schema.json +5 -7
- template.md +1 -1
app.py
CHANGED
|
@@ -2,14 +2,15 @@ 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 |
import sys
|
| 9 |
import json
|
| 10 |
-
|
|
|
|
| 11 |
from stability_sdk import client
|
| 12 |
import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
|
|
|
|
| 13 |
|
| 14 |
title="najimino AI recipe generator"
|
| 15 |
inputs_label="どんな料理か教えてくれれば,新しいレシピを考えます"
|
|
@@ -67,10 +68,11 @@ class StabilityAI:
|
|
| 67 |
|
| 68 |
for resp in answers:
|
| 69 |
for artifact in resp.artifacts:
|
|
|
|
| 70 |
if artifact.finish_reason == generation.FILTER:
|
| 71 |
print("NSFW")
|
| 72 |
if artifact.type == generation.ARTIFACT_IMAGE:
|
| 73 |
-
img = Image.open(io.BytesIO(artifact.binary))
|
| 74 |
return img
|
| 75 |
|
| 76 |
class OpenAI:
|
|
@@ -92,6 +94,8 @@ class OpenAI:
|
|
| 92 |
],
|
| 93 |
}
|
| 94 |
|
|
|
|
|
|
|
| 95 |
# ChatCompletion APIを呼び出す
|
| 96 |
response = requests.post(
|
| 97 |
"https://api.openai.com/v1/chat/completions",
|
|
@@ -101,6 +105,7 @@ class OpenAI:
|
|
| 101 |
},
|
| 102 |
json=data
|
| 103 |
)
|
|
|
|
| 104 |
|
| 105 |
# ChatCompletion APIから返された結果を取得する
|
| 106 |
result = response.json()
|
|
@@ -127,16 +132,20 @@ class OpenAI:
|
|
| 127 |
def chat_completion_with_function(cls, prompt, messages, functions):
|
| 128 |
print("prompt:"+prompt)
|
| 129 |
|
|
|
|
|
|
|
|
|
|
| 130 |
response = openai.ChatCompletion.create(
|
| 131 |
model=MODEL,
|
| 132 |
messages=messages,
|
| 133 |
functions=functions,
|
| 134 |
function_call="auto"
|
| 135 |
)
|
|
|
|
| 136 |
|
| 137 |
# ChatCompletion APIから返された結果を取得する
|
| 138 |
message = response.choices[0].message
|
| 139 |
-
print(json.dumps(message, indent=2))
|
| 140 |
|
| 141 |
return message
|
| 142 |
|
|
@@ -156,7 +165,7 @@ class NajiminoAI:
|
|
| 156 |
"""
|
| 157 |
return prompt
|
| 158 |
|
| 159 |
-
def format_recipe(self, lang, title, description, ingredients, instruction, comment_feelings_taste, explanation_to_blind_person,
|
| 160 |
|
| 161 |
template = get_filetext(filename = "template.md")
|
| 162 |
debug_message = template.format(
|
|
@@ -167,7 +176,7 @@ class NajiminoAI:
|
|
| 167 |
instruction=instruction,
|
| 168 |
comment_feelings_taste=comment_feelings_taste,
|
| 169 |
explanation_to_blind_person=explanation_to_blind_person,
|
| 170 |
-
|
| 171 |
)
|
| 172 |
|
| 173 |
print("debug_message: "+debug_message)
|
|
@@ -197,8 +206,10 @@ class NajiminoAI:
|
|
| 197 |
|
| 198 |
# resultからfunction_callを取り出す
|
| 199 |
# message = result["function_call"]
|
| 200 |
-
print(json.dumps(message, indent=2))
|
| 201 |
|
|
|
|
|
|
|
| 202 |
if message.get("function_call"):
|
| 203 |
function_name = message["function_call"]["name"]
|
| 204 |
|
|
@@ -213,6 +224,17 @@ class NajiminoAI:
|
|
| 213 |
explanation_to_blind_person=args.get("explanation_to_blind_person")
|
| 214 |
prompt_for_visual_expression_in_en=args.get("prompt_for_visual_expression_in_en")
|
| 215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
function_response = self.format_recipe(
|
| 217 |
lang=lang,
|
| 218 |
title=title,
|
|
@@ -221,17 +243,24 @@ class NajiminoAI:
|
|
| 221 |
instruction=instruction,
|
| 222 |
comment_feelings_taste=comment_feelings_taste,
|
| 223 |
explanation_to_blind_person=explanation_to_blind_person,
|
| 224 |
-
|
| 225 |
)
|
| 226 |
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
-
return [function_response, answers]
|
| 230 |
-
|
| 231 |
def main():
|
| 232 |
iface = gr.Interface(fn=NajiminoAI.generate,
|
| 233 |
inputs=gr.Textbox(label=inputs_label),
|
| 234 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
| 235 |
title=title,
|
| 236 |
description=description,
|
| 237 |
article=article,
|
|
@@ -249,16 +278,13 @@ if __name__ == '__main__':
|
|
| 249 |
NajiminoAI.generate("グルテンフリーの香ばしいサバのお好み焼き")
|
| 250 |
|
| 251 |
elif function == 'generate_image':
|
| 252 |
-
|
| 253 |
-
print(
|
| 254 |
-
# <PIL.PngImagePlugin.PngImageFile image mode=RGB size=512x512 at 0x139900430>
|
| 255 |
-
|
| 256 |
-
import PIL
|
| 257 |
|
| 258 |
-
#
|
| 259 |
-
if type(
|
| 260 |
#save image
|
| 261 |
-
|
| 262 |
|
| 263 |
else:
|
| 264 |
main()
|
|
|
|
| 2 |
import openai
|
| 3 |
import requests
|
| 4 |
import os
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
import io
|
| 7 |
import sys
|
| 8 |
import json
|
| 9 |
+
import PIL
|
| 10 |
+
import time
|
| 11 |
from stability_sdk import client
|
| 12 |
import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
|
| 13 |
+
import markdown2
|
| 14 |
|
| 15 |
title="najimino AI recipe generator"
|
| 16 |
inputs_label="どんな料理か教えてくれれば,新しいレシピを考えます"
|
|
|
|
| 68 |
|
| 69 |
for resp in answers:
|
| 70 |
for artifact in resp.artifacts:
|
| 71 |
+
print("artifact: " + json.dumps(artifact, indent=2))
|
| 72 |
if artifact.finish_reason == generation.FILTER:
|
| 73 |
print("NSFW")
|
| 74 |
if artifact.type == generation.ARTIFACT_IMAGE:
|
| 75 |
+
img = PIL.Image.open(io.BytesIO(artifact.binary))
|
| 76 |
return img
|
| 77 |
|
| 78 |
class OpenAI:
|
|
|
|
| 94 |
],
|
| 95 |
}
|
| 96 |
|
| 97 |
+
# 文章生成にかかる時間を計測する
|
| 98 |
+
start = time.time()
|
| 99 |
# ChatCompletion APIを呼び出す
|
| 100 |
response = requests.post(
|
| 101 |
"https://api.openai.com/v1/chat/completions",
|
|
|
|
| 105 |
},
|
| 106 |
json=data
|
| 107 |
)
|
| 108 |
+
print("gpt generation time: "+str(time.time() - start))
|
| 109 |
|
| 110 |
# ChatCompletion APIから返された結果を取得する
|
| 111 |
result = response.json()
|
|
|
|
| 132 |
def chat_completion_with_function(cls, prompt, messages, functions):
|
| 133 |
print("prompt:"+prompt)
|
| 134 |
|
| 135 |
+
# 文章生成にかかる時間を計測する
|
| 136 |
+
start = time.time()
|
| 137 |
+
# ChatCompletion APIを呼び出す
|
| 138 |
response = openai.ChatCompletion.create(
|
| 139 |
model=MODEL,
|
| 140 |
messages=messages,
|
| 141 |
functions=functions,
|
| 142 |
function_call="auto"
|
| 143 |
)
|
| 144 |
+
print("gpt generation time: "+str(time.time() - start))
|
| 145 |
|
| 146 |
# ChatCompletion APIから返された結果を取得する
|
| 147 |
message = response.choices[0].message
|
| 148 |
+
print("chat completion message: " + json.dumps(message, indent=2))
|
| 149 |
|
| 150 |
return message
|
| 151 |
|
|
|
|
| 165 |
"""
|
| 166 |
return prompt
|
| 167 |
|
| 168 |
+
def format_recipe(self, lang, title, description, ingredients, instruction, comment_feelings_taste, explanation_to_blind_person, prompt_for_visual_expression):
|
| 169 |
|
| 170 |
template = get_filetext(filename = "template.md")
|
| 171 |
debug_message = template.format(
|
|
|
|
| 176 |
instruction=instruction,
|
| 177 |
comment_feelings_taste=comment_feelings_taste,
|
| 178 |
explanation_to_blind_person=explanation_to_blind_person,
|
| 179 |
+
prompt_for_visual_expression=prompt_for_visual_expression
|
| 180 |
)
|
| 181 |
|
| 182 |
print("debug_message: "+debug_message)
|
|
|
|
| 206 |
|
| 207 |
# resultからfunction_callを取り出す
|
| 208 |
# message = result["function_call"]
|
| 209 |
+
print("chat completion message: " + json.dumps(message, indent=2))
|
| 210 |
|
| 211 |
+
image = None
|
| 212 |
+
html = None
|
| 213 |
if message.get("function_call"):
|
| 214 |
function_name = message["function_call"]["name"]
|
| 215 |
|
|
|
|
| 224 |
explanation_to_blind_person=args.get("explanation_to_blind_person")
|
| 225 |
prompt_for_visual_expression_in_en=args.get("prompt_for_visual_expression_in_en")
|
| 226 |
|
| 227 |
+
prompt_for_visual_expression = \
|
| 228 |
+
prompt_for_visual_expression_in_en \
|
| 229 |
+
+ " delicious looking extremely detailed photo leica f1.2 (50mm|85mm) award winner depth of field bokeh perfect lighting "
|
| 230 |
+
|
| 231 |
+
print("prompt_for_visual_expression: "+prompt_for_visual_expression)
|
| 232 |
+
|
| 233 |
+
# 画像生成にかかる時間を計測する
|
| 234 |
+
start = time.time()
|
| 235 |
+
image = StabilityAI.generate_image(prompt_for_visual_expression)
|
| 236 |
+
print("image generation time: "+str(time.time() - start))
|
| 237 |
+
|
| 238 |
function_response = self.format_recipe(
|
| 239 |
lang=lang,
|
| 240 |
title=title,
|
|
|
|
| 243 |
instruction=instruction,
|
| 244 |
comment_feelings_taste=comment_feelings_taste,
|
| 245 |
explanation_to_blind_person=explanation_to_blind_person,
|
| 246 |
+
prompt_for_visual_expression=prompt_for_visual_expression
|
| 247 |
)
|
| 248 |
|
| 249 |
+
html = (
|
| 250 |
+
"<div style='max-width:100%; overflow:auto'>"
|
| 251 |
+
+ "<p>"
|
| 252 |
+
+ markdown2.markdown(function_response)
|
| 253 |
+
+ "</div>"
|
| 254 |
+
)
|
| 255 |
+
return [image, html]
|
| 256 |
|
|
|
|
|
|
|
| 257 |
def main():
|
| 258 |
iface = gr.Interface(fn=NajiminoAI.generate,
|
| 259 |
inputs=gr.Textbox(label=inputs_label),
|
| 260 |
+
outputs=[
|
| 261 |
+
gr.Image(label="Visual Expression"),
|
| 262 |
+
"html"
|
| 263 |
+
],
|
| 264 |
title=title,
|
| 265 |
description=description,
|
| 266 |
article=article,
|
|
|
|
| 278 |
NajiminoAI.generate("グルテンフリーの香ばしいサバのお好み焼き")
|
| 279 |
|
| 280 |
elif function == 'generate_image':
|
| 281 |
+
image = 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.")
|
| 282 |
+
print("image: " + image)
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
+
# imageが何のクラス確認する
|
| 285 |
+
if type(image) == PIL.PngImagePlugin.PngImageFile:
|
| 286 |
#save image
|
| 287 |
+
image.save("image.png")
|
| 288 |
|
| 289 |
else:
|
| 290 |
main()
|
schema.json
CHANGED
|
@@ -47,10 +47,10 @@
|
|
| 47 |
"ingredients": {
|
| 48 |
"type": "string",
|
| 49 |
"default": "",
|
| 50 |
-
"title": "Your Ingredients",
|
| 51 |
-
"description": "Brainstorm the ingredients needed to
|
| 52 |
"examples": [
|
| 53 |
-
"
|
| 54 |
]
|
| 55 |
},
|
| 56 |
"instruction": {
|
|
@@ -83,10 +83,8 @@
|
|
| 83 |
"prompt_for_visual_expression_in_en": {
|
| 84 |
"type": "string",
|
| 85 |
"default": "",
|
| 86 |
-
"title": "The Schema of prompt for visual expression
|
| 87 |
-
"
|
| 88 |
-
"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."
|
| 89 |
-
]
|
| 90 |
}
|
| 91 |
}
|
| 92 |
}
|
|
|
|
| 47 |
"ingredients": {
|
| 48 |
"type": "string",
|
| 49 |
"default": "",
|
| 50 |
+
"title": "Your Ingredients List",
|
| 51 |
+
"description": "Brainstorm the ingredients needed to make a new recipe, and list with '- ' all the ingredients needed for the new recipe.",
|
| 52 |
"examples": [
|
| 53 |
+
"- サバのフィレ 200g\n- 卵 2個\n- キャベツ 1/4個\n- もやし 50g\n- 紅しょうが(刻んだもの) 2片\n- ネギ(小口切り) 適量\n- 酒大さじ2\n- しょうゆ 大さじ2\n- だし汁 100ml\n- 片栗粉 大さじ2\n- サラダ油 お好みで"
|
| 54 |
]
|
| 55 |
},
|
| 56 |
"instruction": {
|
|
|
|
| 83 |
"prompt_for_visual_expression_in_en": {
|
| 84 |
"type": "string",
|
| 85 |
"default": "",
|
| 86 |
+
"title": "The Schema of prompt for visual expression in English",
|
| 87 |
+
"description": "Prompts for visual representation of AI image generation, consisting of no more than 20 words only"
|
|
|
|
|
|
|
| 88 |
}
|
| 89 |
}
|
| 90 |
}
|
template.md
CHANGED
|
@@ -20,4 +20,4 @@
|
|
| 20 |
|
| 21 |
## 画像生成プロンプト
|
| 22 |
|
| 23 |
-
{
|
|
|
|
| 20 |
|
| 21 |
## 画像生成プロンプト
|
| 22 |
|
| 23 |
+
{prompt_for_visual_expression}
|