烙AI改变生活:最近都在说月之暗面的kimi的各项能力吊打国内其他大模型,今天我们真实感受下 kimi、通义千问、文心一言的根据需求写代码的能力。

测评结果让人震惊!

kimi

kimi编程过程

我们先看一下热捧的月之暗面的kimi模型。

第一次运行有错误,很正常,我们继续把错误发给kimi,让他修复。

kimi仿佛进入了错误死循环,始终无法修复错误,我认为他可能上下文理解错乱,所以重新描述了自己的需求,让他重新作答。结果依然无法使用。

而且错误变得很离谱,连包都没有导入。

又经过多轮对话,我发现他始终无法修复自己的错误。 最终我放弃和他的对话。

最终代码:

kimi的代码越改越复杂,使用canvas更复杂,有大部分错误发生在canvas绘制和字体选择上,没编程基础,都无法调试。

import os

from tkinter import Tk, Button, Label, Entry, filedialog, messagebox

from PIL import Image

from reportlab.pdfbase import pdfmetrics

from reportlab.pdfbase.ttfonts import TTFont

from reportlab.pdfgen import canvas

from reportlab.lib.pagesizes import A4

# 注册字体

def register_font(font_name, font_path):

try:

pdfmetrics.registerFont(TTFont(font_name, font_path))

except IOError as e:

print("Error registering font:", e)

# 合并图片到PDF

def merge_images_to_pdf(folder_path, output_pdf_path):

register_font('SimSun', r'C:\Windows\Fonts\simsun.ttc') # 确保提供正确的字体文件路径

pdf = canvas.Canvas(output_pdf_path, pagesize=A4)

current_page = 1

page_height = pdf._pagesize[1]

images = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp'))]

for img_path in images:

img = Image.open(img_path).convert('RGB') # 确保图像为RGB模式

width, height = img.size

if height > page_height:

# 如果图片高度大于页面高度,需要创建新页面

pdf.showPage()

current_page += 1

page_height = pdf._pagesize[1]

pdf.drawImage(img, width, height, x=(A4[0] - width) / 2, y=page_height - height)

pdf.save()

messagebox.showinfo("Success", "PDF Created Successfully!")

# 选择文件夹

def select_folder(entry):

folder_path = filedialog.askdirectory()

entry.delete(0, 'end')

entry.insert(0, folder_path)

# GUI应用程序的主函数

def main():

root = Tk()

root.title("Image to PDF Converter")

Label(root, text="Select the folder containing images:").pack()

folder_path_entry = Entry(root, width=50)

folder_path_entry.pack()

Button(root, text="Browse", command=lambda: select_folder(folder_path_entry)).pack()

Button(root, text="Run", command=lambda: merge_images_to_pdf(folder_path_entry.get(), "merged.pdf")).pack()

root.mainloop()

if __name__ == "__main__":

main()

结论:kimi代码编写失败。

通义千问

通义千问编码过程

无一例外第一次都会报错。

很惊喜,通义千问修改代码后,代码就运行成功。

我们看下生成的pdf效果。

pdf内容是出来了,我们再让他优化下程序,将UI调大,pdf中图片调成一样大小。

有一个错误,让他修复,修复后成功。

最终UI效果:

最终PDF内容:

最终代码:

通义千问给的代码更少,选择的库img2pdf比较合适。

import os

import tkinter as tk

from tkinter import filedialog

import img2pdf

'''

模型:通义千问

需求:把一个文件夹中的图片转换成pdf文件,要求有一个界面,可以选择图片所在路径,然后点击运行,可以生产pdf文件,然后输出pdf所在路径。请保证相关的包都导入,如果用到字体,使用宋体常规。

UI大小适中,pdf内容图片一样大小。

'''

def select_folder():

folder_path = filedialog.askdirectory()

if folder_path:

pdf_output_path = os.path.join(folder_path, "output.pdf")

images_list = [os.path.join(folder_path, img) for img in os.listdir(folder_path) if img.endswith(('.png', '.jpg', '.jpeg'))]

# 按照文件名排序,确保顺序正确

images_list.sort()

with open(pdf_output_path, "wb") as f:

pdf_bytes = img2pdf.convert(images_list)

f.write(pdf_bytes)

print(f"PDF文件已生成并保存至:{pdf_output_path}")

def main():

root = tk.Tk()

root.title("图片转PDF工具")

browse_button = tk.Button(root, text="选择图片文件夹", command=select_folder)

browse_button.pack(pady=10)

root.mainloop()

if __name__ == "__main__":

main()

结论:通义千问代码编写成功。

上面还有一个小问题,是通义千问给的UI并没有“运行”按钮,如果需要,可以继续让他添加按钮,但是已经很好了。

文心一言

文心一言编码过程

文心一言给的这段代码连编译都错误,很明显下面代码中少了一个右括号

img = img.resize((int(img_width * 0.75), int(img_height * 0.75)), Image.ANTIALIAS

经过我的提示,它好像明白了,但是给的修正后的代码依然没有添加右括号。 算了,原谅它的无知,我手动添加括号试试。

文心一言后面就和kimi一样,进入了错误循环。 大概又经过4次调试,代码终于调通,能够生成pdf,但是文件内容不完整。

最终代码

文心一言和kimi给的代码非常多,使用canvas库,复杂度更高,更难以调试。

from tkinter import Tk, filedialog, Label, Button, Entry, mainloop

from PIL import Image

from reportlab.pdfgen import canvas

from reportlab.lib.pagesizes import letter

import os

def resize_image(image_path, max_width=None, max_height=None):

"""

Resize an image to fit within the given maximum width and height.

Aspect ratio is preserved.

"""

with Image.open(image_path) as img:

orig_width, orig_height = img.size

if max_width is None and max_height is None:

# No resizing needed if no max width or height is specified

return img

if max_width is None:

max_width = orig_width

if max_height is None:

max_height = orig_height

# Calculate the scaling factor to fit the image

width_ratio = max_width / float(orig_width)

height_ratio = max_height / float(orig_height)

ratio = min(width_ratio, height_ratio)

# Resize the image

new_size = (int(orig_width * ratio), int(orig_height * ratio))

resized_img = img.resize(new_size, Image.ANTIALIAS)

return resized_img

def convert_images_to_pdf(image_folder, output_pdf):

images = [img for img in os.listdir(image_folder) if

img.lower().endswith((".png", ".jpg", ".jpeg", ".tiff", ".bmp", ".gif"))]

if not images:

print("No images found in the folder.")

return

c = canvas.Canvas(output_pdf, pagesize=letter)

height = letter[1] - 72 # 72 is the margin at the top

for img_name in images:

img_path = os.path.join(image_folder, img_name)

# Resize the image to fit the page width (or a specified maximum size)

resized_img = resize_image(img_path, max_width=letter[0] - 72) # 72 is for the left and right margins

# Calculate the new height based on the resized width to preserve aspect ratio

new_height = int((resized_img.size[1] * (letter[0] - 72) / resized_img.size[0])

# Draw the resized image onto the canvas

c.drawImage(resized_img, 36, height - new_height, width=resized_img.size[0], height=new_height)

# Update the current height for the next image

height -= new_height + 12 # 12 is the gap between images

if height < 36: # Add a new page if needed

c.showPage()

height = letter[1] - 72

# Save the PDF and close the canvas

c.save()

print(f"PDF saved to: {output_pdf}")

def select_directory():

directory = filedialog.askdirectory()

entry_image_folder.delete(0, 'end')

entry_image_folder.insert(0, directory)

# 创建GUI界面

root = Tk()

root.title('Image to PDF Converter')

# 图片文件夹输入

label_image_folder = Label(root, text='Select Image Folder:')

label_image_folder.pack()

entry_image_folder = Entry(root, width=50)

entry_image_folder.pack()

# 选择文件夹按钮

button_select_folder = Button(root, text='Select Folder', command=select_directory)

button_select_folder.pack()

# 转换按钮

button_convert = Button(root, text='Convert to PDF',

command=lambda: convert_images_to_pdf(entry_image_folder.get(), 'output.pdf'))

button_convert.pack()

# 运行GUI主循环

mainloop()

我想让他继续优化pdf内容,但是接下来再调试,也无法调试成功。放弃吧~

结论:文心一言代码编写失败。

总结

按照编码运行结果排序:通义千问 > 文心一言 > kimi。

按照编码纠错效率排序:通义千问 > 文心一言 > kimi。

按照代码质量排序:通义千问> kimi / 文心一言 。

如果您对测评结果有疑问,欢迎交流,更多大模型案例在公众号首发。

#AIGC #kimi #通义千问 #文心一言 #大模型

精彩内容

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: