当前位置: 首页 > news >正文

wordpress读者墙不显示企业网站优化问题

wordpress读者墙不显示,企业网站优化问题,网站开发设计新闻界面,php网页制作实例【多模态处理】利用GPT逐一读取本地图片并生成描述#xff0c;支持崩溃后从最新进度恢复题 代码功能#xff1a;核心功能最后碎碎念 代码#xff08;使用中转平台url#xff09;#xff1a;代码#xff08;直接使用openai的key#xff09; 注意 代码功能#xff1a; 读… 【多模态处理】利用GPT逐一读取本地图片并生成描述支持崩溃后从最新进度恢复题 代码功能核心功能最后碎碎念 代码使用中转平台url代码直接使用openai的key 注意 代码功能 读取本地图片文件并使用GPT模型生成图像的元数据描述。生成的结果会保存到一个JSON文件中。代码还包含了检查点机制以便在处理过程中程序崩溃时能够从最新的位置继续生成。 核心功能 读取文件并设置变量 从JSON文件中读取图像路径、宽度和高度等变量。根据读取的变量设置prompt调用GPT模型。 调用GPT模型 使用openai.ChatCompletion.create方法调用GPT模型生成图像的元数据描述。将生成的结果保存到JSON文件中。 保存输出到JSON 每处理一张图片就将结果追加到JSON文件中。 使用检查点机制 每处理一张图片后保存当前处理的位置。如果处理过程中出现错误程序可以从上次保存的位置继续处理。 处理本地图片文件 从本地文件夹读取图片文件并对每张图片进行处理。 最后碎碎念 提供一个模板方便大家理解其思想使用的时候可以和openai最基本的代码对比着看 代码使用中转平台url 使用中转平台需要设置中转平台url from PIL import Image import os import base64 import openai import pickle import json# 设置API密钥和中转平台URL API_SECRET_KEY your_api_secret_key BASE_URL https://api.your_base_url.com/v1# 图像文件夹路径 image_directory_path your_image_directory_path# 设置要处理的图像数量 number_of_images_to_process 50# 输出文件路径 output_file_path output_results.json# 初始化计数器 image_counter 0# 读取 JSON 数据文件 data_file your_data_file.json with open(data_file, r) as f:data json.load(f)def encode_image(image_path):with open(image_path, rb) as image_file:return base64.b64encode(image_file.read()).decode(utf-8)def get_image_details(image_path):获取图像的详细信息包括图像ID和尺寸。参数:image_path (str): 图像文件的路径。返回:tuple: 包含图像ID文件名不包括扩展名和图像尺寸宽度高度的元组。示例:get_image_details(path/to/image.jpg) - (image, (800, 600))image_filename os.path.basename(image_path)image_id os.path.splitext(image_filename)[0]with Image.open(image_path) as img:image_size img.sizereturn image_id, image_sizedef chat_completions(image_path, width, height):base64_image encode_image(image_path)image_id, image_size get_image_details(image_path)client OpenAI(api_keyAPI_SECRET_KEY, base_urlBASE_URL)response openai.ChatCompletion.create(modelgpt-4,messages[{role: system, content: You are an assistant that provides metadata information about images.},{role: user, content: fImage ID: {image_id}, Width: {width}, Height: {height}}],max_tokens3000,timeout999,)return response# 初始化结果字典 results_dict {}# 检查是否存在检查点文件 checkpoint_file checkpoint.pkl if os.path.exists(checkpoint_file):with open(checkpoint_file, rb) as f:start_index pickle.load(f) else:start_index 0# 处理图像文件 for i, image in enumerate(data[start_index:], startstart_index):image_name image[image_path]image_file os.path.join(image_directory_path, image_name)image_width image[width]image_height image[height]if image_name.lower().endswith((.png, .jpg, .jpeg, .tiff, .bmp, .gif)):try:response chat_completions(image_file, image_width, image_height)result {image_name: response.choices[0].message[content]}with open(output_file_path, a) as output_file:output_file.write(json.dumps(result) \n)except Exception as e:print(fError processing image {image_name}: {e})continueimage_counter 1if image_counter number_of_images_to_process:breakwith open(checkpoint_file, wb) as f:pickle.dump(i1, f) 代码直接使用openai的key from PIL import Image import os import base64 import openai import pickle import json# 设置API密钥 API_SECRET_KEY your_api_secret_key# 图像文件夹路径 image_directory_path your_image_directory_path# 设置要处理的图像数量 number_of_images_to_process 50# 输出文件路径 output_file_path output_results.json# 初始化计数器 image_counter 0# 读取 JSON 数据文件 data_file your_data_file.json with open(data_file, r) as f:data json.load(f)def encode_image(image_path):with open(image_path, rb) as image_file:return base64.b64encode(image_file.read()).decode(utf-8)def get_image_details(image_path):获取图像的详细信息包括图像ID和尺寸。参数:image_path (str): 图像文件的路径。返回:tuple: 包含图像ID文件名不包括扩展名和图像尺寸宽度高度的元组。示例:get_image_details(path/to/image.jpg) - (image, (800, 600))image_filename os.path.basename(image_path)image_id os.path.splitext(image_filename)[0]with Image.open(image_path) as img:image_size img.sizereturn image_id, image_sizedef chat_completions(image_path, width, height):base64_image encode_image(image_path)image_id, image_size get_image_details(image_path)openai.api_key API_SECRET_KEYresponse openai.ChatCompletion.create(modelgpt-4,messages[{role: system, content: You are an assistant that provides metadata information about images.},{role: user, content: fImage ID: {image_id}, Width: {width}, Height: {height}}],max_tokens3000,timeout999,)return response# 初始化结果字典 results_dict {}# 检查是否存在检查点文件 checkpoint_file checkpoint.pkl if os.path.exists(checkpoint_file):with open(checkpoint_file, rb) as f:start_index pickle.load(f) else:start_index 0# 处理图像文件 for i, image in enumerate(data[start_index:], startstart_index):image_name image[image_path]image_file os.path.join(image_directory_path, image_name)image_width image[width]image_height image[height]if image_name.lower().endswith((.png, .jpg, .jpeg, .tiff, .bmp, .gif)):try:response chat_completions(image_file, image_width, image_height)result {image_name: response.choices[0].message[content]}with open(output_file_path, a) as output_file:output_file.write(json.dumps(result) \n)except Exception as e:print(fError processing image {image_name}: {e})continueimage_counter 1if image_counter number_of_images_to_process:breakwith open(checkpoint_file, wb) as f:pickle.dump(i1, f) 注意 上面的代码最后四行先判断处理图像数量是否大于规定处理图像数量再保存checkpoint if image_counter number_of_images_to_process:break with open(checkpoint_file, wb) as f:pickle.dump(i1, f)有时候要替换逻辑为这样先保存checkpoint再判断处理图像数量是否大于规定处理图像数量 with open(checkpoint_file, wb) as f:pickle.dump(i1, f) if image_counter number_of_images_to_process:break然后每次程序运行结束时比如5.jpg处理完第二次运行程序不是再处理一遍5.jpg而是从6.jpg开始。但有的时候不用替换也仍然从6.jpg开始不知道为什么。 但确实下方替换后更好一点因为有的时候break完后直接跳出循环导致最后一次的i1没有更新。
http://www.hkea.cn/news/14542330/

