딥러닝

AI 라이브러리 활용 특강 streamlit openai

끈끈 2023. 5. 18. 11:08

https://docs.streamlit.io/library/api-reference

 

Streamlit Docs

Join the community Streamlit is more than just a way to make data apps, it's also a community of creators that share their apps and ideas and help each other make their work better. Please come join us on the community forum. We love to hear your questions

docs.streamlit.io

 


 

pip install streamlit openai

 

WARNING: Failed to write executable - trying to use .deleteme logic
ERROR: Could not install packages due to an OSError: [WinError 2] 지정된 파일을 찾을 수 없습니다:
'C:\\Python311\\Scripts\\watchmedo.exe' ->
'C:\\Python311\\Scripts\\watchmedo.exe.deleteme'

>>> 가상환경 재진입

 

 

 

 

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensorflow-intel 2.12.0 requires numpy<1.24,>=1.22, but you have numpy 1.24.3 which is incompatible.

>>> tensorflow-intel 패키지가 numpy 버전 1.24보다 작고 1.22 이상을 요구하지만,
현재 설치된 numpy 버전은 1.24.3이라서 호환되지 않는다는 것

>>> pip install --upgrade tensorflow-intel
tensorflow-intel 패키지 업그레이드

>>> pip install numpy==1.23
numpy 패키지를 특정 버전으로 다운그레이드

 


 

import streamlit as st

st.title("ChatGPT Plus DALL-E")

with st.form("form"):
    user_input = st.text_input("Prompt")
    submit = st.form_submit_button("Submit")

if submit:
    st.write(user_input)

 

streamlit run app.py
> Email 입력

 

 

openai

 

import streamlit as st
import openai

openai.api_key = "" # key

st.title("ChatGPT Plus DALL-E")

with st.form("form"):
    user_input = st.text_input("Prompt")
    submit = st.form_submit_button("Submit")

if submit:
    st.write(user_input)

    gpt_prompt = []

    # 시스템 메세지
    gpt_prompt.append({
        "role": "system",
        "content": "Imagine the detail appearance of the input. Response shortly."
    })

    # 사용자의 입력
    gpt_prompt.append({
        "role": "user",
        "content": user_input
    })

    prompt = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=gpt_prompt)
    
    st.markdown(prompt)

 

 

json 형태의 output이 나오는데 "content" 부분만 가져오기

 

st.markdown(prompt["choices"][0]["message"]["content"])

 

 

prompt = st.markdown(prompt["choices"][0]["message"]["content"])
st.caption(prompt)

 

 

DALL-E

 

import streamlit as st
import openai

openai.api_key = "" # key

st.title("ChatGPT Plus DALL-E")

with st.form("form"):
    user_input = st.text_input("Prompt")
    submit = st.form_submit_button("Submit")

if submit:
    st.write(user_input)

    # ChatGPT
    gpt_prompt = []
	
    # 시스템 메세지
    gpt_prompt.append({
        "role": "system",
        "content": "Imagine the detail appearance of the input. Response shortly."
    })

	# 사용자의 입력
    gpt_prompt.append({
        "role": "user",
        "content": user_input
    })

    prompt = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=gpt_prompt)

    prompt = prompt["choices"][0]["message"]["content"]
    st.caption(prompt)

    # DALL-E
    result = openai.Image.create(
        prompt=prompt,
        size="1024x1024"
    )

    st.image(result["data"][0]["url"])

 

 

????????????? 영어로 입력하기

 

with st.spinner("Wating for ChatGPT..."):
    prompt = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=gpt_prompt)

 

 

# DALL-E
with st.spinner("Wating for DALL-E..."):
    result = openai.Image.create(
        prompt=prompt,
        size="1024x1024"
)

 

 

 


 

완성 코드

 

import streamlit as st
import openai

openai.api_key = "" # key

st.title("ChatGPT Plus DALL-E")

with st.form("form"):
    user_input = st.text_input("Prompt")
    submit = st.form_submit_button("Submit")

if submit:
    st.write(user_input)

    # ChatGPT
    gpt_prompt = []

    # 시스템 메세지
    gpt_prompt.append({
        "role": "system",
        "content": "Imagine the detail appearance of the input. Response shortly."
    })

    # 사용자의 입력
    gpt_prompt.append({
        "role": "user",
        "content": user_input
    })

    with st.spinner("Wating for ChatGPT..."):
        prompt = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=gpt_prompt)

    prompt = prompt["choices"][0]["message"]["content"]
    st.caption(prompt)

    # DALL-E
    with st.spinner("Wating for DALL-E..."):
        result = openai.Image.create(
            prompt=prompt,
            size="1024x1024"
    )

    st.image(result["data"][0]["url"])