Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import os
|
|
5 |
from PIL import Image
|
6 |
import io
|
7 |
from langchain_openai import ChatOpenAI
|
|
|
8 |
import replicate
|
9 |
|
10 |
# Set up environment variables for API keys
|
@@ -44,20 +45,20 @@ def generate_prompt_gpt_3_turbo(image,instructions):
|
|
44 |
|
45 |
# Format the prompt with the image description
|
46 |
prompt = instructions.format(image_description=image_description)
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
|
51 |
# Convert response to JSON if necessary
|
52 |
response_dict = json.loads(response)
|
53 |
|
54 |
-
return response_dict, image_description
|
55 |
|
56 |
# Define the Gradio interface
|
57 |
iface = gr.Interface(
|
58 |
fn=generate_prompt_gpt_3_turbo,
|
59 |
inputs=[gr.Image(type="pil"),gr.Textbox(value = instructions)],
|
60 |
-
outputs=[gr.JSON(),gr.Textbox()],
|
61 |
title="Image to Text Generator",
|
62 |
description="Upload an image to generate descriptive text based on the image."
|
63 |
)
|
|
|
5 |
from PIL import Image
|
6 |
import io
|
7 |
from langchain_openai import ChatOpenAI
|
8 |
+
from langchain_community.callbacks import get_openai_callback
|
9 |
import replicate
|
10 |
|
11 |
# Set up environment variables for API keys
|
|
|
45 |
|
46 |
# Format the prompt with the image description
|
47 |
prompt = instructions.format(image_description=image_description)
|
48 |
+
with get_openai_callback() as cb:
|
49 |
+
# Invoke LLM model and get response
|
50 |
+
response = llm.invoke(prompt).content
|
51 |
|
52 |
# Convert response to JSON if necessary
|
53 |
response_dict = json.loads(response)
|
54 |
|
55 |
+
return response_dict, image_description, cb
|
56 |
|
57 |
# Define the Gradio interface
|
58 |
iface = gr.Interface(
|
59 |
fn=generate_prompt_gpt_3_turbo,
|
60 |
inputs=[gr.Image(type="pil"),gr.Textbox(value = instructions)],
|
61 |
+
outputs=[gr.JSON(),gr.Textbox(),gr.Textbox()],
|
62 |
title="Image to Text Generator",
|
63 |
description="Upload an image to generate descriptive text based on the image."
|
64 |
)
|