Spaces:
Build error
Build error
Commit
·
073bbfa
1
Parent(s):
4c1d948
change file
Browse files
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
-
import os
|
| 6 |
from PIL import Image
|
| 7 |
import numpy as np
|
| 8 |
import ipadic
|
|
@@ -11,17 +9,18 @@ import difflib
|
|
| 11 |
import io
|
| 12 |
import os
|
| 13 |
|
| 14 |
-
client = OpenAI(api_key=os.getenv(
|
|
|
|
| 15 |
|
| 16 |
def generate_image(text):
|
| 17 |
-
image_path = f"
|
| 18 |
if not os.path.exists(image_path):
|
| 19 |
response = client.images.generate(
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
)
|
| 26 |
image_url = response.data[0].url
|
| 27 |
image_data = requests.get(image_url).content
|
|
@@ -30,27 +29,28 @@ def generate_image(text):
|
|
| 30 |
img.save(image_path)
|
| 31 |
return image_path
|
| 32 |
|
|
|
|
| 33 |
def calulate_similarity_score(ori_text, text):
|
| 34 |
if ori_text != text:
|
| 35 |
model_name = "text-embedding-3-small"
|
| 36 |
-
response = client.embeddings.create(input
|
| 37 |
score = cos_sim(response.data[0].embedding, response.data[1].embedding)
|
| 38 |
score = int(round(score, 2) * 100)
|
| 39 |
if score == 100:
|
| 40 |
score = 99
|
| 41 |
else:
|
| 42 |
-
|
| 43 |
return score
|
| 44 |
|
|
|
|
| 45 |
def cos_sim(v1, v2):
|
| 46 |
return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
|
| 47 |
|
|
|
|
| 48 |
def tokenize_text(text):
|
| 49 |
mecab = MeCab.Tagger(f"-Ochasen {ipadic.MECAB_ARGS}")
|
| 50 |
-
return [
|
| 51 |
-
|
| 52 |
-
for t in mecab.parse(text).splitlines()[:-1]
|
| 53 |
-
]
|
| 54 |
|
| 55 |
def create_match_words(ori_text, text):
|
| 56 |
ori_words = tokenize_text(ori_text)
|
|
@@ -58,74 +58,68 @@ def create_match_words(ori_text, text):
|
|
| 58 |
match_words = [w for w in words if w in ori_words]
|
| 59 |
return match_words
|
| 60 |
|
|
|
|
| 61 |
def create_hint_text(ori_text, text):
|
| 62 |
response = list(difflib.ndiff(list(text), list(ori_text)))
|
| 63 |
output = ""
|
| 64 |
for r in response:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
return output
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
elif selected_option == "Q3":
|
| 79 |
-
return "/content/images/東京スカイツリーの近くで花火大会が行われている.png"
|
| 80 |
-
elif selected_option == "Q4":
|
| 81 |
-
return "/content/images/イカとタイがいた都会.png"
|
| 82 |
-
elif selected_option == "Q5":
|
| 83 |
-
return "/content/images/赤いきつねと緑のたぬき.png"
|
| 84 |
-
if selected_option == "Q6":
|
| 85 |
-
return "/content/images/宇宙に向かってたい焼きが空を飛んでいる.png"
|
| 86 |
-
elif selected_option == "Q7":
|
| 87 |
-
return "/content/images/イケメンが海岸でクリームパンを眺めている.png"
|
| 88 |
-
elif selected_option == "Q8":
|
| 89 |
-
return "/content/images/生麦生米生卵生麦生米生卵生麦生米生卵.png"
|
| 90 |
-
elif selected_option == "Q9":
|
| 91 |
-
return "/content/images/サイバーエージェントで働く人たち.png"
|
| 92 |
-
elif selected_option == "Q10":
|
| 93 |
-
return "/content/images/柿くへば鐘が鳴るなり法隆寺.png"
|
| 94 |
-
elif selected_option == "Q11":
|
| 95 |
-
return "/content/images/鳴くよウグイス平安京.png"
|
| 96 |
-
else:
|
| 97 |
-
return "/content/images/abc.png"
|
| 98 |
|
| 99 |
def main(text, option):
|
| 100 |
-
ori_text =
|
| 101 |
image_path = generate_image(text)
|
| 102 |
score = calulate_similarity_score(ori_text, text)
|
| 103 |
|
| 104 |
if score < 80:
|
| 105 |
match_words = create_match_words(ori_text, text)
|
| 106 |
-
hint_text = "一致している単語リスト: "+" ".join(match_words)
|
| 107 |
elif 80 <= score < 100:
|
| 108 |
-
hint_text = "一致していない箇所: "+create_hint_text(ori_text, text)
|
| 109 |
else:
|
| 110 |
hint_text = ""
|
| 111 |
return image_path, f"{score}点", hint_text
|
| 112 |
|
|
|
|
| 113 |
with gr.Blocks() as demo:
|
| 114 |
with gr.Row():
|
| 115 |
with gr.Column():
|
| 116 |
gr.Markdown(
|
| 117 |
"# プロンプトを当てるゲーム \n これは表示されている画像のプロンプトを当てるゲームです。プロンプトを入���するとそれに対応した画像とスコアとヒントが表示されます。スコア100点を目指して頑張ってください! \n\nヒントは80点未満の場合は当たっている単語、80点以上の場合は足りない文字を「^」で示した文字列を表示しています。",
|
| 118 |
)
|
| 119 |
-
|
|
|
|
|
|
|
| 120 |
output_title_image = gr.components.Image(type="filepath", label="お題")
|
| 121 |
-
|
|
|
|
|
|
|
| 122 |
|
| 123 |
-
input_text = gr.components.Textbox(
|
|
|
|
|
|
|
| 124 |
submit_button = gr.Button("Submit")
|
| 125 |
with gr.Column():
|
| 126 |
output_image = gr.components.Image(type="filepath", label="生成画像")
|
| 127 |
output_score = gr.components.Textbox(lines=1, label="スコア")
|
| 128 |
output_hint_text = gr.components.Textbox(lines=1, label="ヒント")
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
submit_button.click(
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from openai import OpenAI
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
import ipadic
|
|
|
|
| 9 |
import io
|
| 10 |
import os
|
| 11 |
|
| 12 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 13 |
+
|
| 14 |
|
| 15 |
def generate_image(text):
|
| 16 |
+
image_path = f"./{text}.png"
|
| 17 |
if not os.path.exists(image_path):
|
| 18 |
response = client.images.generate(
|
| 19 |
+
model="dall-e-3",
|
| 20 |
+
prompt=text,
|
| 21 |
+
size="1024x1024",
|
| 22 |
+
quality="standard",
|
| 23 |
+
n=1,
|
| 24 |
)
|
| 25 |
image_url = response.data[0].url
|
| 26 |
image_data = requests.get(image_url).content
|
|
|
|
| 29 |
img.save(image_path)
|
| 30 |
return image_path
|
| 31 |
|
| 32 |
+
|
| 33 |
def calulate_similarity_score(ori_text, text):
|
| 34 |
if ori_text != text:
|
| 35 |
model_name = "text-embedding-3-small"
|
| 36 |
+
response = client.embeddings.create(input=[ori_text, text], model=model_name)
|
| 37 |
score = cos_sim(response.data[0].embedding, response.data[1].embedding)
|
| 38 |
score = int(round(score, 2) * 100)
|
| 39 |
if score == 100:
|
| 40 |
score = 99
|
| 41 |
else:
|
| 42 |
+
score = 100
|
| 43 |
return score
|
| 44 |
|
| 45 |
+
|
| 46 |
def cos_sim(v1, v2):
|
| 47 |
return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
|
| 48 |
|
| 49 |
+
|
| 50 |
def tokenize_text(text):
|
| 51 |
mecab = MeCab.Tagger(f"-Ochasen {ipadic.MECAB_ARGS}")
|
| 52 |
+
return [t.split()[0] for t in mecab.parse(text).splitlines()[:-1]]
|
| 53 |
+
|
|
|
|
|
|
|
| 54 |
|
| 55 |
def create_match_words(ori_text, text):
|
| 56 |
ori_words = tokenize_text(ori_text)
|
|
|
|
| 58 |
match_words = [w for w in words if w in ori_words]
|
| 59 |
return match_words
|
| 60 |
|
| 61 |
+
|
| 62 |
def create_hint_text(ori_text, text):
|
| 63 |
response = list(difflib.ndiff(list(text), list(ori_text)))
|
| 64 |
output = ""
|
| 65 |
for r in response:
|
| 66 |
+
if r[:2] == "- ":
|
| 67 |
+
continue
|
| 68 |
+
elif r[:2] == "+ ":
|
| 69 |
+
output += "^"
|
| 70 |
+
else:
|
| 71 |
+
output += r.strip()
|
| 72 |
return output
|
| 73 |
|
| 74 |
+
|
| 75 |
+
def update_question(option):
|
| 76 |
+
answer = os.getenv(option)
|
| 77 |
+
return f"./{answer}.png"
|
| 78 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
def main(text, option):
|
| 81 |
+
ori_text = os.getenv(option)
|
| 82 |
image_path = generate_image(text)
|
| 83 |
score = calulate_similarity_score(ori_text, text)
|
| 84 |
|
| 85 |
if score < 80:
|
| 86 |
match_words = create_match_words(ori_text, text)
|
| 87 |
+
hint_text = "一致している単語リスト: " + " ".join(match_words)
|
| 88 |
elif 80 <= score < 100:
|
| 89 |
+
hint_text = "一致していない箇所: " + create_hint_text(ori_text, text)
|
| 90 |
else:
|
| 91 |
hint_text = ""
|
| 92 |
return image_path, f"{score}点", hint_text
|
| 93 |
|
| 94 |
+
|
| 95 |
with gr.Blocks() as demo:
|
| 96 |
with gr.Row():
|
| 97 |
with gr.Column():
|
| 98 |
gr.Markdown(
|
| 99 |
"# プロンプトを当てるゲーム \n これは表示されている画像のプロンプトを当てるゲームです。プロンプトを入���するとそれに対応した画像とスコアとヒントが表示されます。スコア100点を目指して頑張ってください! \n\nヒントは80点未満の場合は当たっている単語、80点以上の場合は足りない文字を「^」で示した文字列を表示しています。",
|
| 100 |
)
|
| 101 |
+
option = gr.components.Radio(
|
| 102 |
+
["Q1", "Q2", "Q3"], label="問題を選んでください!"
|
| 103 |
+
)
|
| 104 |
output_title_image = gr.components.Image(type="filepath", label="お題")
|
| 105 |
+
option.change(
|
| 106 |
+
update_question, inputs=[option], outputs=[output_title_image]
|
| 107 |
+
)
|
| 108 |
|
| 109 |
+
input_text = gr.components.Textbox(
|
| 110 |
+
lines=1, label="画像にマッチするテキストを入力して!"
|
| 111 |
+
)
|
| 112 |
submit_button = gr.Button("Submit")
|
| 113 |
with gr.Column():
|
| 114 |
output_image = gr.components.Image(type="filepath", label="生成画像")
|
| 115 |
output_score = gr.components.Textbox(lines=1, label="スコア")
|
| 116 |
output_hint_text = gr.components.Textbox(lines=1, label="ヒント")
|
| 117 |
+
with gr.Row():
|
| 118 |
+
gr.Dropdown()
|
| 119 |
|
| 120 |
+
submit_button.click(
|
| 121 |
+
main,
|
| 122 |
+
inputs=[input_text, option],
|
| 123 |
+
outputs=[output_image, output_score, output_hint_text],
|
| 124 |
+
)
|
| 125 |
+
demo.launch()
|