simoncwang/AIArtAdvisor
0
1import base642import requests3from openai import OpenAI4import gradio as gr5import numpy as np6import os7from PIL import Image as im 8 9# art critique function10def critique(language, system_role, your_image, openai_api_key, user_text):11 12 print(system_role)13 14 # converting input image from numpy array to png and saving it15 image = im.fromarray(your_image)16 image.save('images/image.png')17 18 # checking if the image size is less than 20MB (requirement for gpt vision)19 print(os.path.getsize('images/image.png'))20 if os.path.getsize('images/image.png') > 20000000:21 raise gr.Error('File size is > 20MB, please choose a different file')22 23 # Function to encode the image24 def encode_image(image_path):25 with open(image_path, "rb") as image_file:26 return base64.b64encode(image_file.read()).decode('utf-8')27 28 # # Path to your image29 image_path = "images/image.png"30 31 # Getting the base64 string32 base64_image = encode_image(image_path)33 34 headers = {35 "Content-Type": "application/json",36 "Authorization": f"Bearer {openai_api_key}"37 }38 39 # the defualt input text prompt40 default_text = "Please give me constructive feedback on this artwork, and things I could work on to improve it."41 42 # "You are a knowledgeable art critic, skilled in giving the artist feedback to help them improve their artwork. Please list out both positive and negative aspects of their work in a constructive way. Also, do your best to take into account any specific questions or comments they have and answer them!"43 44 payload = {45 "model": "gpt-4o",46 "messages": [47 {"role": "system", "content": system_role + language},48 {49 "role": "user",50 "content": [51 {52 "type": "text",53 "text": default_text54 },55 {56 "type": "image_url",57 "image_url": {58 "url": f"data:image/jpeg;base64,{base64_image}"59 }60 },61 {62 "type": "text",63 "text": user_text 64 }65 ]66 }67 ],68 "max_tokens": 100069 }70 71 response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)72 73 # # generating an image based on the feedback74 # client = OpenAI()75 76 # # getting input image in rgba format77 # rgba_image = im.open("images/image.png").convert('RGBA')78 # rgba_image.save("images/rgba_image.png")79 80 # response = client.images.edit(81 # model="dall-e-2",82 # image=open("images/rgba_image.png", "rb"),83 # prompt="Circle the following feedback in this image: add more shadows to the trees",84 # n=1,85 # response_format="b64_json",86 # size="256x256",87 # )88 # image_output = response.data[0]89 90 # image_obj = image_output.model_dump()["b64_json"]91 # image_obj = im.open(base64.b64decode(image_obj))92 # # Add something here to change the filename in each loop93 # image_obj.save("images/output.png")94 95 return response.json()['choices'][0]['message']['content']96 97 98# Gradio demo!99 100# the role of the system101system_prompt = "test"102 103# system role prompt strings104genz_prompt = "Give constructive feedback for this painting including negative and positive aspects as well as how to improve, but use gen-z slang and references in your response."105 106beginner_prompt = "You are an art teacher focused on inspiring young or beginner artists. Please list out both positive and negative aspects of their work in a constructive way. Answer their questions and give advice using very simple terms that are easy to understand for a beginner artist or young child, but still accurate enough to teach them good art principles! Think children's book or cartoon tone!"107 108intermediate_prompt = "You are an art critic that focuses on intermediate level artists. Think a high school student or artist that has a few years of experience! Give them constructive feedback by listing out both positive and negative aspects of their work as well as how to improve it!"109 110advanced_prompt = "You are an expert art critic that specializes in helping professional artists improve. You are skilled and extremely knowledgeable about advanced art techniques. Please list out both positive and negative aspects of their work in a constructive way. Answer any questions the artist might have and provide feedback using a tone that an extremely experienced artist would benefit from. Think art textbook or professional artist in terms of the tone of your response!"111 112# language prompts113english_prompt = "Please write your response in English."114 115chinese_prompt = "Please write your response in Chinese."116 117with gr.Blocks() as demo:118 gr.Markdown(119 """120 121 # AI Art Advisor122 123 Welcome to your personal AI art advisor! I can give you constructive feedback to help you learn and take your art to the next level!124 """125 )126 127 with gr.Accordion("Click for instructions:", open=False):128 gr.Markdown(129 """130 131 - Upload any artwork as a .png or .jpg image into the image upload section. Note, due to gpt-4o restrictions, please keep file sizes <20MB!132 133 - Paste your own OpenAI API key into the API key textbox.134 135 - (Optional) Type any specific questions or comments into the additional textbox. For example, "I am having trouble making this painting more interesting, what could I do to create a better focal point?"136 - By default, the advisor will give you the postives and negatives of your work, as well as some advice on how to improve it137 138 """139 )140 141 with gr.Row():142 with gr.Column():143 gr.Markdown("## Your art")144 image = gr.Image()145 api_key = gr.Textbox(label="OpenAI API Key", placeholder="your API key here")146 user_text = gr.Textbox(label="Specific questions", placeholder="your questions or comments here")147 148 skill_select = gr.Dropdown(149 choices=[("Gen-Z", genz_prompt), ("Beginner", beginner_prompt), ("Intermediate", intermediate_prompt), ("Advanced", advanced_prompt)], 150 label="Level", 151 info="Select your desired skill level to cater your feedback!",152 value=intermediate_prompt,153 interactive=True154 )155 156 language_select = gr.Dropdown(157 choices=[("English", english_prompt), ("Chinese", chinese_prompt)], 158 label="Language", 159 value=english_prompt,160 interactive=True161 )162 163 submit_btn = gr.Button("Submit")164 165 166 with gr.Column():167 gr.Markdown("## Feedback")168 result = gr.Markdown(label="Feedback")169 170 submit_btn.click(critique, inputs=[language_select,skill_select,image,api_key,user_text], outputs=result)171 172# launching the demo173demo.launch()