22from typing import Optional
33
44import replicate
5- import requests
65from dotenv import load_dotenv
76from langchain_core .tools import tool
87
@@ -77,41 +76,34 @@ def generate_image(
7776 return "Failed to generate image. Please try again."
7877
7978 # Handle Flux Kontext Pro output format
79+ image_data : Optional [bytes ] = None
8080 try :
81- # Flux Kontext Pro returns an object with .url() method
81+ # Flux Kontext Pro returns an object that can be used directly as image data
82+ # and also has a .url() method for the URL
8283 generated_image_url = output .url ()
8384 print (f"[TOOL] Extracted URL using output.url(): { generated_image_url } " )
84- except AttributeError :
85- # Fallback for unexpected output formats
86- print (f"[TOOL] No .url() method found, output type: { type (output )} " )
87- if isinstance (output , list ):
88- generated_image_url = output [0 ]
89- print (f"[TOOL] Fallback: Extracted URL from list: { generated_image_url } " )
90- elif isinstance (output , str ):
91- generated_image_url = output
92- print (f"[TOOL] Fallback: Using direct string URL: { generated_image_url } " )
93- else :
94- print (f"[TOOL] Unexpected output type: { type (output )} , trying to convert to string" )
95- generated_image_url = str (output )
9685
97- print (f"[TOOL] Final generated image URL: { generated_image_url } " )
86+ # Get the image data directly from the output object
87+ image_data = output .read ()
88+ print (f"[TOOL] Got image data directly from output, size: { len (image_data ) if image_data else 0 } bytes" )
9889
99- # Download the generated image
100- image_data : Optional [bytes ] = None
101- try :
102- response = requests .get (generated_image_url )
103- response .raise_for_status ()
104- image_data = response .content # get the actual image bytes in content into memory
105- # Close the response to free up resources
106- response .close ()
90+ except AttributeError as e :
91+ print (f"[TOOL] Error accessing output methods: { e } " )
92+ return f"Failed to process generated image: { str (e )} "
10793 except Exception as e :
108- return f"Failed to download generated image: { str (e )} "
94+ print (f"[TOOL] Unexpected error processing output: { e } " )
95+ return f"Failed to process generated image: { str (e )} "
96+
97+ # Check if we successfully got image data
98+ if image_data is None :
99+ return "Failed to get image data from generation output"
109100
110101 # Generate unique ID for the image
111102 image_id = str (uuid .uuid4 ())
112103
113104 # Upload to S3
114105 print (f"[TOOL] Uploading to S3 with image_id: { image_id } " )
106+ print (f"[TOOL] Image data size: { len (image_data )} bytes" )
115107 try :
116108 s3_result = upload_generated_image_to_s3 (
117109 image_data = image_data ,
@@ -121,11 +113,14 @@ def generate_image(
121113 title = title ,
122114 )
123115 print (f"[TOOL] S3 upload result: { s3_result } " )
116+ print (f"[TOOL] S3 upload success: { s3_result .get ('success' , False )} " )
124117
125118 if s3_result ["success" ]:
126119 # Store structured result for the agent to retrieve
127120 tool_result = {"image_id" : image_id , "title" : title , "prompt" : prompt , "success" : True }
121+ print (f"[TOOL] About to store tool result: { tool_result } " )
128122 store_tool_result (user_id , "generate_image" , tool_result )
123+ print ("[TOOL] Tool result stored successfully" )
129124
130125 result_msg = f"Image generated successfully! User can find it his/her gallery. \
131126 Image ID: { image_id } , Title: { title } "
0 commit comments