Spaces:
Sleeping
Sleeping
Delete gemini.py
Browse files
gemini.py
DELETED
|
@@ -1,57 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from io import BytesIO
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import gradio as gr
|
| 5 |
-
from google import genai
|
| 6 |
-
from google.genai import types
|
| 7 |
-
import logging
|
| 8 |
-
|
| 9 |
-
# ่จญๅฎ logging
|
| 10 |
-
logging.basicConfig(
|
| 11 |
-
filename='app.log',
|
| 12 |
-
level=logging.INFO,
|
| 13 |
-
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
# ่จญๅฎ Gemini API ้้ฐ
|
| 17 |
-
# === ๅๅงๅ Google Gemini ===
|
| 18 |
-
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 19 |
-
client = genai.Client(api_key=GEMINI_API_KEY)
|
| 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",
|
| 27 |
-
contents=prompt,
|
| 28 |
-
config=types.GenerateContentConfig(
|
| 29 |
-
response_modalities=["TEXT", "IMAGE"]
|
| 30 |
-
),
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
# ่็ๅๆไธญ็ๅ็
|
| 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:
|
| 45 |
-
gr.Markdown("## ๐ผ๏ธ Gemini ๅ็็ๆๅจ")
|
| 46 |
-
prompt_input = gr.Textbox(label="่ผธๅ
ฅๆ็คบ่ฉ", placeholder="ไพๅฆ๏ผไธ้ปๆด่ๅขจ้ก็่ฒๅจๆฒ็ไธ")
|
| 47 |
-
generate_button = gr.Button("็ๆๅ็")
|
| 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 |
-
|
| 56 |
-
if __name__ == "__main__":
|
| 57 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|