huchiahsi commited on
Commit
95645e7
ยท
verified ยท
1 Parent(s): 01c2e26

Update gemini.py

Browse files
Files changed (1) hide show
  1. gemini.py +12 -24
gemini.py CHANGED
@@ -1,13 +1,12 @@
1
  import os
2
- import uuid
3
  from io import BytesIO
4
  from PIL import Image
5
  import gradio as gr
6
- from google import genai
7
- from google.genai import types
8
  import logging
9
 
10
- # ่จญๅฎšlogging
11
  logging.basicConfig(
12
  filename='app.log',
13
  level=logging.INFO,
@@ -16,18 +15,12 @@ logging.basicConfig(
16
 
17
  # ่จญๅฎš Gemini API ้‡‘้‘ฐ
18
  GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
19
- client = genai.Client(api_key=GEMINI_API_KEY)
20
-
21
- # ่จญๅฎšๅœ–็‰‡ๅ„ฒๅญ˜็›ฎ้Œ„
22
- STATIC_IMAGE_PATH = "static/images"
23
- os.makedirs(STATIC_IMAGE_PATH, exist_ok=True)
24
-
25
- # ๅ–ๅพ— Hugging Face Space ็š„ไธปๆฉŸๅ็จฑ
26
- SPACE_HOST = os.environ.get("SPACE_HOST", "your-space-name.hf.space")
27
 
28
  def generate_image(prompt):
29
  """
30
- ไฝฟ็”จ Gemini API ๆ นๆ“šๆ็คบ่ฉž็”Ÿๆˆๅœ–็‰‡๏ผŒไธฆ่ฟ”ๅ›žๅœ–็‰‡็š„ๅ…ฌ้–‹ URLใ€‚
31
  """
32
  response = client.models.generate_content(
33
  model="gemini-2.0-flash-exp-image-generation",
@@ -41,16 +34,11 @@ def generate_image(prompt):
41
  for part in response.candidates[0].content.parts:
42
  if part.inline_data is not None:
43
  image = Image.open(BytesIO(part.inline_data.data))
44
- filename = f"{uuid.uuid4().hex}.png"
45
- image_path = os.path.join(STATIC_IMAGE_PATH, filename)
46
- image.save(image_path)
47
-
48
- # ๅปบ็ซ‹ๅœ–็‰‡็š„ๅ…ฌ้–‹ URL
49
- image_url = f"https://{SPACE_HOST}/static/images/{filename}"
50
- logging.info(f"็”Ÿๆˆ็š„ๅœ–็‰‡ URL: {image_url}")
51
- return image_url
52
 
53
- return "ๆœช่ƒฝ็”Ÿๆˆๅœ–็‰‡๏ผŒ่ซ‹ๅ˜—่ฉฆๅ…ถไป–ๆ็คบ่ฉžใ€‚"
 
54
 
55
  # ๅปบ็ซ‹ Gradio ไป‹้ข
56
  with gr.Blocks() as demo:
@@ -60,8 +48,8 @@ with gr.Blocks() as demo:
60
  image_output = gr.Image(label="็”Ÿๆˆ็š„ๅœ–็‰‡")
61
 
62
  def on_generate(prompt):
63
- image_url = generate_image(prompt)
64
- return image_url
65
 
66
  generate_button.click(fn=on_generate, inputs=prompt_input, outputs=image_output)
67
 
 
1
  import os
 
2
  from io import BytesIO
3
  from PIL import Image
4
  import gradio as gr
5
+ import google.generativeai as genai
6
+ from google.generativeai import types
7
  import logging
8
 
9
+ # ่จญๅฎš logging
10
  logging.basicConfig(
11
  filename='app.log',
12
  level=logging.INFO,
 
15
 
16
  # ่จญๅฎš Gemini API ้‡‘้‘ฐ
17
  GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
18
+ genai.configure(api_key=GEMINI_API_KEY)
19
+ client = genai.Client()
 
 
 
 
 
 
20
 
21
  def generate_image(prompt):
22
  """
23
+ ไฝฟ็”จ Gemini API ๆ นๆ“šๆ็คบ่ฉž็”Ÿๆˆๅœ–็‰‡๏ผŒไธฆ่ฟ”ๅ›ž PIL ๅœ–ๅƒ็‰ฉไปถใ€‚
24
  """
25
  response = client.models.generate_content(
26
  model="gemini-2.0-flash-exp-image-generation",
 
34
  for part in response.candidates[0].content.parts:
35
  if part.inline_data is not None:
36
  image = Image.open(BytesIO(part.inline_data.data))
37
+ logging.info("ๆˆๅŠŸ็”Ÿๆˆๅœ–็‰‡ใ€‚")
38
+ return image
 
 
 
 
 
 
39
 
40
+ logging.warning("ๆœช่ƒฝ็”Ÿๆˆๅœ–็‰‡๏ผŒ่ซ‹ๅ˜—่ฉฆๅ…ถไป–ๆ็คบ่ฉžใ€‚")
41
+ return None
42
 
43
  # ๅปบ็ซ‹ Gradio ไป‹้ข
44
  with gr.Blocks() as demo:
 
48
  image_output = gr.Image(label="็”Ÿๆˆ็š„ๅœ–็‰‡")
49
 
50
  def on_generate(prompt):
51
+ image = generate_image(prompt)
52
+ return image
53
 
54
  generate_button.click(fn=on_generate, inputs=prompt_input, outputs=image_output)
55