taka-yayoi commited on
Commit
650b85f
·
verified ·
1 Parent(s): c24eec6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -23
app.py CHANGED
@@ -31,6 +31,7 @@ def respond(message, history):
31
  #prompt = list(itertools.chain.from_iterable(history))
32
  #prompt.append(message)
33
 
 
34
  prompt = pd.DataFrame(
35
  {"prompt": [message], "num_inference_steps": 25}
36
  )
@@ -41,31 +42,40 @@ def respond(message, history):
41
 
42
  embed_image_markdown = ""
43
 
44
- response = requests.request(method="POST", headers=headers, url=local_endpoint, data=data_json)
45
- response_data = response.json()
46
- #print(response_data["predictions"])
47
-
48
- im_array = np.array(response_data["predictions"], dtype=np.uint8)
49
- #print(im_array)
50
- im = Image.fromarray(im_array, 'RGB')
 
 
 
 
51
 
52
 
53
- # debug
54
- #image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg/687px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg"
55
- #print("image_url:", image_url)
56
- #im = Image.open(io.BytesIO(requests.get(image_url).content))
57
- #numpydata = np.asarray(im)
58
-
59
- rawBytes = io.BytesIO()
60
- im.save(rawBytes, "PNG")
61
- rawBytes.seek(0) # ファイルの先頭に移動
62
- image_encoded = base64.b64encode(rawBytes.read()).decode('ascii')
63
- #print(image_encoded)
64
-
65
- embed_image_markdown = f"![](data:image/png;base64,{image_encoded})"
66
- #print(embed_image_markdown)
67
-
68
- # print(response.json())
 
 
 
 
 
69
  return embed_image_markdown
70
 
71
 
 
31
  #prompt = list(itertools.chain.from_iterable(history))
32
  #prompt.append(message)
33
 
34
+ # プロンプトの作成
35
  prompt = pd.DataFrame(
36
  {"prompt": [message], "num_inference_steps": 25}
37
  )
 
42
 
43
  embed_image_markdown = ""
44
 
45
+ try:
46
+ # モデルサービングエンドポイントに問い合わせ
47
+ response = requests.request(method="POST", headers=headers, url=local_endpoint, data=data_json)
48
+ response_data = response.json()
49
+ #print(response_data["predictions"])
50
+
51
+ # numpy arrayに変換
52
+ im_array = np.array(response_data["predictions"], dtype=np.uint8)
53
+ #print(im_array)
54
+ # 画像に変換
55
+ im = Image.fromarray(im_array, 'RGB')
56
 
57
 
58
+ # debug
59
+ #image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg/687px-Mona_Lisa,_by_Leonardo_da_Vinci,_from_C2RMF_retouched.jpg"
60
+ #print("image_url:", image_url)
61
+ #im = Image.open(io.BytesIO(requests.get(image_url).content))
62
+ #numpydata = np.asarray(im)
63
+
64
+ rawBytes = io.BytesIO()
65
+ im.save(rawBytes, "PNG")
66
+ rawBytes.seek(0) # ファイルの先頭に移動
67
+ # base64にエンコード
68
+ image_encoded = base64.b64encode(rawBytes.read()).decode('ascii')
69
+ #print(image_encoded)
70
+
71
+ # マークダウンに埋め込み
72
+ embed_image_markdown = f"![](data:image/png;base64,{image_encoded})"
73
+ #print(embed_image_markdown)
74
+
75
+ except Exception as error:
76
+ response_data = f"ERROR status_code: {type(error).__name__}"
77
+ #+ str(response.status_code) + " response:" + response.text
78
+
79
  return embed_image_markdown
80
 
81