alanchen1115 commited on
Commit
a85a131
Β·
verified Β·
1 Parent(s): 8e6ac56

Delete Image_text_generation.py

Browse files
Files changed (1) hide show
  1. 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
-