-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
230 lines (204 loc) · 6.82 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import os
import openai
import gradio as gr
def init_auth():
global authenticated
globals()['authenticated'] = False
async def generate_image(prompt:str, size: str):
try:
response = openai.Image.create(
prompt=prompt,
n=1,
size=size,
)
result = response['data']
url = result[0]['url']
print(url)
return [url,[url]]
except Exception as e:
gr.Warning(e)
return ["Error"]
def get_prompt(text_system: str, query: str) -> list:
return [
{
"role": "system",
"content": f"""{text_system}""",
},
{
"role": "user",
"content": f"""{query}""",
}
]
async def generate_text(text_system, text_prompt, max_tokens: int, text_model: str, temperature: float):
if not globals()['authenticated']:
gr.Warning("Please enter a valid API key")
return
if not text_prompt:
gr.Warning("Please enter your prompt")
return
print()
print("Inference parameters:")
print(text_system, text_prompt, max_tokens, text_model, temperature)
print()
print()
try:
creation = openai.ChatCompletion.create(
model=text_model,
messages=get_prompt(text_system, text_prompt),
temperature=temperature,
max_tokens=max_tokens,
)
print(creation)
return creation.choices[0].message.content
except Exception as e:
gr.Warning(e)
return "Error"
async def save_key(api_key):
try:
openai.api_key = api_key
openai.Model.list()
globals()['authenticated'] = True
print(globals()['authenticated'])
gr.Info("API key saved")
except Exception as e:
gr.Warning("Invalid API key")
# APP
theme = gr.themes.Monochrome(
font=[gr.themes.GoogleFont("Kanit"), "sans-serif"],
)
system_examples = [
"You are a exceptional artist",
"You are a exceptional artist who can express special moments in words and images.",
]
prompt_examples = [
"Describe an image about ",
"Describe an image with kids playing in the park. ",
]
text_models = [
"gpt-3.5-turbo",
"gpt-3.5-turbo-16k",
"gpt-4",
]
with gr.Blocks(title="Generate Text and Images", theme=theme) as demo:
with gr.Column(variant="panel"):
with gr.Row():
gr.Markdown("# Generate Text and Images",)
with gr.Row():
with gr.Column(variant="panel", scale=3):
with gr.Row():
text_system = gr.Textbox(
label="Enter your system",
max_lines=10,
placeholder="Enter your system",
container=False,
lines=3,
)
text_prompt = gr.Textbox(
label="Enter your prompt",
max_lines=10,
placeholder="Enter your prompt",
container=False,
lines=3,
)
with gr.Row():
output = gr.Textbox(
max_lines=10,
container=False,
lines=10,
interactive=True
)
with gr.Row():
img_url = gr.Textbox(
placeholder="Image URL",
container=False,
interactive=False,
lines=5,
show_copy_button=True,
)
gallery = gr.Gallery(
label="Generated images", show_label=False, elem_id="gallery"
, columns=[2], rows=[2], object_fit="contain", height="auto")
with gr.Column(variant="panel"):
with gr.Row():
btn_txt = gr.Button("Generate text", scale=0)
btn_img = gr.Button("Generate image", scale=0)
with gr.Row():
gr.Examples(
examples=system_examples,
inputs=[text_system],
label="System examples",
)
with gr.Row():
gr.Examples(
examples=prompt_examples,
inputs=[text_prompt],
label="Prompt examples",
)
with gr.Row():
text_model = gr.Dropdown(
label="Text model",
choices=text_models,
value="gpt-3.5-turbo",
container=True,
)
with gr.Row():
max_tokens = gr.Number(
label="Max tokens",
value=200,
minimum=1,
maximum=500,
step=10,
container=True,
precision=0,
)
with gr.Row():
temperature = gr.Slider(
label="Temperature",
value=1,
minimum=0,
maximum=1,
step=0.1,
container=True,
)
with gr.Row():
img_size = gr.Dropdown(
label="Image size",
choices=['1024x1024', '512x512', '256x256'],
value="256x256",
)
with gr.Row():
api_key = gr.Textbox(
type="password",
label="OpenAI API Key",
scale=5,
container=True,
)
with gr.Row():
save_key_btn = gr.Button(
value="Save",
size="sm",
scale=1,
)
try:
btn_img.click(generate_image, [output, img_size], [img_url, gallery])
btn_txt.click(
fn=generate_text,
inputs=[text_system,text_prompt,max_tokens, text_model, temperature],
outputs=output,
batch=False,
trigger_mode="once",
show_progress=True,
)
save_key_btn.click(
fn=save_key,
inputs=[api_key],
outputs=None,
batch=False,
trigger_mode="once",
show_progress=True,
)
except Exception as e:
gr.Warning(e)
if __name__ == "__main__":
init_auth()
demo.launch()