mrbeliever commited on
Commit
9d344e1
·
verified ·
1 Parent(s): e2109c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -13
app.py CHANGED
@@ -3,8 +3,7 @@ import requests
3
  import os
4
  import base64
5
  from PIL import Image
6
- from io import BytesIO # Correctly import BytesIO from the io module
7
- import pyperclip # For copying text to clipboard
8
 
9
  # Set page title and layout
10
  st.set_page_config(page_title="Image Caption Generator", layout="centered")
@@ -54,7 +53,7 @@ uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"]
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
 
@@ -86,11 +85,22 @@ if uploaded_image and API_KEY:
86
  help="The generated caption will appear here.", label_visibility="collapsed"
87
  )
88
 
89
- # Add a copy button to copy the caption to clipboard
90
- copy_button = st.button("Copy Caption", use_container_width=True)
91
- if copy_button:
92
- pyperclip.copy(caption_area) # Copy caption to clipboard
93
- st.success("Caption copied to clipboard!")
 
 
 
 
 
 
 
 
 
 
 
94
 
95
  except Exception as e:
96
  st.error(f"Error processing the response: {e}")
@@ -100,7 +110,7 @@ else:
100
  else:
101
  st.info("Please upload an image.")
102
 
103
- # Customizing CSS for text area and button
104
  st.markdown(
105
  """
106
  <style>
@@ -111,21 +121,26 @@ st.markdown(
111
  font-size: 16px;
112
  padding: 10px;
113
  }
114
-
 
115
  .stButton>button {
116
- background-color: #4CAF50;
117
  color: white;
118
  font-size: 16px;
119
  border: none;
120
  border-radius: 4px;
121
- padding: 10px;
122
  cursor: pointer;
 
 
123
  }
124
 
125
  .stButton>button:hover {
126
- background-color: #45a049;
 
127
  }
128
 
 
129
  .stTextArea>div>textarea {
130
  background-color: #f5f5f5;
131
  color: #333;
 
3
  import os
4
  import base64
5
  from PIL import Image
6
+ from io import BytesIO
 
7
 
8
  # Set page title and layout
9
  st.set_page_config(page_title="Image Caption Generator", layout="centered")
 
53
  if uploaded_image and API_KEY:
54
  # Convert the image to base64
55
  image = Image.open(uploaded_image)
56
+ buffered = BytesIO()
57
  image.save(buffered, format="PNG")
58
  image_base64 = base64.b64encode(buffered.getvalue()).decode()
59
 
 
85
  help="The generated caption will appear here.", label_visibility="collapsed"
86
  )
87
 
88
+ # JavaScript to copy text from the text area
89
+ st.markdown(
90
+ """
91
+ <script>
92
+ function copyText() {
93
+ var copyText = document.getElementById("caption_area");
94
+ copyText.select();
95
+ copyText.setSelectionRange(0, 99999); // For mobile devices
96
+ document.execCommand("copy");
97
+ }
98
+ </script>
99
+ """, unsafe_allow_html=True
100
+ )
101
+
102
+ # Add a button to trigger the copy functionality
103
+ st.button("Copy Caption", on_click="copyText()")
104
 
105
  except Exception as e:
106
  st.error(f"Error processing the response: {e}")
 
110
  else:
111
  st.info("Please upload an image.")
112
 
113
+ # Customizing CSS for the text area and button (with blue glowing effect for button)
114
  st.markdown(
115
  """
116
  <style>
 
121
  font-size: 16px;
122
  padding: 10px;
123
  }
124
+
125
+ /* Glowing blue button styling */
126
  .stButton>button {
127
+ background-color: #007bff; /* Blue color */
128
  color: white;
129
  font-size: 16px;
130
  border: none;
131
  border-radius: 4px;
132
+ padding: 12px 24px;
133
  cursor: pointer;
134
+ box-shadow: 0 4px 6px rgba(0, 123, 255, 0.6), 0 0 10px rgba(0, 123, 255, 0.8);
135
+ transition: all 0.3s ease-in-out;
136
  }
137
 
138
  .stButton>button:hover {
139
+ background-color: #0056b3; /* Darker blue on hover */
140
+ box-shadow: 0 4px 6px rgba(0, 123, 255, 0.8), 0 0 20px rgba(0, 123, 255, 1);
141
  }
142
 
143
+ /* Text area styling */
144
  .stTextArea>div>textarea {
145
  background-color: #f5f5f5;
146
  color: #333;