QuentinL52 commited on
Commit
ca6e0ed
·
verified ·
1 Parent(s): c36cc82

Create solle_img_generation.py

Browse files
Files changed (1) hide show
  1. src/solle_img_generation.py +43 -0
src/solle_img_generation.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import mimetypes
3
+ from google import genai
4
+ from google.genai import types
5
+ from .config import load_prompt_template
6
+
7
+
8
+ class imggenrator:
9
+ def __init__(self):
10
+ self.api_key = os.getenv("IMG_GEN_API_KEY")
11
+ self.model = os.getenv("model")
12
+ self.client = genai.Client(api_key=self.api_key)
13
+
14
+ # Notez que le paramètre `save_fn` n'est plus nécessaire.
15
+ def generate(self, image_path: str, user_input_text: str):
16
+ with open(image_path, "rb") as f:
17
+ image_data = f.read()
18
+ image_mime_type = mimetypes.guess_type(image_path)[0]
19
+ full_prompt = load_prompt_template(image_path, user_input_text)
20
+ contents = [
21
+ types.Content(
22
+ role="user",
23
+ parts=[
24
+ types.Part.from_bytes(data=image_data, mime_type=image_mime_type),
25
+ types.Part.from_text(text=full_prompt),
26
+ ],
27
+ ),
28
+ ]
29
+ config = types.GenerateContentConfig(response_modalities=["IMAGE", "TEXT"])
30
+ generated_images_data = []
31
+
32
+ for chunk in self.client.models.generate_content_stream(
33
+ model=self.model,
34
+ contents=contents,
35
+ config=config,
36
+ ):
37
+ if chunk.candidates and chunk.candidates[0].content and chunk.candidates[0].content.parts:
38
+ for part in chunk.candidates[0].content.parts:
39
+ if hasattr(part, "inline_data") and part.inline_data:
40
+ generated_images_data.append(part.inline_data.data)
41
+ elif hasattr(part, "text") and part.text:
42
+ print(part.text)
43
+ return generated_images_data