mayf commited on
Commit
301b896
·
verified ·
1 Parent(s): 8e5f097

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -7
app.py CHANGED
@@ -21,14 +21,35 @@ def load_clients():
21
 
22
  caption_client, story_client = load_clients()
23
 
24
- # —––––––– Main Flow
25
- uploaded = st.file_uploader("Upload a child-friendly image:", type=["jpg", "png", "jpeg"])
26
- if not uploaded:
27
- st.info("🌈 Please upload an image to start the magic!")
28
- else:
29
- # Process Image
30
- img = Image.open(uploaded).convert("RGB")
 
 
 
 
 
 
 
 
 
 
31
  st.image(img, use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  # Generate Caption
34
  with st.spinner("🔍 Discovering image secrets..."):
 
21
 
22
  caption_client, story_client = load_clients()
23
 
24
+ # —––––––– Modified image processing (safe version) —–––––––
25
+ def process_image(uploaded_file):
26
+ try:
27
+ img = Image.open(uploaded_file).convert("RGB")
28
+
29
+ # Only resize if absolutely necessary
30
+ if img.size[0] > 2048 or img.size[1] > 2048:
31
+ img.thumbnail((2048, 2048))
32
+
33
+ return img
34
+ except Exception as e:
35
+ st.error(f"Image processing error: {str(e)}")
36
+ st.stop()
37
+ # —––––––– Main flow —–––––––
38
+ uploaded = st.file_uploader("Upload an image:", type=["jpg", "png", "jpeg"])
39
+ if uploaded:
40
+ img = process_image(uploaded)
41
  st.image(img, use_column_width=True)
42
+
43
+ with st.spinner("Generating caption..."):
44
+ caption = generate_caption(img)
45
+
46
+ # Your original validation
47
+ if not caption:
48
+ st.error("😢 Couldn't understand this image. Try another one!")
49
+ st.stop()
50
+
51
+ st.success(f"**Caption:** {caption}")
52
+
53
 
54
  # Generate Caption
55
  with st.spinner("🔍 Discovering image secrets..."):