mrbeliever commited on
Commit
788e1b9
·
verified ·
1 Parent(s): 80e99aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -29
app.py CHANGED
@@ -1,19 +1,17 @@
1
  import streamlit as st
2
  import base64
3
- import requests
4
  import os
5
- import random
6
- import time
7
 
8
- # Function to convert image to base64
9
  def convert_image_to_base64(image):
10
  image_bytes = image.read()
11
  encoded_image = base64.b64encode(image_bytes).decode("utf-8")
12
  return encoded_image
13
 
14
- # Function to generate a caption using Nebius API
15
  def generate_caption(encoded_image):
16
- API_URL = "https://api.studio.nebius.ai/v1/"
17
  API_KEY = os.environ.get("NEBIUS_API_KEY")
18
 
19
  headers = {
@@ -21,31 +19,26 @@ def generate_caption(encoded_image):
21
  "Content-Type": "application/json"
22
  }
23
 
24
- # Add a random string to the URL to prevent caching issues
25
- random_string = str(random.randint(100000, 999999))
26
-
27
  payload = {
28
  "model": "Qwen/Qwen2-VL-72B-Instruct",
29
  "messages": [
30
  {
31
  "role": "system",
32
- "content": """describe this image in great detail"""
33
  },
34
  {
35
  "role": "user",
36
- "content": "write a detailed caption for this image"
 
 
 
 
37
  }
38
  ],
39
- "image": {
40
- "type": "image_url",
41
- "image_url": {
42
- "url": f"data:image/png;base64,{encoded_image}?{random_string}" # Added random query to avoid cache
43
- }
44
- },
45
- "temperature": 1
46
  }
47
 
48
- # Send request to API
49
  response = requests.post(API_URL, headers=headers, json=payload)
50
 
51
  if response.status_code == 200:
@@ -56,12 +49,10 @@ def generate_caption(encoded_image):
56
  st.error(f"API Error {response.status_code}: {response.text}")
57
  return None
58
 
59
- # Streamlit app
60
  def main():
61
- st.set_page_config(page_title="Image to Caption Converter", layout="centered", initial_sidebar_state="collapsed")
62
-
63
- st.title("🖼️ Image to Caption Converter")
64
- st.markdown("Upload an image and let the AI generate a detailed caption for it.")
65
 
66
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
67
 
@@ -69,22 +60,21 @@ def main():
69
  # Display the uploaded image
70
  st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
71
 
72
- # Convert image to base64 and get caption
73
  if st.button("Generate Caption"):
 
74
  with st.spinner("Generating caption..."):
75
  encoded_image = convert_image_to_base64(uploaded_file)
76
 
77
- # Debugging: print out the base64 string length to verify if it's changing
78
  st.write(f"Encoded image length: {len(encoded_image)} characters")
79
 
 
80
  caption = generate_caption(encoded_image)
81
 
82
  if caption:
83
  st.subheader("Generated Caption:")
84
  st.text_area("", caption, height=100, key="caption_area")
85
-
86
- # Copy button (if you want to implement this, use st.session_state)
87
- st.button("Copy to Clipboard", on_click=lambda: st.session_state.update({"caption": caption}))
88
 
89
  if __name__ == "__main__":
90
  main()
 
1
  import streamlit as st
2
  import base64
 
3
  import os
4
+ import requests
 
5
 
6
+ # Function to convert uploaded 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 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 = {
 
19
  "Content-Type": "application/json"
20
  }
21
 
 
 
 
22
  payload = {
23
  "model": "Qwen/Qwen2-VL-72B-Instruct",
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 caption for this image"
32
+ },
33
+ {
34
+ "role": "user",
35
+ "content": f"data:image/png;base64,{encoded_image}" # This is where the image is passed as base64 directly
36
  }
37
  ],
38
+ "temperature": 0
 
 
 
 
 
 
39
  }
40
 
41
+ # Send request to Nebius API
42
  response = requests.post(API_URL, headers=headers, json=payload)
43
 
44
  if response.status_code == 200:
 
49
  st.error(f"API Error {response.status_code}: {response.text}")
50
  return None
51
 
52
+ # Streamlit app layout
53
  def main():
54
+ st.set_page_config(page_title="Image Caption Generator", layout="centered", initial_sidebar_state="collapsed")
55
+ st.title("🖼️ Image to Caption Generator")
 
 
56
 
57
  uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
58
 
 
60
  # Display the uploaded image
61
  st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
62
 
 
63
  if st.button("Generate Caption"):
64
+ # Convert the uploaded image to base64
65
  with st.spinner("Generating caption..."):
66
  encoded_image = convert_image_to_base64(uploaded_file)
67
 
68
+ # Debugging: Ensure the encoded image is valid and not too large
69
  st.write(f"Encoded image length: {len(encoded_image)} characters")
70
 
71
+ # Get the generated caption from the API
72
  caption = generate_caption(encoded_image)
73
 
74
  if caption:
75
  st.subheader("Generated Caption:")
76
  st.text_area("", caption, height=100, key="caption_area")
77
+ st.success("Caption generated successfully!")
 
 
78
 
79
  if __name__ == "__main__":
80
  main()