Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
with gr.Blocks() as demo:
|
| 8 |
+
with gr.Row():
|
| 9 |
+
image = gr.Image(label="image", height=600)
|
| 10 |
+
chatbot = gr.Chatbot()
|
| 11 |
+
|
| 12 |
+
prompt = gr.Textbox(label="prompt")
|
| 13 |
+
url = gr.Textbox(label="url")
|
| 14 |
+
openai_key = gr.Textbox(label="OpenAI API key")
|
| 15 |
+
gr.Examples(
|
| 16 |
+
examples=[
|
| 17 |
+
["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"]
|
| 18 |
+
],
|
| 19 |
+
inputs=[image, prompt,url],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
def respond(message,openai_key, url ,chat_history):
|
| 23 |
+
os.environ["OPENAI_API_KEY"] = openai_key
|
| 24 |
+
client = OpenAI()
|
| 25 |
+
|
| 26 |
+
response = client.chat.completions.create(
|
| 27 |
+
model="gpt-4-vision-preview",
|
| 28 |
+
messages=[
|
| 29 |
+
{
|
| 30 |
+
"role": "user",
|
| 31 |
+
"content": [
|
| 32 |
+
{"type": "text", "text": message},
|
| 33 |
+
{
|
| 34 |
+
"type": "image_url",
|
| 35 |
+
"image_url":url,
|
| 36 |
+
},
|
| 37 |
+
],
|
| 38 |
+
},
|
| 39 |
+
],
|
| 40 |
+
max_tokens=1000,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
out = response.choices[0].message.content
|
| 44 |
+
|
| 45 |
+
chat_history.append((message, out))
|
| 46 |
+
return "", chat_history
|
| 47 |
+
|
| 48 |
+
def update_image(url, image):
|
| 49 |
+
return url
|
| 50 |
+
|
| 51 |
+
prompt.submit(respond, [prompt,openai_key, url, chatbot], [prompt, chatbot])
|
| 52 |
+
url.submit(update_image, [ url, image], [image])
|
| 53 |
+
demo.queue().launch()
|