Spaces:
Running
Running
import os | |
from PIL import Image | |
import gradio as gr | |
from google import genai | |
import logging | |
# ่จญๅฎ logging | |
logging.basicConfig( | |
filename='app.log', | |
level=logging.INFO, | |
format='%(asctime)s - %(levelname)s - %(message)s' | |
) | |
# ๅๅงๅ Gemini API | |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY") | |
client = genai.Client(api_key=GEMINI_API_KEY) | |
# ๅฎ็พฉใๅ่งฃ้ๆใๅ่ฝ | |
def explain_image(image: Image.Image): | |
# ็ดๆฅๆ PIL image ๅณ้ฒๅป | |
response = client.generate_content( | |
contents=[image, "่ซ็จ็น้ซไธญๆๆ่ฟฐ้ๅผตๅ็"] | |
) | |
# ๅๅบๅ็ญ | |
explanation = response.text | |
logging.info("ๅ็่ชชๆๆๅๅๅพใ") | |
return explanation | |
# Gradio ไป้ข | |
with gr.Blocks() as demo: | |
gr.Markdown("## ๐ง Gemini ๅ็่งฃ้ๅจ๏ผๅ โ ๆ๏ผ") | |
image_input = gr.Image(type="pil", label="ไธๅณๅ็") | |
explain_button = gr.Button("่งฃ้ๅ็") | |
output_text = gr.Textbox(label="ๅ็่ชชๆ", lines=5) | |
explain_button.click(fn=explain_image, inputs=image_input, outputs=output_text) | |
if __name__ == "__main__": | |
demo.launch() | |