mrbeliever commited on
Commit
c32572d
1 Parent(s): c1ea009

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -8
app.py CHANGED
@@ -3,20 +3,22 @@ import requests
3
  import os
4
  import base64
5
  from PIL import Image
 
6
 
7
  # Set page title
8
  st.set_page_config(page_title="Image Caption Generator", layout="centered")
9
 
 
 
 
10
  # UI for the app
11
  st.title("Image Caption Generator")
12
  st.write(
13
  "Upload an image, and this app will generate a detailed caption for it using the Nebius AI API."
14
  )
15
 
16
- # Sidebar for API key
17
- api_key = st.sidebar.text_input(
18
- "Enter Nebius API Key", type="password", help="Add your Nebius API key here."
19
- )
20
 
21
  # Function to call Nebius API
22
  def generate_caption(image_base64, api_key):
@@ -49,17 +51,17 @@ def generate_caption(image_base64, api_key):
49
  # File uploader for image
50
  uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
51
 
52
- if uploaded_image and api_key:
53
  # Convert the image to base64
54
  image = Image.open(uploaded_image)
55
- buffered = st.BytesIO()
56
  image.save(buffered, format="PNG")
57
  image_base64 = base64.b64encode(buffered.getvalue()).decode()
58
 
59
  # Generate caption
60
  st.image(image, caption="Uploaded Image", use_column_width=True)
61
  st.write("Generating caption...")
62
- result = generate_caption(image_base64, api_key)
63
 
64
  # Display the result
65
  if "error" in result:
@@ -68,7 +70,9 @@ if uploaded_image and api_key:
68
  caption = result.get("messages", [{}])[-1].get("content", [{}])[0].get("text", "No caption generated.")
69
  st.subheader("Generated Caption")
70
  st.write(caption)
 
 
71
  else:
72
- st.info("Please upload an image and provide your API key.")
73
 
74
  st.sidebar.write("Built with ❤️ by OpenAI GPT-4")
 
3
  import os
4
  import base64
5
  from PIL import Image
6
+ from io import BytesIO # Correctly import BytesIO from the io module
7
 
8
  # Set page title
9
  st.set_page_config(page_title="Image Caption Generator", layout="centered")
10
 
11
+ # API key from environment variable
12
+ API_KEY = os.environ.get("NEBIUS_API_KEY")
13
+
14
  # UI for the app
15
  st.title("Image Caption Generator")
16
  st.write(
17
  "Upload an image, and this app will generate a detailed caption for it using the Nebius AI API."
18
  )
19
 
20
+ if not API_KEY:
21
+ st.error("API key not found. Please set the `NEBIUS_API_KEY` environment variable.")
 
 
22
 
23
  # Function to call Nebius API
24
  def generate_caption(image_base64, api_key):
 
51
  # File uploader for image
52
  uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
53
 
54
+ if uploaded_image and API_KEY:
55
  # Convert the image to base64
56
  image = Image.open(uploaded_image)
57
+ buffered = BytesIO() # Use BytesIO from the io module
58
  image.save(buffered, format="PNG")
59
  image_base64 = base64.b64encode(buffered.getvalue()).decode()
60
 
61
  # Generate caption
62
  st.image(image, caption="Uploaded Image", use_column_width=True)
63
  st.write("Generating caption...")
64
+ result = generate_caption(image_base64, API_KEY)
65
 
66
  # Display the result
67
  if "error" in result:
 
70
  caption = result.get("messages", [{}])[-1].get("content", [{}])[0].get("text", "No caption generated.")
71
  st.subheader("Generated Caption")
72
  st.write(caption)
73
+ elif not API_KEY:
74
+ st.warning("Please set the `NEBIUS_API_KEY` environment variable.")
75
  else:
76
+ st.info("Please upload an image.")
77
 
78
  st.sidebar.write("Built with ❤️ by OpenAI GPT-4")