Spaces:
No application file
No application file
First
Browse files- main.py +106 -0
- requirements.txt +3 -0
main.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain.chains import ConversationChain
|
| 3 |
+
from langchain.chains.conversation.memory import ConversationEntityMemory
|
| 4 |
+
from langchain.chains.conversation.prompt import \
|
| 5 |
+
ENTITY_MEMORY_CONVERSATION_TEMPLATE
|
| 6 |
+
from langchain.chat_models import ChatOpenAI
|
| 7 |
+
|
| 8 |
+
st.set_page_config("RoastingPoet", layout="wide")
|
| 9 |
+
|
| 10 |
+
# 페이지 새로 로딩될때마다 바뀌는 변수들은 st.session_state["변수명"] 이런식으로 저장해줘야함
|
| 11 |
+
if "generated" not in st.session_state:
|
| 12 |
+
st.session_state["generated"] = []
|
| 13 |
+
if "past" not in st.session_state:
|
| 14 |
+
st.session_state["past"] = []
|
| 15 |
+
if "input" not in st.session_state:
|
| 16 |
+
st.session_state["input"] = ""
|
| 17 |
+
if "stored_session" not in st.session_state:
|
| 18 |
+
st.session_state["stored_session"] = []
|
| 19 |
+
|
| 20 |
+
# 유저인풋 받는 함수
|
| 21 |
+
def get_text():
|
| 22 |
+
"""
|
| 23 |
+
Get the user input text.
|
| 24 |
+
Returns:
|
| 25 |
+
(str): The text entered by the user
|
| 26 |
+
"""
|
| 27 |
+
input_text = st.text_input(
|
| 28 |
+
"You: ",
|
| 29 |
+
st.session_state["input"],
|
| 30 |
+
key="input",
|
| 31 |
+
placeholder="Type in and press enter",
|
| 32 |
+
label_visibility="hidden",
|
| 33 |
+
)
|
| 34 |
+
return input_text
|
| 35 |
+
|
| 36 |
+
# 초기화(나도 이해 못함 아직)
|
| 37 |
+
def new_chat():
|
| 38 |
+
"""
|
| 39 |
+
Clears session state and starts a new chat.
|
| 40 |
+
"""
|
| 41 |
+
save = []
|
| 42 |
+
for i in range(len(st.session_state["generated"]) - 1, -1, -1):
|
| 43 |
+
save.append("User:" + st.session_state["past"][i])
|
| 44 |
+
save.append("Bot:" + st.session_state["generated"][i])
|
| 45 |
+
st.session_state["stored_session"].append(save)
|
| 46 |
+
st.session_state["generated"] = []
|
| 47 |
+
st.session_state["past"] = []
|
| 48 |
+
st.session_state["input"] = ""
|
| 49 |
+
st.session_state.entity_memory.store = {}
|
| 50 |
+
st.session_state.entity_memory.buffer.clear()
|
| 51 |
+
# entity memory라는건 memory 중에서도 {'Sarah': '주인공, 여자, ...', 'Mr.Thompson': '집사, 수상한 움직임'}
|
| 52 |
+
# 이런 식으로 기억할 내용을 저장하는 방식. langchain의 기능임
|
| 53 |
+
|
| 54 |
+
# Set up the Streamlit app layout
|
| 55 |
+
st.title("💡 A Wise Robot Says...")
|
| 56 |
+
|
| 57 |
+
st.markdown(
|
| 58 |
+
"""
|
| 59 |
+
```
|
| 60 |
+
Share your worries, your questions so dear,
|
| 61 |
+
With a wise robot, who's always near.
|
| 62 |
+
Type in your troubles, let them take flight,
|
| 63 |
+
Press enter and watch, as wisdom ignites.
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
"""
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# API 키 입력받기
|
| 70 |
+
API_O = "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"
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# Large Language Model은 OpenAI 쓸거임
|
| 74 |
+
llm = ChatOpenAI(temperature=0.8, openai_api_key=API_O, model_name="gpt-3.5-turbo", verbose=False)
|
| 75 |
+
|
| 76 |
+
# Create a ConversationEntityMemory object if not already created
|
| 77 |
+
if "entity_memory" not in st.session_state:
|
| 78 |
+
st.session_state.entity_memory = ConversationEntityMemory(llm=llm, k=1)
|
| 79 |
+
|
| 80 |
+
# Create the ConversationChain object with the specified configuration
|
| 81 |
+
Conversation = ConversationChain(
|
| 82 |
+
llm=llm,
|
| 83 |
+
prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE,
|
| 84 |
+
memory=st.session_state.entity_memory,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
user_input = get_text()
|
| 89 |
+
if user_input:
|
| 90 |
+
output = Conversation.run(input=user_input+" (INSTRUCTION: You are RoastingPoet. People ask you for advice, but they get roasted instead. Instead of just replying, I want you to brutally roast them in a poem. I mean BRUTALLY. And also put '\n' in front of capital letters.)")
|
| 91 |
+
st.session_state.past.append(user_input)
|
| 92 |
+
st.session_state.generated.append(output)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
with st.expander("Conversation", expanded=True):
|
| 96 |
+
for i in range(len(st.session_state["generated"]) - 1, -1, -1):
|
| 97 |
+
st.success(st.session_state["generated"][i], icon="✒️")
|
| 98 |
+
st.info(st.session_state["past"][i], icon="😭")
|
| 99 |
+
|
| 100 |
+
st.markdown(
|
| 101 |
+
"""
|
| 102 |
+
> :black[*powered by - [LangChain]('https://langchain.readthedocs.io/en/latest/modules/memory.html#memory') +
|
| 103 |
+
[OpenAI]('https://platform.openai.com/docs/models/gpt-3-5') +
|
| 104 |
+
[Streamlit]('https://streamlit.io') + [DataButton](https://www.databutton.io/)*]
|
| 105 |
+
"""
|
| 106 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
langchain
|
| 3 |
+
OpenAI
|