victorgg commited on
Commit
705bdac
·
verified ·
1 Parent(s): dbc9309

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -3,6 +3,8 @@ import torch
3
  from PIL import Image
4
  import numpy as np
5
  from transformers import AutoProcessor, AutoModelForCausalLM
 
 
6
 
7
  # Initialize Florence model
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -34,13 +36,12 @@ def generate_caption(image):
34
  parsed_answer = florence_processor.post_process_generation(
35
  generated_text,
36
  task="<MORE_DETAILED_CAPTION>",
37
- image_size=(image.width, image.height) # Use PIL image dimensions here
38
  )
39
 
40
- prompt = parsed_answer["<MORE_DETAILED_CAPTION>"]
41
- return prompt
42
 
43
- # Streamlit Interface
44
  st.title("Florence 2 Caption Generator")
45
  st.write("Upload an image to generate a caption:")
46
 
@@ -50,10 +51,29 @@ uploaded_image = st.file_uploader("Choose an Image", type=["jpg", "jpeg", "png"]
50
  # If an image is uploaded
51
  if uploaded_image is not None:
52
  image = Image.open(uploaded_image)
53
- st.image(image, caption="Uploaded Image", use_container_width=True) # <-- FIXED DEPRECATED PARAMETER
54
-
55
  # Generate caption when button is pressed
56
  if st.button("Generate Caption"):
57
  caption = generate_caption(image)
58
  st.subheader("Generated Caption:")
59
- st.write(caption)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from PIL import Image
4
  import numpy as np
5
  from transformers import AutoProcessor, AutoModelForCausalLM
6
+ from io import BytesIO
7
+ import base64
8
 
9
  # Initialize Florence model
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
36
  parsed_answer = florence_processor.post_process_generation(
37
  generated_text,
38
  task="<MORE_DETAILED_CAPTION>",
39
+ image_size=(image.width, image.height)
40
  )
41
 
42
+ return parsed_answer["<MORE_DETAILED_CAPTION>"]
 
43
 
44
+ # Streamlit UI
45
  st.title("Florence 2 Caption Generator")
46
  st.write("Upload an image to generate a caption:")
47
 
 
51
  # If an image is uploaded
52
  if uploaded_image is not None:
53
  image = Image.open(uploaded_image)
54
+ st.image(image, caption="Uploaded Image", use_container_width=True)
55
+
56
  # Generate caption when button is pressed
57
  if st.button("Generate Caption"):
58
  caption = generate_caption(image)
59
  st.subheader("Generated Caption:")
60
+ st.write(caption)
61
+
62
+ # ✅ API Mode: Handle API Requests
63
+ st.experimental_set_query_params() # Ensure Streamlit can handle query params
64
+
65
+ def handle_api_request():
66
+ """Handle API request by checking URL query parameters."""
67
+ query_params = st.experimental_get_query_params()
68
+
69
+ if "image" in query_params:
70
+ image_base64 = query_params["image"][0] # Get Base64-encoded image
71
+ image_bytes = BytesIO(base64.b64decode(image_base64))
72
+ image = Image.open(image_bytes)
73
+
74
+ caption = generate_caption(image)
75
+ st.json({"caption": caption}) # Return JSON response
76
+
77
+ # Check if API mode is enabled
78
+ if "image" in st.experimental_get_query_params():
79
+ handle_api_request()