Spaces:
Runtime error
Runtime error
Delete Image_text_generation.py
Browse files- Image_text_generation.py +0 -57
Image_text_generation.py
DELETED
|
@@ -1,57 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import io
|
| 3 |
-
import tempfile
|
| 4 |
-
from datetime import datetime
|
| 5 |
-
import PIL.Image
|
| 6 |
-
from google import genai
|
| 7 |
-
from google.genai import types
|
| 8 |
-
import httpx
|
| 9 |
-
|
| 10 |
-
genai_client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
|
| 11 |
-
|
| 12 |
-
class Image_text_Generator:
|
| 13 |
-
def __init__(self, user_id):
|
| 14 |
-
|
| 15 |
-
self.user_id = user_id
|
| 16 |
-
|
| 17 |
-
def generate_image_with_gemini(self, prompt):
|
| 18 |
-
"""
|
| 19 |
-
δ½Ώη¨ Gemini 樑εηζεηγ
|
| 20 |
-
|
| 21 |
-
εζΈ:
|
| 22 |
-
prompt (str): η¨ζΌηζεηηζη€Ίθ©γ
|
| 23 |
-
|
| 24 |
-
θΏε:
|
| 25 |
-
bytes: ηζηεηηδΊι²δ½θ³ζοΌε¦ζηζε€±ζεθΏε Noneγ
|
| 26 |
-
"""
|
| 27 |
-
response = genai_client.models.generate_content(
|
| 28 |
-
model="gemini-2.0-flash-exp",
|
| 29 |
-
contents=prompt,
|
| 30 |
-
config=types.GenerateContentConfig(response_modalities=['Text', 'Image'])
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
for part in response.candidates[0].content.parts:
|
| 34 |
-
if part.text is not None:
|
| 35 |
-
print(part.text)
|
| 36 |
-
elif part.inline_data is not None:
|
| 37 |
-
return part.inline_data.data
|
| 38 |
-
return None
|
| 39 |
-
|
| 40 |
-
def upload_image_to_tmp(self, image_binary):
|
| 41 |
-
"""
|
| 42 |
-
θΏε:
|
| 43 |
-
str: δΈε³εΎηεη URLοΌε¦ζδΈε³ε€±ζεθΏε Noneγ
|
| 44 |
-
"""
|
| 45 |
-
try:
|
| 46 |
-
# ε°δΊι²δ½θ³ζθ½ζηΊ PIL Image
|
| 47 |
-
image = PIL.Image.open(io.BytesIO(image_binary))
|
| 48 |
-
|
| 49 |
-
# ε»Ίη«ζ«εζͺζ‘δ»₯δΎΏδΈε³
|
| 50 |
-
image.save('static/'+self.user_id+'.png', format='PNG')
|
| 51 |
-
|
| 52 |
-
# θΏεεηηι£η΅
|
| 53 |
-
return 'static/'+self.user_id+'.png'
|
| 54 |
-
except Exception as e:
|
| 55 |
-
print(f"εηδΈε³ε€±ζ: {e}")
|
| 56 |
-
return None
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|