taka-yayoi commited on
Commit
2c26f27
·
verified ·
1 Parent(s): 8c6d11a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import itertools
2
+ import gradio as gr
3
+ import requests
4
+ import os
5
+ from gradio.themes.utils import sizes
6
+
7
+
8
+ def respond(message, history):
9
+
10
+ if len(message.strip()) == 0:
11
+ return "ERROR the question should not be empty"
12
+
13
+ local_token = os.getenv('API_TOKEN')
14
+ local_endpoint = os.getenv('API_ENDPOINT')
15
+
16
+ if local_token is None or local_endpoint is None:
17
+ return "ERROR missing env variables"
18
+
19
+ # Add your API token to the headers
20
+ headers = {
21
+ 'Content-Type': 'application/json',
22
+ 'Authorization': f'Bearer {local_token}'
23
+ }
24
+
25
+ #prompt = list(itertools.chain.from_iterable(history))
26
+ #prompt.append(message)
27
+ #q = {"inputs": [prompt]}
28
+ q = {
29
+ "dataframe_split": {
30
+ "columns": [
31
+ "prompt",
32
+ "num_inference_steps"
33
+ ],
34
+ "data": [
35
+ [
36
+ {message},25
37
+ ]
38
+ ]
39
+ }
40
+ }
41
+
42
+ try:
43
+ response = requests.post(
44
+ local_endpoint, json=q, headers=headers, timeout=100)
45
+ response_data = response.json()
46
+ #print(response_data)
47
+ response_data=response_data["predictions"][0]
48
+ #print(response_data)
49
+
50
+ except Exception as error:
51
+ response_data = f"ERROR status_code: {type(error).__name__}"
52
+ # + str(response.status_code) + " response:" + response.text
53
+
54
+ # print(response.json())
55
+ return response_data
56
+
57
+
58
+ theme = gr.themes.Soft(
59
+ text_size=sizes.text_sm,radius_size=sizes.radius_sm, spacing_size=sizes.spacing_sm,
60
+ )
61
+
62
+
63
+ demo = gr.ChatInterface(
64
+ respond,
65
+ chatbot=gr.Chatbot(show_label=False, container=False, show_copy_button=True, bubble_full_width=True),
66
+ textbox=gr.Textbox(placeholder="生成する画像を指示",
67
+ container=False, scale=7),
68
+ title="Databricks Personalized Image demo - Generate personalized image using model serving endpoint",
69
+ description="[Databricksにおける生成AIを用いたブランドに沿う画像の生成](https://qiita.com/taka_yayoi/items/8d3473847d9ccc8ca00c)",
70
+ examples=[["A photo of an orange bcnchr chair"],
71
+ ["A photo of an blue hsmnchr chair"],
72
+ ["A photo of an red rckchr chair"],],
73
+ cache_examples=False,
74
+ theme=theme,
75
+ retry_btn=None,
76
+ undo_btn=None,
77
+ clear_btn="Clear",
78
+ )
79
+
80
+ if __name__ == "__main__":
81
+ demo.launch()