|
import os |
|
import io |
|
import tempfile |
|
from datetime import datetime |
|
import PIL.Image |
|
from google import genai |
|
from google.genai import types |
|
import httpx |
|
|
|
genai_client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"]) |
|
|
|
class Image_text_Generator: |
|
def __init__(self): |
|
pass |
|
|
|
def generate_image_with_gemini(self, prompt): |
|
""" |
|
δ½Ώη¨ Gemini 樑εηζεηγ |
|
|
|
εζΈ: |
|
prompt (str): η¨ζΌηζεηηζη€Ίθ©γ |
|
|
|
θΏε: |
|
bytes: ηζηεηηδΊι²δ½θ³ζοΌε¦ζηζε€±ζεθΏε Noneγ |
|
""" |
|
response = genai_client.models.generate_content( |
|
model="gemini-2.0-flash", |
|
contents=prompt, |
|
config=types.GenerateContentConfig(response_modalities=['Text', 'Image']) |
|
) |
|
|
|
for part in response.candidates[0].content.parts: |
|
if part.text is not None: |
|
print(part.text) |
|
elif part.inline_data is not None: |
|
return part.inline_data.data |
|
return None |
|
|
|
def upload_image_to_tmp(self, image_binary): |
|
""" |
|
θΏε: |
|
str: δΈε³εΎηεη URLοΌε¦ζδΈε³ε€±ζεθΏε Noneγ |
|
""" |
|
try: |
|
|
|
image = PIL.Image.open(io.BytesIO(image_binary)) |
|
|
|
|
|
image.save('tmp/temp.png', format='PNG') |
|
|
|
|
|
return 'tmp/temp.png' |
|
except Exception as e: |
|
print(f"εηδΈε³ε€±ζ: {e}") |
|
return None |
|
|
|
def generate_txt_with_gemini(self, prompt,doc_url): |
|
doc_data = httpx.get(doc_url).content |
|
|
|
response = client.models.generate_content( |
|
model="gemini-2.0-flash", |
|
contents=[ |
|
types.Part.from_bytes(data=doc_data, mime_type='application/pdf',),prompt] |
|
) |
|
|
|
return response.text |
|
|
|
|