Spaces:
Running
Running
mrbeliever
commited on
Commit
•
6e2e0c5
1
Parent(s):
d5a06f6
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,27 @@
|
|
1 |
import streamlit as st
|
2 |
-
import requests
|
3 |
import base64
|
4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
# Function to convert image to base64
|
7 |
def convert_image_to_base64(image):
|
8 |
-
|
|
|
9 |
encoded_image = base64.b64encode(image_bytes).decode("utf-8")
|
10 |
return encoded_image
|
11 |
|
12 |
-
# Function to generate
|
13 |
def generate_caption(encoded_image):
|
14 |
API_URL = "https://api.studio.nebius.ai/v1/chat/completions"
|
15 |
API_KEY = os.environ.get("NEBIUS_API_KEY")
|
@@ -20,7 +32,7 @@ def generate_caption(encoded_image):
|
|
20 |
}
|
21 |
|
22 |
payload = {
|
23 |
-
"model": "
|
24 |
"messages": [
|
25 |
{
|
26 |
"role": "system",
|
@@ -28,20 +40,19 @@ def generate_caption(encoded_image):
|
|
28 |
},
|
29 |
{
|
30 |
"role": "user",
|
31 |
-
"content": "
|
|
|
|
|
|
|
|
|
32 |
}
|
33 |
],
|
34 |
-
"
|
35 |
-
"type": "image_url",
|
36 |
-
"image_url": {
|
37 |
-
"url": f"data:image/png;base64,{encoded_image}"
|
38 |
-
}
|
39 |
-
},
|
40 |
-
"temperature": 0.7
|
41 |
}
|
42 |
|
|
|
43 |
response = requests.post(API_URL, headers=headers, json=payload)
|
44 |
-
|
45 |
if response.status_code == 200:
|
46 |
result = response.json()
|
47 |
caption = result.get("choices", [{}])[0].get("message", {}).get("content", "No caption generated.")
|
@@ -50,58 +61,32 @@ def generate_caption(encoded_image):
|
|
50 |
st.error(f"API Error {response.status_code}: {response.text}")
|
51 |
return None
|
52 |
|
53 |
-
# Streamlit app
|
54 |
def main():
|
55 |
-
st.set_page_config(page_title="Image
|
56 |
-
|
57 |
-
# Gradient background style
|
58 |
-
st.markdown("""
|
59 |
-
<style>
|
60 |
-
body {
|
61 |
-
background: linear-gradient(135deg, #1e3c72, #2a5298);
|
62 |
-
color: white;
|
63 |
-
font-family: 'Arial', sans-serif;
|
64 |
-
}
|
65 |
-
.uploaded-image {
|
66 |
-
max-width: 100%;
|
67 |
-
border: 2px solid #ffffff;
|
68 |
-
border-radius: 10px;
|
69 |
-
}
|
70 |
-
.copy-button {
|
71 |
-
background-color: #ff8800;
|
72 |
-
color: white;
|
73 |
-
border: none;
|
74 |
-
border-radius: 5px;
|
75 |
-
padding: 10px 15px;
|
76 |
-
cursor: pointer;
|
77 |
-
}
|
78 |
-
.copy-button:hover {
|
79 |
-
background-color: #cc6b00;
|
80 |
-
}
|
81 |
-
</style>
|
82 |
-
""", unsafe_allow_html=True)
|
83 |
-
|
84 |
-
st.title("🖼️ Image to Caption Converter")
|
85 |
|
86 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
|
|
87 |
if uploaded_file:
|
88 |
# Display the uploaded image
|
89 |
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
|
90 |
|
91 |
-
# Convert image to base64 and get caption
|
92 |
if st.button("Generate Caption"):
|
|
|
93 |
with st.spinner("Generating caption..."):
|
94 |
encoded_image = convert_image_to_base64(uploaded_file)
|
|
|
|
|
|
|
|
|
|
|
95 |
caption = generate_caption(encoded_image)
|
96 |
|
97 |
if caption:
|
98 |
st.subheader("Generated Caption:")
|
99 |
st.text_area("", caption, height=100, key="caption_area")
|
100 |
-
|
101 |
-
# Copy button
|
102 |
-
if st.button("Copy to Clipboard"):
|
103 |
-
st.code(caption, language="text")
|
104 |
-
st.success("Caption copied to clipboard!")
|
105 |
|
106 |
if __name__ == "__main__":
|
107 |
main()
|
|
|
1 |
import streamlit as st
|
|
|
2 |
import base64
|
3 |
import os
|
4 |
+
import requests
|
5 |
+
from PIL import Image
|
6 |
+
from io import BytesIO
|
7 |
+
|
8 |
+
# Function to compress and resize the image before base64 encoding
|
9 |
+
def compress_and_resize_image(image, max_size=(1024, 1024), quality=85):
|
10 |
+
img = Image.open(image)
|
11 |
+
img.thumbnail(max_size) # Resize image while maintaining aspect ratio
|
12 |
+
with BytesIO() as byte_io:
|
13 |
+
img.save(byte_io, format="JPEG", quality=quality) # Save with reduced quality
|
14 |
+
byte_io.seek(0)
|
15 |
+
return byte_io
|
16 |
|
17 |
+
# Function to convert uploaded image to base64
|
18 |
def convert_image_to_base64(image):
|
19 |
+
compressed_image = compress_and_resize_image(image)
|
20 |
+
image_bytes = compressed_image.read()
|
21 |
encoded_image = base64.b64encode(image_bytes).decode("utf-8")
|
22 |
return encoded_image
|
23 |
|
24 |
+
# Function to generate caption using Nebius API
|
25 |
def generate_caption(encoded_image):
|
26 |
API_URL = "https://api.studio.nebius.ai/v1/chat/completions"
|
27 |
API_KEY = os.environ.get("NEBIUS_API_KEY")
|
|
|
32 |
}
|
33 |
|
34 |
payload = {
|
35 |
+
"model": "Qwen/Qwen2-VL-72B-Instruct",
|
36 |
"messages": [
|
37 |
{
|
38 |
"role": "system",
|
|
|
40 |
},
|
41 |
{
|
42 |
"role": "user",
|
43 |
+
"content": "Write a caption for this image"
|
44 |
+
},
|
45 |
+
{
|
46 |
+
"role": "user",
|
47 |
+
"content": f"data:image/png;base64,{encoded_image}" # This is where the image is passed as base64 directly
|
48 |
}
|
49 |
],
|
50 |
+
"temperature": 0
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
}
|
52 |
|
53 |
+
# Send request to Nebius API
|
54 |
response = requests.post(API_URL, headers=headers, json=payload)
|
55 |
+
|
56 |
if response.status_code == 200:
|
57 |
result = response.json()
|
58 |
caption = result.get("choices", [{}])[0].get("message", {}).get("content", "No caption generated.")
|
|
|
61 |
st.error(f"API Error {response.status_code}: {response.text}")
|
62 |
return None
|
63 |
|
64 |
+
# Streamlit app layout
|
65 |
def main():
|
66 |
+
st.set_page_config(page_title="Image Caption Generator", layout="centered", initial_sidebar_state="collapsed")
|
67 |
+
st.title("🖼️ Image to Caption Generator")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
70 |
+
|
71 |
if uploaded_file:
|
72 |
# Display the uploaded image
|
73 |
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
|
74 |
|
|
|
75 |
if st.button("Generate Caption"):
|
76 |
+
# Convert the uploaded image to base64
|
77 |
with st.spinner("Generating caption..."):
|
78 |
encoded_image = convert_image_to_base64(uploaded_file)
|
79 |
+
|
80 |
+
# Debugging: Ensure the encoded image is valid and not too large
|
81 |
+
st.write(f"Encoded image length: {len(encoded_image)} characters")
|
82 |
+
|
83 |
+
# Get the generated caption from the API
|
84 |
caption = generate_caption(encoded_image)
|
85 |
|
86 |
if caption:
|
87 |
st.subheader("Generated Caption:")
|
88 |
st.text_area("", caption, height=100, key="caption_area")
|
89 |
+
st.success("Caption generated successfully!")
|
|
|
|
|
|
|
|
|
90 |
|
91 |
if __name__ == "__main__":
|
92 |
main()
|