相关文章:

  • 哈尔滨网站建设哪家好而且价格不贵wordpress两个域名访问
  • 网站建设打造学院建设招标网官方网站电脑版
  • 青海公司网站建设哪家快新媒体营销思维
  • 纯文字网站设计网站建设人员工作计划
  • 网站新闻怎么写网页设计师培训 网页设计师培训
  • 酒店网站建设描述个人网站的主题
  • 网站分为哪些结构wordpress下载页插件
  • 汽车零件销售网站开发平面设计接单攻略电子书
  • 网站呢建设邢台今天的招工信息
  • 移动端购物网站建设作图神器
  • 12306网站很难做吗网页设计与制作教程资源
  • 自己做网站 需要哪些大连网站建设开发
  • 网站的构造河南注册公司代理
  • 东莞seo网站建设公司wordpress 域名更改 页面链接
  • 网站优化要做哪些工作赣县网站建设
  • 榆次网站建设网站改版404页面
  • 不动产登记网站建设做网站猫腻大吗
  • 乐清网络网站建设厦门seo网站推广
  • 昆山苏州网站建设wordpress上传媒体文件大小修改
  • 受欢迎的唐山网站建设学校asp网站
  • 天猫网站设计分析网站平台建设可行性
  • 做网站要看什么书品牌建设最高境界是培育客户成为
  • 成都企业网站开发公司社交网络的推广方法有哪些
  • 营销型网站和传统网站区别百度收录提交
  • 行业导航类网站模板宜昌做网站
  • 优化网站速度的要点网站色彩搭配表
  • 新闻类网站设计外卖网站建设
  • 网站布局方法分类公司电商网站开发
  • 刷网站百度关键词软件齐装网装修公司
  • 那些网站做网批外币投资理财网站开发