mrbeliever commited on
Commit
c73a6b4
1 Parent(s): 5e15f24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -49
app.py CHANGED
@@ -1,69 +1,74 @@
1
- import os
2
  import streamlit as st
3
- from openai import OpenAI
 
 
4
  from PIL import Image
5
- import io
6
 
7
- # Set up the OpenAI client
8
- client = OpenAI(
9
- base_url="https://api.studio.nebius.ai/v1/",
10
- api_key=os.environ.get("NEBIUS_API_KEY")
 
 
 
11
  )
12
 
13
- # Function to generate caption from image URL
14
- def generate_caption(image_data):
15
- completion = client.chat.completions.create(
16
- model="Qwen/Qwen2-VL-72B-Instruct",
17
- messages=[
 
 
 
 
 
 
 
18
  {
19
  "role": "system",
20
- "content": """You are an image to prompt converter. Your work is to observe each and every detail of the image and craft a detailed prompt under 75 words in this format: [image content/subject, description of action, state, and mood], [art form, style], [artist/photographer reference if needed], [additional settings such as camera and lens settings, lighting, colors, effects, texture, background, rendering]."""
21
  },
22
  {
23
  "role": "user",
24
  "content": [
25
- {
26
- "type": "text",
27
- "text": """Write a caption for this image"""
28
- },
29
- {
30
- "type": "image_url",
31
- "image_url": {
32
- "url": image_data
33
- }
34
- }
35
- ]
36
- }
37
  ],
38
- temperature=0
39
- )
40
-
41
- caption = completion.to_json().get("choices", [{}])[0].get("message", {}).get("content", "")
42
- return caption
43
 
44
- # Streamlit UI
45
- st.title("Image to Caption Generator")
46
- st.write("Upload an image, and the app will generate a detailed caption for it.")
 
 
47
 
48
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
 
49
 
50
- if uploaded_file is not None:
51
- # Display the uploaded image
52
- image = Image.open(uploaded_file)
53
- st.image(image, caption="Uploaded Image", use_column_width=True)
54
-
55
- # Convert image to a base64 string
56
- buffered = io.BytesIO()
57
  image.save(buffered, format="PNG")
58
- img_base64 = buffered.getvalue().decode("utf-8")
59
 
60
- # Generate caption using the OpenAI API
 
61
  st.write("Generating caption...")
62
- caption = generate_caption(img_base64)
63
 
64
- # Display the generated caption
65
- if caption:
66
- st.subheader("Generated Caption:")
67
- st.write(caption)
68
  else:
69
- st.write("No caption could be generated.")
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ 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):
23
+ api_url = "https://api.studio.nebius.ai/v1/chat/completions"
24
+ headers = {"Authorization": f"Bearer {api_key}"}
25
+ payload = {
26
+ "model": "Qwen/Qwen2-VL-72B-Instruct",
27
+ "messages": [
28
  {
29
  "role": "system",
30
+ "content": """You are an image to prompt converter. Your work is to observe each and every detail of the image and craft a detailed prompt under 75 words in this format: [image content/subject, description of action, state, and mood], [art form, style], [artist/photographer reference if needed], [additional settings such as camera and lens settings, lighting, colors, effects, texture, background, rendering].""",
31
  },
32
  {
33
  "role": "user",
34
  "content": [
35
+ {"type": "text", "text": "Write a caption for this image"},
36
+ {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_base64}"}},
37
+ ],
38
+ },
 
 
 
 
 
 
 
 
39
  ],
40
+ "temperature": 0,
41
+ }
 
 
 
42
 
43
+ response = requests.post(api_url, json=payload, headers=headers)
44
+ if response.status_code == 200:
45
+ return response.json()
46
+ else:
47
+ return {"error": response.text}
48
 
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:
66
+ st.error(f"Error: {result['error']}")
 
67
  else:
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")