VDC commited on
Commit
c5599c3
·
1 Parent(s): 3e9e130

added imagen gen

Browse files
Files changed (3) hide show
  1. .idea/.gitignore +3 -0
  2. .idea/vcs.xml +4 -0
  3. app.py +37 -2
.idea/.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
.idea/vcs.xml ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings" defaultProject="true" />
4
+ </project>
app.py CHANGED
@@ -4,10 +4,45 @@ import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
-
8
  from Gradio_UI import GradioUI
 
 
 
 
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  @tool
12
  def generate_image_from_prompt(prompt: str) -> str:
13
  """Generates an image from a text prompt.
@@ -54,7 +89,7 @@ with open("prompts.yaml", 'r') as stream:
54
 
55
  agent = CodeAgent(
56
  model=model,
57
- tools=[final_answer, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
58
  max_steps=6,
59
  verbosity_level=1,
60
  grammar=None,
 
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
 
7
  from Gradio_UI import GradioUI
8
+ from diffusers import StableDiffusionPipeline
9
+ import torch
10
+ from io import BytesIO
11
+ import base64
12
 
13
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
14
+
15
+ @tool
16
+
17
+ class ImageGenerator:
18
+ def __init__(self, model_id="runwayml/stable-diffusion-v1-5", device="cuda" if torch.cuda.is_available() else "cpu"):
19
+ self.pipeline = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32).to(device)
20
+ self.device = device
21
+
22
+ def generate_image(self, prompt, num_inference_steps=25, guidance_scale=7.5):
23
+ """Generates an image from a text prompt."""
24
+ image = self.pipeline(prompt, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale).images[0]
25
+ return image
26
+
27
+ def generate_base64_image(self, prompt, num_inference_steps=25, guidance_scale=7.5):
28
+ """Generates a base64 encoded image from a text prompt."""
29
+ image = self.generate_image(prompt, num_inference_steps, guidance_scale)
30
+ buffered = BytesIO()
31
+ image.save(buffered, format="PNG")
32
+ img_str = base64.b64encode(buffered.getvalue()).decode()
33
+ return img_str
34
+
35
+ def generate_image_tool(image_generator):
36
+ """Creates a tool function for image generation."""
37
+ def image_generation_tool(prompt):
38
+ """Generates an image from a prompt."""
39
+ return image_generator.generate_base64_image(prompt)
40
+ return image_generation_tool
41
+
42
+ # Initialize the ImageGenerator and tool
43
+ image_generator = ImageGenerator()
44
+ image_generation_tool_function = generate_image_tool(image_generator)
45
+
46
  @tool
47
  def generate_image_from_prompt(prompt: str) -> str:
48
  """Generates an image from a text prompt.
 
89
 
90
  agent = CodeAgent(
91
  model=model,
92
+ tools=[final_answer, generate_image_from_prompt, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
93
  max_steps=6,
94
  verbosity_level=1,
95
  grammar=None,