Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import base64
|
3 |
|
4 |
-
from langchain.callbacks import get_openai_callback
|
5 |
from openai import OpenAI
|
6 |
|
7 |
config = {
|
@@ -23,22 +22,20 @@ def invoke(openai_api_key, prompt, image):
|
|
23 |
raise gr.Error("Image is required.")
|
24 |
|
25 |
content = ""
|
26 |
-
cb = ""
|
27 |
|
28 |
try:
|
29 |
client = OpenAI(api_key = openai_api_key)
|
30 |
|
31 |
img_b64 = get_img_b64(image)
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
temperature = config["temperature"],)
|
42 |
|
43 |
content = completion.choices[0].message.content
|
44 |
except Exception as e:
|
@@ -46,7 +43,7 @@ def invoke(openai_api_key, prompt, image):
|
|
46 |
|
47 |
raise gr.Error(e)
|
48 |
|
49 |
-
return content,
|
50 |
|
51 |
description = """<a href='https://www.gradio.app/'>Gradio</a> UI using the <a href='https://openai.com/'>OpenAI</a> API
|
52 |
with <a href='https://openai.com/research/gpt-4'>gpt-4-vision-preview</a> model."""
|
@@ -62,4 +59,4 @@ demo = gr.Interface(fn = invoke,
|
|
62 |
title = "Generative AI - Multimodal LLM",
|
63 |
description = description,)
|
64 |
|
65 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import base64
|
3 |
|
|
|
4 |
from openai import OpenAI
|
5 |
|
6 |
config = {
|
|
|
22 |
raise gr.Error("Image is required.")
|
23 |
|
24 |
content = ""
|
|
|
25 |
|
26 |
try:
|
27 |
client = OpenAI(api_key = openai_api_key)
|
28 |
|
29 |
img_b64 = get_img_b64(image)
|
30 |
|
31 |
+
completion = client.chat.completions.create(
|
32 |
+
max_tokens = config["max_tokens"],
|
33 |
+
messages=[{"role": "user",
|
34 |
+
"content": [{"type": "text", "text": prompt},
|
35 |
+
{"type": "image_url",
|
36 |
+
"image_url": {"url": f"data:image/jpeg;base64,{img_b64}"},},],},],
|
37 |
+
model = config["model"],
|
38 |
+
temperature = config["temperature"],)
|
|
|
39 |
|
40 |
content = completion.choices[0].message.content
|
41 |
except Exception as e:
|
|
|
43 |
|
44 |
raise gr.Error(e)
|
45 |
|
46 |
+
return content, ""
|
47 |
|
48 |
description = """<a href='https://www.gradio.app/'>Gradio</a> UI using the <a href='https://openai.com/'>OpenAI</a> API
|
49 |
with <a href='https://openai.com/research/gpt-4'>gpt-4-vision-preview</a> model."""
|
|
|
59 |
title = "Generative AI - Multimodal LLM",
|
60 |
description = description,)
|
61 |
|
62 |
+
demo.launch()
|