Spaces:
Running
Running
mrbeliever
commited on
Commit
•
a93e14b
1
Parent(s):
c7bca57
Update app.py
Browse files
app.py
CHANGED
@@ -1,83 +1,107 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
|
|
3 |
import os
|
4 |
-
import json
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
<style>
|
17 |
-
body {
|
18 |
-
background: linear-gradient(135deg, #1e3c72, #2a5298);
|
19 |
-
color: #FFFFFF;
|
20 |
-
font-family: 'Arial', sans-serif;
|
21 |
}
|
22 |
-
.stApp {
|
23 |
-
align-items: center;
|
24 |
-
justify-content: center;
|
25 |
-
}
|
26 |
-
img {
|
27 |
-
border-radius: 10px;
|
28 |
-
margin-bottom: 20px;
|
29 |
-
max-width: 100%;
|
30 |
-
height: auto;
|
31 |
-
}
|
32 |
-
h1, h2, h3 {
|
33 |
-
text-align: center;
|
34 |
-
}
|
35 |
-
</style>
|
36 |
-
""",
|
37 |
-
unsafe_allow_html=True
|
38 |
-
)
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
st.
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
if response.status_code == 200:
|
71 |
-
# Extract the generated prompt
|
72 |
-
result = response.json()
|
73 |
-
generated_prompt = result.get("choices", [{}])[0].get("message", {}).get("content", "No prompt generated.")
|
74 |
-
|
75 |
-
# Display the generated prompt
|
76 |
-
st.subheader("Generated Prompt")
|
77 |
-
st.text_area("", generated_prompt, height=200)
|
78 |
-
|
79 |
-
# Copy button
|
80 |
-
if st.button("Copy Prompt"):
|
81 |
-
st.write("Copy functionality is not supported in this environment. Please manually copy the text.")
|
82 |
-
else:
|
83 |
-
st.error(f"Failed to generate prompt: {response.status_code} - {response.text}")
|
|
|
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 |
+
image_bytes = image.read()
|
9 |
+
encoded_image = base64.b64encode(image_bytes).decode("utf-8")
|
10 |
+
return encoded_image
|
11 |
|
12 |
+
# Function to generate a caption using Nebius API
|
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")
|
16 |
|
17 |
+
headers = {
|
18 |
+
"Authorization": f"Bearer {API_KEY}",
|
19 |
+
"Content-Type": "application/json"
|
|
|
|
|
|
|
|
|
|
|
20 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
payload = {
|
23 |
+
"model": "llava-hf/llava-1.5-7b-hf",
|
24 |
+
"messages": [
|
25 |
+
{
|
26 |
+
"role": "system",
|
27 |
+
"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]."""
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"role": "user",
|
31 |
+
"content": "write a detailed caption for this image"
|
32 |
+
}
|
33 |
+
],
|
34 |
+
"image": {
|
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.")
|
48 |
+
return caption
|
49 |
+
else:
|
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 to Caption Converter", layout="centered", initial_sidebar_state="collapsed")
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|