Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- README.md +6 -6
- app.py +74 -0
- requirements.txt +7 -0
README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: streamlit
|
7 |
-
sdk_version: 1.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
license:
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Chatbot
|
3 |
+
emoji: π
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: yellow
|
6 |
sdk: streamlit
|
7 |
+
sdk_version: 1.33.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
license: other
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from langchain.chat_models import ChatOpenAI
|
4 |
+
from langchain.schema import HumanMessage
|
5 |
+
from langchain.agents import AgentType, initialize_agent, load_tools
|
6 |
+
from langchain.callbacks import StreamlitCallbackHandler
|
7 |
+
from langchain.memory import ConversationBufferMemory
|
8 |
+
from langchain.prompts import MessagesPlaceholder
|
9 |
+
|
10 |
+
st.title("ζη« ηζζ―ζ΄Chatbot")
|
11 |
+
|
12 |
+
api_model = os.getenv("OPENAI_API_MODEL")
|
13 |
+
temperature = os.getenv("OPENAI_API_TEMPERATURE")
|
14 |
+
|
15 |
+
origin_text = st.sidebar.text_area("γγγ³γγε
₯ε")
|
16 |
+
system_prompt = origin_text if origin_text else os.getenv("system_prompt")
|
17 |
+
print(system_prompt)
|
18 |
+
|
19 |
+
def create_agent_chain():
|
20 |
+
chat = ChatOpenAI(
|
21 |
+
model_name = api_model,
|
22 |
+
temperature = temperature,
|
23 |
+
streaming = True,
|
24 |
+
)
|
25 |
+
|
26 |
+
agent_kwargs = {
|
27 |
+
"extra_prompt_messages": [MessagesPlaceholder(variable_name = "memory")],
|
28 |
+
}
|
29 |
+
|
30 |
+
memory = ConversationBufferMemory(memory_key = "memory", return_messages = True)
|
31 |
+
|
32 |
+
tools = load_tools(["ddg-search", "wikipedia"])
|
33 |
+
return initialize_agent(
|
34 |
+
tools,
|
35 |
+
chat,
|
36 |
+
agent = AgentType.OPENAI_FUNCTIONS,
|
37 |
+
agent_kwargs = agent_kwargs,
|
38 |
+
memory = memory,
|
39 |
+
)
|
40 |
+
|
41 |
+
if "messages" not in st.session_state:
|
42 |
+
st.session_state.messages = []
|
43 |
+
|
44 |
+
if "agent_chain" not in st.session_state:
|
45 |
+
st.session_state.agent_chain = create_agent_chain()
|
46 |
+
|
47 |
+
if "polly_client" not in st.session_state:
|
48 |
+
st.session_state.polly_client = PollyClient()
|
49 |
+
|
50 |
+
for message in st.session_state.messages:
|
51 |
+
with st.chat_message(message["role"]):
|
52 |
+
st.markdown(message["content"])
|
53 |
+
|
54 |
+
prompt = st.chat_input("What is up?")
|
55 |
+
|
56 |
+
if prompt:
|
57 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
58 |
+
|
59 |
+
with st.chat_message("user"):
|
60 |
+
st.markdown(prompt)
|
61 |
+
|
62 |
+
with st.chat_message("assistant"):
|
63 |
+
callback = StreamlitCallbackHandler(st.container())
|
64 |
+
response = st.session_state.agent_chain.run(system_prompt + prompt, callbacks = [callback])
|
65 |
+
st.markdown(response)
|
66 |
+
|
67 |
+
# ι³ε£°γγ‘γ€γ«γηζ
|
68 |
+
file_name = "response.mp3"
|
69 |
+
st.session_state.polly_client.text_to_speech(response, file_name)
|
70 |
+
|
71 |
+
audio_file_path = file_name
|
72 |
+
st.audio(audio_file_path)
|
73 |
+
|
74 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
boto3
|
2 |
+
streamlit
|
3 |
+
python-dotenv
|
4 |
+
langchain
|
5 |
+
openai
|
6 |
+
wikipedia==1.4.0
|
7 |
+
duckduckgo-search==4.1.0
|