光腚拉磨,转圈丢人 -“华为演示程序其实只要一行代码。 - 监考老师 -”

来源: ncaa 2024-05-18 09:55:51 [] [旧帖] [给我悄悄话] 本文已被阅读: 次 (3483 bytes)

老师解释一行code就够用
while True: query(input()).write('output.jpeg')

好吧,小爷爷把您这句蹩脚code改进一下,看看华为如何做到的!!

# 设置Bing Image Search API的相关信息
subscription_key = "your_bing_api_key"
search_url = "https://api.bing.microsoft.com/v7.0/images/search"
headers = {"Ocp-Apim-Subscription-Key": subscription_key}

def query(user_input):
    # 使用Bing Image Search API搜索图像找到与用户描述最相关的图像
    params = {"q": user_input, "license": "public", "imageType": "photo"}
    response = requests.get(search_url, headers=headers, params=params)
    search_results = response.json()
    
    if "value" in search_results and len(search_results["value"]) > 0:
        # 获取第一个图像的URL
        image_url = search_results["value"][0]["contentUrl"]
        image_response = requests.get(image_url)
        if image_response.status_code == 200:
            image_data = image_response.content
        else:
            raise ValueError("Failed to retrieve image from the URL")
    else:
        raise ValueError("No images found for the query")

    class QueryResult:
        def __init__(self, data):
            self.data = data
        
        def write(self, filename):
            with open(filename, 'wb') as f:
                f.write(self.data)
    
    return QueryResult(image_data)

while True:
    user_input = input("请输入图像描述 (or type 'exit' to quit): ")
    if user_input.lower() == 'exit':
        break
    try:
        result = query(user_input)

        print("上面query运行太快,显得很不真实,俺只好假装思考6秒钟")
        time.sleep(6)

        result.write('output.jpeg')
        print(”嗯,这样结束就蛮真实了“)

    except Exception as e:
        print(f"An error occurred: {e}")

所有跟帖: 

想证明自己是胡搅蛮缠的小丑的话不用再努力了,这已是公论。 -监考老师- 给 监考老师 发送悄悄话 监考老师 的博客首页 (0 bytes) () 05/18/2024 postreply 10:01:03

像这样Ctrl-C 中断sleep时,根本不在write 内部。结论:不及格 -监考老师- 给 监考老师 发送悄悄话 监考老师 的博客首页 (0 bytes) () 05/18/2024 postreply 10:08:25

其他低手常见病就不一一列举了。 -监考老师- 给 监考老师 发送悄悄话 监考老师 的博客首页 (0 bytes) () 05/18/2024 postreply 10:27:30

高手马老师您好,俺是徐晓东!哈哈 -ncaa- 给 ncaa 发送悄悄话 (0 bytes) () 05/18/2024 postreply 11:10:11

这备战裹脚布,开战六秒喷的牛叉,装别人也装不来呀? -监考老师- 给 监考老师 发送悄悄话 监考老师 的博客首页 (0 bytes) () 05/18/2024 postreply 12:17:09

请您先登陆,再发跟帖!

发现Adblock插件

如要继续浏览
请支持本站 请务必在本站关闭/移除任何Adblock

关闭Adblock后 请点击

请参考如何关闭Adblock/Adblock plus

安装Adblock plus用户请点击浏览器图标
选择“Disable on www.wenxuecity.com”

安装Adblock用户请点击图标
选择“don't run on pages on this domain”