|
|
|
import gradio as gr |
|
import os |
|
from openai import OpenAI |
|
|
|
|
|
with gr.Blocks() as demo: |
|
with gr.Row(): |
|
image = gr.Image(label="image", height=600) |
|
chatbot = gr.Chatbot() |
|
|
|
prompt = gr.Textbox(label="prompt") |
|
url = gr.Textbox(label="url") |
|
openai_key = gr.Textbox(label="OpenAI API key") |
|
gr.Examples( |
|
examples=[ |
|
["https://huggingface.co/Adapter/t2iadapter/resolve/main/figs_SDXLV1.0/org_sketch.png", "Describe what is in the image","https://huggingface.co/Adapter/t2iadapter/resolve/main/figs_SDXLV1.0/org_sketch.png"] |
|
], |
|
inputs=[image, prompt,url], |
|
) |
|
|
|
def respond(message,openai_key, url ,chat_history): |
|
os.environ["OPENAI_API_KEY"] = openai_key |
|
client = OpenAI() |
|
|
|
response = client.chat.completions.create( |
|
model="gpt-4-vision-preview", |
|
messages=[ |
|
{ |
|
"role": "user", |
|
"content": [ |
|
{"type": "text", "text": message}, |
|
{ |
|
"type": "image_url", |
|
"image_url":url, |
|
}, |
|
], |
|
}, |
|
], |
|
max_tokens=1000, |
|
) |
|
|
|
out = response.choices[0].message.content |
|
|
|
chat_history.append((message, out)) |
|
return "", chat_history |
|
|
|
def update_image(url, image): |
|
return url |
|
|
|
prompt.submit(respond, [prompt,openai_key, url, chatbot], [prompt, chatbot]) |
|
url.submit(update_image, [ url, image], [image]) |
|
demo.queue().launch(share=True) |