|
| 1 | +import uuid |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +import replicate |
| 5 | +import requests |
| 6 | +from dotenv import load_dotenv |
| 7 | +from langchain_core.tools import tool |
| 8 | + |
| 9 | +from llm.utils import upload_generated_image_to_s3 |
| 10 | + |
| 11 | +load_dotenv() |
| 12 | + |
| 13 | + |
| 14 | +def initialize_tools(): |
| 15 | + """Initialize the tools for the agent.""" |
| 16 | + |
| 17 | + @tool( |
| 18 | + description="Generate an image based on a prompt", |
| 19 | + ) |
| 20 | + def generate_image( |
| 21 | + prompt: str, |
| 22 | + user_id: str, |
| 23 | + image_url: str, |
| 24 | + ) -> str: |
| 25 | + """ |
| 26 | + Generate an image based on a prompt. |
| 27 | + """ |
| 28 | + input = { |
| 29 | + "width": 768, |
| 30 | + "height": 768, |
| 31 | + "prompt": prompt, |
| 32 | + "refine": "expert_ensemble_refiner", |
| 33 | + "apply_watermark": False, |
| 34 | + "num_inference_steps": 25, |
| 35 | + "prompt_strength": 0.5, |
| 36 | + "image": image_url, |
| 37 | + } |
| 38 | + |
| 39 | + # Generate image using Replicate |
| 40 | + output = replicate.run( |
| 41 | + "stability-ai/sdxl:7762fd07cf82c948538e41f63f77d6 \ |
| 42 | + 85e02b063e37e496e96eefd46c929f9bdc", |
| 43 | + input=input, |
| 44 | + ) |
| 45 | + |
| 46 | + # Check if generation was successful |
| 47 | + if not output or len(output) == 0: |
| 48 | + return "Failed to generate image. Please try again." |
| 49 | + |
| 50 | + generated_image_url = output[0] if isinstance(output, list) else output |
| 51 | + |
| 52 | + # Download the generated image |
| 53 | + image_data: Optional[bytes] = None |
| 54 | + try: |
| 55 | + response = requests.get(generated_image_url) |
| 56 | + response.raise_for_status() |
| 57 | + image_data = response.content # get the actual image bytes in content into memory |
| 58 | + # Close the response to free up resources |
| 59 | + response.close() |
| 60 | + except Exception as e: |
| 61 | + return f"Failed to download generated image: {str(e)}" |
| 62 | + |
| 63 | + # Generate unique ID for the image |
| 64 | + image_id = str(uuid.uuid4()) |
| 65 | + |
| 66 | + # Upload to S3 |
| 67 | + try: |
| 68 | + s3_result = upload_generated_image_to_s3( |
| 69 | + image_data=image_data, image_id=image_id, user_id=user_id, prompt=prompt |
| 70 | + ) |
| 71 | + |
| 72 | + if s3_result["success"]: |
| 73 | + return f"Image generated successfully! User can find it his/her gallery. \ |
| 74 | + Image ID: {image_id}" |
| 75 | + else: |
| 76 | + return ( |
| 77 | + f"Image generated but failed to save: {s3_result.get('error', 'Unknown error')}" |
| 78 | + ) |
| 79 | + |
| 80 | + except Exception as e: |
| 81 | + # Clear image data from memory even if upload fails |
| 82 | + return f"Image generated but failed to save to storage: {str(e)}" |
| 83 | + |
| 84 | + finally: |
| 85 | + if image_data: |
| 86 | + # Clear image data from memory |
| 87 | + del image_data |
| 88 | + |
| 89 | + return [generate_image] |
0 commit comments