光腚拉磨,轉圈丟人 -“華為演示程序其實隻要一行代碼。 - 監考老師 -”

來源: 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”