-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.py
42 lines (34 loc) · 1.41 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
import streamlit as st
from openai import OpenAI
import base64
from utils import get_image_description
# Streamlit app layout
st.title("Image Description using GPT-4o and GPT-4o Mini")
st.write("Upload an image and get a description using GPT-4o or GPT-4o Mini.")
# Textbox for updating OpenAI API key
api_key = st.text_input("Enter your OpenAI API key", type="password")
if not api_key:
api_key = os.environ.get("OPENAI_API_KEY", "")
if api_key:
# Initialize the OpenAI client
client = OpenAI(api_key=api_key)
# Dropdown for selecting the model
model_choice = st.selectbox("Select the model", ["gpt-4o", "gpt-4o-mini"])
# Textbox for updating the prompt
prompt = st.text_input("Enter the prompt for image description", "What’s in this image?")
# Upload image button
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
try:
# Display the uploaded image
st.image(uploaded_file, caption='Uploaded Image.', use_column_width=True)
st.write("")
st.write("Classifying...")
# Get the image description
description = get_image_description(client, uploaded_file, prompt, model_choice)
st.write(description)
except Exception as e:
st.error(f"Error: {e}")
else:
st.error("Please provide a valid OpenAI API key.")