├── README.md
├── chat2image.py
├── imgs
├── 152aef75-7815-478d-8346-fbdc337de443.jpg
├── 50121ea5-2661-400a-bfc9-31f761159d44.jpg
├── 5ba897b6-f0f2-45f7-8b53-f3ffc80e258f.jpg
├── 67f8619b-567d-4f04-a29b-7f32cac4cd77.jpg
├── 83a698ff-79ad-49c5-b5ea-287e09cee75c.jpg
├── 92a07148-f403-4836-a43d-543a2dc545d4.jpg
├── a7acc99f-3a43-45e1-a0ba-c83f42dfe455.jpg
├── b5e674c8-98a8-457c-9402-5f790e7e22ee.jpg
└── result.png
└── test.ipynb
/README.md:
--------------------------------------------------------------------------------
1 | # Chat2Image Creator
2 |
3 | Generate a image through chat using ChatGPT API and StableDiffusion. 챗봇을 사용하여 채팅으로 상상하게하고 그림으로 그리기.
4 |
5 | 
6 |
7 | 
8 |
9 | ## Dependency
10 |
11 | - Python 3
12 | - gradio
13 | - openai
14 | - diffusers
15 | - torch
16 |
17 | ## Reference
18 |
19 | - [My previos video using ChatGPT and Midjourney v4](https://youtu.be/sIE_6EtFVpE)
20 | - [ChatGPT API](https://platform.openai.com/docs/guides/chat) by OpenAI
21 | - [Dreamlike Photoreal 2.0](https://huggingface.co/dreamlike-art/dreamlike-photoreal-2.0) by [Dreamlike Art](https://dreamlike.art/)
22 |
23 | > Get OpenAI API key: https://platform.openai.com/account/api-keys
--------------------------------------------------------------------------------
/chat2image.py:
--------------------------------------------------------------------------------
1 | import gradio as gr
2 | import openai
3 | from diffusers import StableDiffusionPipeline
4 | import torch
5 | import uuid
6 |
7 | openai.api_key = '[YOUR-OPENAI-API-KEY-HERE]'
8 |
9 | model_id = 'dreamlike-art/dreamlike-photoreal-2.0'
10 | pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
11 | pipe = pipe.to('cuda')
12 |
13 |
14 | def answer(state, state_chatbot, text):
15 | if '그림' in text:
16 | prompt = state[-1]['content']
17 |
18 | img = pipe(prompt).images[0]
19 |
20 | img_path = f'imgs/{uuid.uuid4()}.jpg'
21 | img.save(img_path)
22 |
23 | state_chatbot = state_chatbot + [(text, f'')]
24 | else:
25 | messages = state + [{
26 | 'role': 'user',
27 | 'content': text
28 | }]
29 |
30 | res = openai.ChatCompletion.create(
31 | model='gpt-3.5-turbo',
32 | messages=messages
33 | )
34 |
35 | msg = res['choices'][0]['message']['content']
36 |
37 | new_state = [{
38 | 'role': 'user',
39 | 'content': text
40 | }, {
41 | 'role': 'assistant',
42 | 'content': msg
43 | }]
44 |
45 | state = state + new_state
46 | state_chatbot = state_chatbot + [(text, msg)]
47 |
48 | print(state)
49 |
50 | return state, state_chatbot, state_chatbot
51 |
52 |
53 | with gr.Blocks(css='#chatbot .overflow-y-auto{height:500px}') as demo:
54 | state = gr.State([{
55 | 'role': 'system',
56 | 'content': 'You are a helpful assistant.'
57 | }])
58 | state_chatbot = gr.State([])
59 |
60 | with gr.Row():
61 | gr.HTML("""
62 |
63 |
Chat2Image Creator
64 |
65 |
66 | YouTube 빵형의 개발도상국
67 |
68 |
""")
69 |
70 | with gr.Row():
71 | chatbot = gr.Chatbot(elem_id='chatbot')
72 |
73 | with gr.Row():
74 | txt = gr.Textbox(show_label=False, placeholder='ChatGPT의 상상을 그림으로 그려보세요').style(container=False)
75 |
76 | txt.submit(answer, [state, state_chatbot, txt], [state, state_chatbot, chatbot])
77 | txt.submit(lambda: '' , None, txt)
78 |
79 |
80 | demo.launch(debug=True, share=True)
81 |
--------------------------------------------------------------------------------
/imgs/152aef75-7815-478d-8346-fbdc337de443.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kairess/Chat2Image-Creator/90461d1c62bef796a091c84300ca46793f1c7f67/imgs/152aef75-7815-478d-8346-fbdc337de443.jpg
--------------------------------------------------------------------------------
/imgs/50121ea5-2661-400a-bfc9-31f761159d44.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kairess/Chat2Image-Creator/90461d1c62bef796a091c84300ca46793f1c7f67/imgs/50121ea5-2661-400a-bfc9-31f761159d44.jpg
--------------------------------------------------------------------------------
/imgs/5ba897b6-f0f2-45f7-8b53-f3ffc80e258f.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kairess/Chat2Image-Creator/90461d1c62bef796a091c84300ca46793f1c7f67/imgs/5ba897b6-f0f2-45f7-8b53-f3ffc80e258f.jpg
--------------------------------------------------------------------------------
/imgs/67f8619b-567d-4f04-a29b-7f32cac4cd77.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kairess/Chat2Image-Creator/90461d1c62bef796a091c84300ca46793f1c7f67/imgs/67f8619b-567d-4f04-a29b-7f32cac4cd77.jpg
--------------------------------------------------------------------------------
/imgs/83a698ff-79ad-49c5-b5ea-287e09cee75c.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kairess/Chat2Image-Creator/90461d1c62bef796a091c84300ca46793f1c7f67/imgs/83a698ff-79ad-49c5-b5ea-287e09cee75c.jpg
--------------------------------------------------------------------------------
/imgs/92a07148-f403-4836-a43d-543a2dc545d4.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kairess/Chat2Image-Creator/90461d1c62bef796a091c84300ca46793f1c7f67/imgs/92a07148-f403-4836-a43d-543a2dc545d4.jpg
--------------------------------------------------------------------------------
/imgs/a7acc99f-3a43-45e1-a0ba-c83f42dfe455.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kairess/Chat2Image-Creator/90461d1c62bef796a091c84300ca46793f1c7f67/imgs/a7acc99f-3a43-45e1-a0ba-c83f42dfe455.jpg
--------------------------------------------------------------------------------
/imgs/b5e674c8-98a8-457c-9402-5f790e7e22ee.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kairess/Chat2Image-Creator/90461d1c62bef796a091c84300ca46793f1c7f67/imgs/b5e674c8-98a8-457c-9402-5f790e7e22ee.jpg
--------------------------------------------------------------------------------
/imgs/result.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kairess/Chat2Image-Creator/90461d1c62bef796a091c84300ca46793f1c7f67/imgs/result.png
--------------------------------------------------------------------------------