溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么使用Python和ChatGPT制作一個AI實用工具

發(fā)布時間:2023-02-23 14:06:46 來源:億速云 閱讀:103 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“怎么使用Python和ChatGPT制作一個AI實用工具”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

注冊O(shè)penAI

首先需要注冊OpenAI,這樣就可以使用ChatGPT

怎么使用Python和ChatGPT制作一個AI實用工具

搭建網(wǎng)站及其框架

那么這里我們需要用到這幾個庫,用pip命令來下載

# 安裝streamlit和openai
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple streamlit
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple streamlit_option_menu
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple openai

那么首先網(wǎng)頁的左側(cè)有一個工具欄,其中羅列了一系列的功能,我們這里簡單的囊括了幾個,包括了“簡介”、“AI聊天”、“AI繪畫”,大家感興趣的后期可以繼續(xù)往里面添加,例如“AI配音”,代碼如下

with st.sidebar:
    choose = option_menu("工具欄", ["簡介","AI聊天", "AI繪畫"],
                         icons=['house', 'person lines fill', 'app-indicator'],
                         menu_icon="list", default_index=0,
                         styles={
                             "container": {"padding": "5!important", "background-color": "#fafafa"},
                             "icon": {"color": "orange", "font-size": "25px"},
                             "nav-link": {"font-size": "16px", "text-align": "left", "margin": "0px",
                                          "--hover-color": "#eee"},
                             "nav-link-selected": {"background-color": "#24A608"},
                         }
                         )

那么在“簡介”這一欄當中,顧名思義就是對該網(wǎng)頁簡單的介紹,我們簡單的寫一些介紹,代碼如下

if choose == "簡介":
    col1, col2 = st.columns([0.8, 0.2])
    with col1:  # To display the header text using css style
        st.markdown(""" <style> .font {
            font-size:35px ; font-family: 'Cooper Black'; color: #FF9633;} 
            </style> """, unsafe_allow_html=True)
        st.markdown('<p class="font">About the Creator</p>', unsafe_allow_html=True)
    with col2:  # To display brand log
        logo = Image.open("wechat_logo.jpg")
        st.image(logo, width=130)

    st.markdown('**AI百寶箱,里面集成了各種工具,歡迎使用**')

展示出來的效果如下

怎么使用Python和ChatGPT制作一個AI實用工具

AI聊天機器人

那么首先我們需要在個人設(shè)置里面去獲取一個秘鑰,

然后選擇一個模型,這里我們選擇text-davinci-003模型,相比其他而言,性能更好,然后我們調(diào)用OpenAI里面的方法來生成回答

def ChatGPT(user_query):
    completion = openai.Completion.create(
        engine=model_engine,
        prompt=user_query,
        max_tokens=1024,
        n=1,
        temperature=0.5,
    )
    response = completion.choices[0].text
    return response

然后我們調(diào)用該函數(shù)結(jié)合streamlit當中的輸入框,代碼如下

elif choose == "AI聊天":
    st.title("AI聊天機器人")
    # 設(shè)置密匙
    model_engine = "text-davinci-003"

    def ChatGPT(user_query):
        completion = openai.Completion.create(
            engine=model_engine,
            prompt=user_query,
            max_tokens=1024,
            n=1,
            temperature=0.5,
        )
        response = completion.choices[0].text
        return response

    user_query = st.text_input("在這里輸入問題,回車查詢", "Python是什么?")
    if user_query != ":q" or user_query != "":
        # 將問題提交給ChatGPT, 返回結(jié)果
        response = ChatGPT(user_query)
        st.write(f"{response}")

AI繪畫機器人

而在“AI繪畫”的模塊中,代碼邏輯也是相類似的,這邊需要調(diào)用與繪畫相關(guān)的API,代碼如下

def image_generate(user_demand):
    completion = openai.Image.create(
        prompt=user_demand,
        n=2,
        size="1024x1024"
    )
    response = completion.get("data")
    return response[0].get("url")

由于返回給我們的是一個URL,因此還需要保存到本地,然后再通過Image模塊打開,代碼如下

image_url = image_generate(user_query)
response = requests.get(image_url, stream=True)
try:
    with open("./image/01.png", 'wb') as f:
        for chunk in response:
            f.write(chunk)
        f.close()
        print("Download done!!")
except Exception as e:
    print(e)

img1 = Image.open(r'./image/01.png')
st.image(img1, width=500, caption='Image by OpenAI')

最后就可以在終端運行下面的代碼了,

streamlit run example.py

我們在瀏覽器中打開頁面,例如我們點擊進入“AI聊天”這個模塊,我們可以看到右上角處于RUNNING的狀態(tài),表示正在運行中,等會兒之后就能看到結(jié)果

怎么使用Python和ChatGPT制作一個AI實用工具

而點擊進入“AI繪畫”這個模塊,例如想要繪制可愛的貓咪,我們也能看到如下的結(jié)果

怎么使用Python和ChatGPT制作一個AI實用工具

“怎么使用Python和ChatGPT制作一個AI實用工具”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI