Prashanthsrn commited on
Commit
7e4e182
·
verified ·
1 Parent(s): fe6aedc

Update ui_components.py

Browse files
Files changed (1) hide show
  1. ui_components.py +41 -7
ui_components.py CHANGED
@@ -1,4 +1,6 @@
1
  import streamlit as st
 
 
2
 
3
  def create_sidebar():
4
  with st.sidebar:
@@ -10,19 +12,51 @@ def create_sidebar():
10
  st.write("- Use high-quality images for better results")
11
  st.write("- Try different images to see how the AI adapts")
12
  st.write("- Edit and refine the generated captions for your specific needs")
 
13
 
14
- def create_main_content(caption):
15
  st.header("Generated Caption")
16
- st.write(caption)
17
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  st.header("Customization Options")
19
  tone = st.selectbox("Tone", ["Professional", "Casual", "Humorous", "Formal"])
20
  length = st.slider("Caption Length", min_value=50, max_value=200, value=100, step=10)
21
 
22
- if st.button("Regenerate Caption"):
23
- with st.spinner("Regenerating caption..."):
24
- new_caption = f"{caption} (Regenerated with {tone} tone, {length} characters)"
25
- st.write(new_caption)
26
 
27
  st.header("Caption Preview")
28
- st.text_area("Edit Caption", caption, height=150)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from image_captioning import generate_caption
3
+ from utils import copy_to_clipboard, get_image_file_buffer
4
 
5
  def create_sidebar():
6
  with st.sidebar:
 
12
  st.write("- Use high-quality images for better results")
13
  st.write("- Try different images to see how the AI adapts")
14
  st.write("- Edit and refine the generated captions for your specific needs")
15
+ st.write("- Use the regenerate button to get alternative captions")
16
 
17
+ def create_main_content(image, initial_caption):
18
  st.header("Generated Caption")
19
+ caption = st.text_area("Edit Caption", initial_caption, height=100)
20
 
21
+ col1, col2 = st.columns(2)
22
+ with col1:
23
+ if st.button("Copy Caption"):
24
+ copy_to_clipboard(caption)
25
+
26
+ with col2:
27
+ if st.button("Regenerate Caption"):
28
+ with st.spinner("Regenerating caption..."):
29
+ new_caption = generate_caption(image)
30
+ st.session_state.caption = new_caption
31
+ st.experimental_rerun()
32
+
33
  st.header("Customization Options")
34
  tone = st.selectbox("Tone", ["Professional", "Casual", "Humorous", "Formal"])
35
  length = st.slider("Caption Length", min_value=50, max_value=200, value=100, step=10)
36
 
37
+ if st.button("Apply Customization"):
38
+ with st.spinner("Applying customization..."):
39
+ customized_caption = f"{caption}\n\nCustomized with {tone} tone, targeting {length} characters."
40
+ st.text_area("Customized Caption", customized_caption, height=150)
41
 
42
  st.header("Caption Preview")
43
+ st.markdown(f"*{caption}*")
44
+
45
+ def display_image_upload():
46
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
47
+
48
+ if uploaded_file is not None:
49
+ image = Image.open(uploaded_file)
50
+ st.image(image, caption="Uploaded Image", use_column_width=True)
51
+
52
+ with st.spinner("Generating caption..."):
53
+ try:
54
+ if 'caption' not in st.session_state:
55
+ st.session_state.caption = generate_caption(image)
56
+ create_main_content(image, st.session_state.caption)
57
+ except Exception as e:
58
+ st.error(f"An error occurred while generating the caption: {str(e)}")
59
+
60
+ return image
61
+
62
+ return None