欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python+ChatGPT制作一個(gè)AI實(shí)用百寶箱

 更新時(shí)間:2023年08月09日 15:59:25   作者:俊欣  
ChatGPT最近在互聯(lián)網(wǎng)掀起了一陣熱潮,其高度智能化的功能能夠給我們現(xiàn)實(shí)生活帶來(lái)諸多的便利。本文就來(lái)用Python和ChatGPT制作一個(gè)AI實(shí)用百寶箱吧

ChatGPT最近在互聯(lián)網(wǎng)掀起了一陣熱潮,其高度智能化的功能能夠給我們現(xiàn)實(shí)生活帶來(lái)諸多的便利,可以幫助你寫(xiě)文章、寫(xiě)報(bào)告、寫(xiě)周報(bào)、做表格、做策劃甚至還會(huì)寫(xiě)代碼。只要與文字相關(guān)的工作,它幾乎都能給出一份滿(mǎn)意的答卷。

小編趁著有空上去玩了一下,也發(fā)現(xiàn)了其中的強(qiáng)大

那么本篇文章小編就通過(guò)streamlit框架來(lái)搭建一個(gè)AI百寶箱的網(wǎng)頁(yè),其中里面集成了一系列功能包括智能聊天機(jī)器兒、智能繪畫(huà)師,大家有興趣還可以另外添加例如配音等功能,核心邏輯的話(huà)就是調(diào)用第三方的接口,然后做一層封裝和優(yōu)化。

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

那么這里我們需要用到這幾個(gè)庫(kù),用pip命令來(lái)下載

# 安裝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)頁(yè)的左側(cè)有一個(gè)工具欄,其中羅列了一系列的功能,我們這里簡(jiǎn)單的囊括了幾個(gè),包括了“簡(jiǎn)介”、“AI聊天”、“AI繪畫(huà)”,大家感興趣的后期可以繼續(xù)往里面添加,例如“AI配音”,代碼如下

with st.sidebar:
    choose = option_menu("工具欄", ["簡(jiǎn)介","AI聊天", "AI繪畫(huà)"],
                         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"},
                         }
                         )

那么在“簡(jiǎn)介”這一欄當(dāng)中,顧名思義就是對(duì)該網(wǎng)頁(yè)簡(jiǎn)單的介紹,我們簡(jiǎn)單的寫(xiě)一些介紹,代碼如下

if choose == "簡(jiǎn)介":
    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百寶箱,里面集成了各種工具,歡迎使用**')

展示出來(lái)的效果如下

AI聊天機(jī)器人

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

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

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當(dāng)中的輸入框,代碼如下

elif choose == "AI聊天":
    st.title("AI聊天機(jī)器人")
    # 設(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("在這里輸入問(wèn)題,回車(chē)查詢(xún)", "Python是什么?")
    if user_query != ":q" or user_query != "":
        # 將問(wèn)題提交給ChatGPT, 返回結(jié)果
        response = ChatGPT(user_query)
        st.write(f"{response}")

AI繪畫(huà)機(jī)器人

而在“AI繪畫(huà)”的模塊中,代碼邏輯也是相類(lèi)似的,這邊需要調(diào)用與繪畫(huà)相關(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")

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

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')

最后就可以在終端運(yùn)行下面的代碼了,

streamlit?run?example.py

我們?cè)跒g覽器中打開(kāi)頁(yè)面,例如我們點(diǎn)擊進(jìn)入“AI聊天”這個(gè)模塊,我們可以看到右上角處于RUNNING的狀態(tài),表示正在運(yùn)行中,等會(huì)兒之后就能看到結(jié)果

而點(diǎn)擊進(jìn)入“AI繪畫(huà)”這個(gè)模塊,例如想要繪制可愛(ài)的貓咪,我們也能看到如下的結(jié)果

到此這篇關(guān)于Python+ChatGPT制作一個(gè)AI實(shí)用百寶箱的文章就介紹到這了,更多相關(guān)Python ChatGPT制作AI百寶箱內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論