Update main.py
Browse files
    	
        main.py
    CHANGED
    
    | @@ -98,20 +98,22 @@ def analyze_with_gemini(image_path, user_text): | |
| 98 | 
             
                    if not os.path.exists(image_path):
         | 
| 99 | 
             
                        raise FileNotFoundError(f"圖片路徑無效:{image_path}")
         | 
| 100 |  | 
| 101 | 
            -
             | 
| 102 | 
            -
             | 
| 103 | 
            -
             | 
| 104 | 
            -
             | 
|  | |
|  | |
| 105 |  | 
| 106 | 
             
                    # 構建 Gemini 的請求
         | 
| 107 | 
             
                    prompt = f"""
         | 
| 108 | 
             
                    用戶的問題是:{user_text}
         | 
| 109 | 
            -
                    圖片已上傳,Base64  | 
| 110 | 
             
                    {image_base64}
         | 
| 111 | 
             
                    請根據圖片和問題進行詳細分析。
         | 
| 112 | 
             
                    """
         | 
| 113 | 
             
                    # 使用 Gemini API 請求
         | 
| 114 | 
            -
                    response = model.generate_content( | 
| 115 |  | 
| 116 | 
             
                    # 提取回應內容
         | 
| 117 | 
             
                    return response.parts[0].text
         | 
|  | |
| 98 | 
             
                    if not os.path.exists(image_path):
         | 
| 99 | 
             
                        raise FileNotFoundError(f"圖片路徑無效:{image_path}")
         | 
| 100 |  | 
| 101 | 
            +
                     # 打開本地圖片文件並將其轉換為 Base64 編碼
         | 
| 102 | 
            +
                        with open(image_path, "rb") as image_file:
         | 
| 103 | 
            +
                            image_binary = image_file.read()
         | 
| 104 | 
            +
             | 
| 105 | 
            +
                    # 將二進制數據編碼為 Base64
         | 
| 106 | 
            +
                    image_base64 = base64.b64encode(image_binary).decode('utf-8')
         | 
| 107 |  | 
| 108 | 
             
                    # 構建 Gemini 的請求
         | 
| 109 | 
             
                    prompt = f"""
         | 
| 110 | 
             
                    用戶的問題是:{user_text}
         | 
| 111 | 
            +
                    圖片已上傳,Base64 編碼如下:
         | 
| 112 | 
             
                    {image_base64}
         | 
| 113 | 
             
                    請根據圖片和問題進行詳細分析。
         | 
| 114 | 
             
                    """
         | 
| 115 | 
             
                    # 使用 Gemini API 請求
         | 
| 116 | 
            +
                    response = model.generate_content([{'mime_type':'image/png', 'data': image_base64}, prompt])
         | 
| 117 |  | 
| 118 | 
             
                    # 提取回應內容
         | 
| 119 | 
             
                    return response.parts[0].text
         | 

