-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradio_gen_img.py
106 lines (93 loc) · 3.55 KB
/
gradio_gen_img.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
# code reference from https://learn.deeplearning.ai/huggingface-gradio/lesson/4/image-generation-app
# code reference from https://learn.deeplearning.ai/huggingface-gradio/lesson/6/chat-with-any-llm
import gradio as gr
from io import BytesIO
from PIL import Image
import requests, json
from openai import OpenAI
from datetime import datetime
import base64
with open('api_key.txt') as f:
private_key = f.read()
client = OpenAI(
api_key=private_key,
)
# Text-to-image endpoint
def run_dall_e(message):
response = client.images.generate(
model="dall-e-3",
prompt=message,
size="1024x1024",
quality="standard",
n=1,
)
return response.data[0].url
# convert response to PIL image
def response_to_pil(image_url):
response = requests.get(image_url)
img = Image.open(BytesIO(response.content))
return img
# convert the PIL image to base64
def image_to_base64(pil_image):
buffer = BytesIO()
pil_image.save(buffer, format="PNG")
return base64.b64encode(buffer.getvalue()).decode()
# generate image
def gen_img(date, input_prompt):
try:
prompt = f"A winter picture matching {date} and mood of this conversation: {input_prompt}"
output = run_dall_e(prompt)
return output
except Exception as e:
print(f"Error generating image: {e}")
return None
# Convert turn to prompt
def convert_history_to_prompt(message, chat_history):
guide = """
You're a bot that needs to ask questions to guess the mood of the user's input message.
Please answer of the user's input message in one or two sentences.
In response to user's third message, please don't ask questions.
"""
prompt = [{"role": "system", "content": guide}]
prompt.append({"role":"assistant", "content":"How was your day?"})
for turn in chat_history:
user_message, bot_message = turn
prompt.append({"role":"user", "content":user_message})
prompt.append({"role":"assistant", "content":bot_message})
prompt.append({"role":"user", "content": message})
return prompt
# generate chatbot response
def chatbot_response(message, chat_history):
input_prompt = convert_history_to_prompt(message, chat_history)
if len(chat_history) < 3:
response = client.chat.completions.create(model="gpt-4", messages=input_prompt)
bot_message = response.choices[0].message.content
if len(chat_history) == 2:
question = "Okay, I will make an image for you. Is there any specific object you want to see in the image?"
bot_message += f"<br><br>{question}"
chat_history.append((message, bot_message))
else:
image_url = gen_img(datetime.today().strftime("%Y-%m-%d"), input_prompt)
image_html = f"<img src='{image_url}'/>"
bot_message = f"This is the image I made for you:)"
image_html += f"<br><br>{bot_message}"
chat_history.append((message, image_html))
return "", chat_history
with gr.Blocks() as demo:
date = datetime.today().strftime("%Y-%m-%d")
gr.Markdown(
f"""
# 🎄Image Advent Calendar🎄
Hello! I am image advent calendar. <br>
I can generate an image for you. <br>
Today is {date}. <br>
How was your day?
"""
)
chatbot = gr.Chatbot(label="Conversation")
msg = gr.Textbox(label="User Input")
btn = gr.Button("Submit")
btn.click(chatbot_response, [msg, chatbot], [msg, chatbot])
msg.submit(chatbot_response, [msg, chatbot], [msg, chatbot])
gr.close_all()
demo.launch(share=True)