Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from openai import OpenAI
|
3 |
+
import requests
|
4 |
+
from PIL import Image
|
5 |
+
from io import BytesIO
|
6 |
+
|
7 |
+
def process_text(api_key, example, question):
|
8 |
+
if example:
|
9 |
+
question=example
|
10 |
+
client = OpenAI(api_key=api_key)
|
11 |
+
|
12 |
+
image_response = client.images.generate(model="dall-e-3", prompt=question,size="1024x1024",quality="standard",n=1,)
|
13 |
+
image_url = image_response.data[0].url
|
14 |
+
response = requests.get(image_url)
|
15 |
+
if response.status_code == 200:
|
16 |
+
image = Image.open(BytesIO(response.content))
|
17 |
+
|
18 |
+
chat_response = client.chat.completions.create(model="gpt-4-vision-preview", messages=[{"role": "user","content": [{"type": "text", "text": question}, {"type": "image_url","image_url": {"url": image_url,},},],}],max_tokens=300,)
|
19 |
+
answer = chat_response.choices[0].message.content
|
20 |
+
|
21 |
+
return image, answer
|
22 |
+
|
23 |
+
demo = gr.Interface(
|
24 |
+
fn=process_text,
|
25 |
+
inputs=[
|
26 |
+
gr.Textbox(label="Your API Key", type="password"),
|
27 |
+
gr.Radio(["A group of people are crowded around in a living room talking to one another. A man in the foreground introduces two individuals one appears to be a regular human male the other appears to be an animal. What is unusual about this description?", \
|
28 |
+
"A woman is waiting to get on the elevator. But the people in the elevator are on fire. Where can this event take place?"], label="Example Question"),
|
29 |
+
gr.Textbox(label="Question")
|
30 |
+
],
|
31 |
+
outputs=[
|
32 |
+
gr.Image(type="pil", label="Image Generated by DALL·E 3", image_mode="fixed", width=768, height=768),
|
33 |
+
gr.Textbox(label="Answer")],
|
34 |
+
title="Chain of Images for Intuitively Reasoning"
|
35 |
+
)
|
36 |
+
|
37 |
+
if __name__ == "__main__":
|
38 |
+
demo.launch(show_api=True)
|