kevinwang676 commited on
Commit
43d251c
β€’
1 Parent(s): de3d9dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -1
app.py CHANGED
@@ -4,6 +4,9 @@ from __future__ import annotations
4
 
5
  import gradio as gr
6
  import torch
 
 
 
7
 
8
  from app_canny import create_demo as create_demo_canny
9
  from app_depth import create_demo as create_demo_depth
@@ -27,13 +30,59 @@ if not torch.cuda.is_available():
27
 
28
  model = Model(base_model_id=DEFAULT_MODEL_ID, task_name="Canny")
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  with gr.Blocks(css="style.css") as demo:
31
- gr.Markdown(DESCRIPTION)
 
 
 
 
 
32
  gr.DuplicateButton(
33
  value="Duplicate Space for private use",
34
  elem_id="duplicate-button",
35
  visible=SHOW_DUPLICATE_BUTTON,
36
  )
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  with gr.Tabs():
39
  with gr.TabItem("Canny"):
 
4
 
5
  import gradio as gr
6
  import torch
7
+ import re
8
+ import openai
9
+ from cairosvg import svg2png
10
 
11
  from app_canny import create_demo as create_demo_canny
12
  from app_depth import create_demo as create_demo_depth
 
30
 
31
  model = Model(base_model_id=DEFAULT_MODEL_ID, task_name="Canny")
32
 
33
+ def gpt_control(apikey, prompt):
34
+
35
+ openai.api_key = apikey
36
+
37
+ # gpt
38
+ messages = [{"role": "system", "content": "You are an SVG expert with years of experience and multiple contributions to the SVG project. Based on the prompt and the description, please generate the corresponding SVG code."},
39
+ {"role": "user", "content": f"""Provide only the shell command without any explanations.
40
+ The current objective is below. Reply with the SVG code only:
41
+ OBJECTIVE: {prompt}
42
+ YOUR SVG CODE:
43
+ """}]
44
+
45
+ completion = openai.ChatCompletion.create(
46
+ model = "gpt-4",
47
+ messages = messages
48
+ )
49
+
50
+ chat_response = completion.choices[0].message.content
51
+
52
+ code = re.findall('<.*>', chat_response)
53
+ code_new = '\n'.join(code)
54
+
55
+ svg_code = f"""
56
+ {code_new}
57
+ """
58
+ svg2png(bytestring=svg_code,write_to='output.png')
59
+
60
+ return 'output.png'
61
+
62
+
63
  with gr.Blocks(css="style.css") as demo:
64
+ gr.HTML("<center>"
65
+ "<h1>🌁πŸͺ„πŸŒƒ - ControlNet with GPT-4</h1>"
66
+ "</center>")
67
+
68
+ gr.Markdown("## <center>🌟 Born to Create: Controllable Text-to-Image Generation with GPT-4</center>")
69
+
70
  gr.DuplicateButton(
71
  value="Duplicate Space for private use",
72
  elem_id="duplicate-button",
73
  visible=SHOW_DUPLICATE_BUTTON,
74
  )
75
+
76
+ with gr.Tab("GPT-4 Control"):
77
+ with gr.Row():
78
+ with gr.Column():
79
+ inp1 = gr.Textbox(label="OpenAI API Key", type="password")
80
+ inp2 = gr.Textbox(label="Position Prompt (as simple as possible)")
81
+ btn1 = gr.Button("GPT-4 Control", variant="primary")
82
+ with gr.Column():
83
+ out1 = gr.Image(label="Output Image", type="pil")
84
+ btn1.click(gpt_control, [inp1, inp2], [out1])
85
+
86
 
87
  with gr.Tabs():
88
  with gr.TabItem("Canny"):