Spaces:
Sleeping
Sleeping
File size: 1,188 Bytes
ae03257 83e409c ae03257 83e409c ae03257 83e409c ae03257 83e409c ae03257 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import os
import logging
from PIL import Image
import gradio as gr
import google.generativeai as genai
# ่จญๅฎ 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")
genai.configure(api_key=GEMINI_API_KEY)
google_client = genai.GenerativeModel(model_name="gemini-2.0-flash")
# ๅฎ็พฉใๅ่งฃ้ๆใๅ่ฝ
def explain_image(image: Image.Image):
# ็ดๆฅๆ PIL image ๅณ้ฒๅป
response = google_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()
|