Spaces:
Runtime error
Runtime error
| #!pip install -q -U google-generativeai | |
| import google.generativeai as genai | |
| import PIL.Image | |
| import image_converter as img_converter | |
| import random | |
| import os | |
| import ast | |
| import target_object | |
| #基本設定都放這邊---------------------------------------- | |
| # | |
| # | |
| # 設定圖檔位置 (此處僅為範例,純文字查詢時可忽略) | |
| image_path = r'G:\Python\tools\input_images\拾穗.jpeg' | |
| # 要使用的模型種類,免費版一分鐘只能跑最多十筆 | |
| gemini_model = 'gemini-2.5-flash' | |
| #-------------------------------------------------------- | |
| ## 替換冒號和逗號為換行符號 | |
| def replace_colon_comma_with_newline(input_string): | |
| processed_string = input_string.replace(':', '\n').replace(':', '\n').replace('],', ']\n') | |
| return processed_string | |
| def getApiToken(): | |
| try: | |
| my_api_key = os.getenv('my_api_key') | |
| my_list = ast.literal_eval(my_api_key) # Convert string to list因為存在環境變數中是字串格式 | |
| return random.choice(my_list) | |
| except Exception as e: | |
| return "" | |
| # function,輸入是文字或是圖檔的位置 | |
| def analyze_content_with_gemini_work(input_content): | |
| """ | |
| 透過 Gemini API 辨識內容,可處理純文字或圖片。 | |
| Args: | |
| input_content (str or PIL.Image.Image): | |
| 如果輸入是字串,則代表要辨識的文字訊息或圖片路徑。 | |
| 如果輸入是 PIL.Image.Image 物件,則直接使用該圖片。 | |
| user_prompt (str, optional): | |
| 使用者提供的自訂 prompt。如果為 None,則使用預設的 prompt。 | |
| Defaults to None. | |
| Returns: | |
| str: 辨識結果的文字描述。 | |
| """ | |
| my_api_key = getApiToken() # 從環境變數中獲取API金鑰 | |
| genai.configure(api_key=my_api_key) | |
| # 根據 user_prompt 決定要使用的 prompt | |
| prompt_to_use = str(target_object.work_JSON) | |
| # print("-"*50) | |
| # print(prompt_to_use) | |
| try: | |
| # 判斷輸入的類型 | |
| if isinstance(input_content, str): | |
| # 如果輸入是字串,嘗試判斷是否為圖片路徑 | |
| if input_content.lower().endswith(('.png', '.jpg', '.jpeg', '.gif','.webp')): | |
| if input_content.lower().endswith(('.webp')): | |
| input_content = img_converter.convert_webp_to_jpg(input_content) # 如果是 webp 圖片,先轉換為 jpg | |
| model = genai.GenerativeModel(gemini_model) | |
| image_obj = PIL.Image.open(input_content) | |
| response = model.generate_content([prompt_to_use, image_obj]) | |
| else: | |
| # 純文字輸入 | |
| model = genai.GenerativeModel(gemini_model) | |
| response = model.generate_content(input_content) # 純文字直接使用輸入內容當 prompt | |
| elif isinstance(input_content, PIL.Image.Image): | |
| model = genai.GenerativeModel(gemini_model) | |
| response = model.generate_content([prompt_to_use, input_content]) | |
| else: | |
| return "錯誤:輸入必須是文字、圖片路徑(字串)或 PIL.Image 物件。" | |
| return replace_colon_comma_with_newline(response.text) | |
| except Exception as e: | |
| return f"發生錯誤:{e}" | |
| if __name__ == '__main__': | |
| # --- 程式碼使用範例 --- | |
| # 範例 1:傳送純文字訊息 | |
| # print("正在處理純文字訊息...") | |
| # text_message = "你好,請簡要說明一下Python是什麼?" | |
| # response_text = analyze_content_with_gemini(text_message) | |
| # print("回應結果:") | |
| # print(response_text) | |
| # print("-" * 20) | |
| # 範例 2:傳送圖片路徑 | |
| # 請確保 image_path 指向有效的圖片檔案 | |
| print("正在處理圖片訊息...") | |
| response_image = analyze_content_with_gemini_work(image_path) | |
| print("回應結果:") | |
| print(response_image) | |
| print("-" * 20) | |