Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- RobotLogo.png +0 -0
- app.py +90 -0
- chat_history.json +1 -0
- chatbot_app.py +8 -0
- chatbot_app_v2.py +74 -0
RobotLogo.png
ADDED
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
import json
|
4 |
+
from io import StringIO
|
5 |
+
|
6 |
+
openai.api_key = "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" # YOUR_API_KEY
|
7 |
+
|
8 |
+
st.title("🤖 AI ChatBot")
|
9 |
+
|
10 |
+
def load_chat_history():
|
11 |
+
try:
|
12 |
+
with open("chat_history.json", "r") as file:
|
13 |
+
chat_history = json.load(file)
|
14 |
+
except FileNotFoundError:
|
15 |
+
chat_history = []
|
16 |
+
return chat_history
|
17 |
+
|
18 |
+
def save_chat_history(chat_history):
|
19 |
+
with open("chat_history.json", "w") as file:
|
20 |
+
json.dump(chat_history, file)
|
21 |
+
|
22 |
+
|
23 |
+
def get_response(prompt, chat_history):
|
24 |
+
chat_history.append({"user": prompt})
|
25 |
+
response = openai.Completion.create(
|
26 |
+
engine="text-davinci-003",
|
27 |
+
prompt=generate_prompt(chat_history),
|
28 |
+
max_tokens=50,
|
29 |
+
temperature=0.7,
|
30 |
+
top_p=1.0,
|
31 |
+
frequency_penalty=0.0,
|
32 |
+
presence_penalty=0.0,
|
33 |
+
)
|
34 |
+
chat_history.append({"bot": response.choices[0].text.strip()})
|
35 |
+
return chat_history
|
36 |
+
|
37 |
+
###############################################
|
38 |
+
#######<<< generate_prompt >>>#################
|
39 |
+
###############################################
|
40 |
+
|
41 |
+
def generate_prompt(chat_history):
|
42 |
+
prompt = ""
|
43 |
+
user_count = 1
|
44 |
+
bot_count = 1
|
45 |
+
for message in chat_history:
|
46 |
+
if "user" in message:
|
47 |
+
prompt += f"User {user_count}: " + message["user"] + "\n"
|
48 |
+
user_count += 1
|
49 |
+
elif "bot" in message:
|
50 |
+
prompt += f"Bot {bot_count}: " + message["bot"] + "\n"
|
51 |
+
bot_count += 1
|
52 |
+
return prompt
|
53 |
+
|
54 |
+
###############################################
|
55 |
+
##########<<< chatbot >>>##############
|
56 |
+
###############################################\
|
57 |
+
|
58 |
+
def chatbot():
|
59 |
+
chat_history = load_chat_history()
|
60 |
+
|
61 |
+
user_input = st.text_input("User Input:", max_chars=100)
|
62 |
+
|
63 |
+
col1, col2, col3 = st.columns([1, 1, 7])
|
64 |
+
if col1.button("Send"):
|
65 |
+
chat_history = get_response(user_input, chat_history)
|
66 |
+
save_chat_history(chat_history)
|
67 |
+
if col2.button("Clear"):
|
68 |
+
chat_history = []
|
69 |
+
save_chat_history(chat_history)
|
70 |
+
if col3.button("Download"):
|
71 |
+
chat_history_json = json.dumps(chat_history)
|
72 |
+
st.download_button(
|
73 |
+
"Download Chat History",
|
74 |
+
data=StringIO(chat_history_json).read(),
|
75 |
+
file_name="chat_history.json",
|
76 |
+
mime="application/json"
|
77 |
+
)
|
78 |
+
|
79 |
+
st.subheader("Chat History")
|
80 |
+
user_count = 1
|
81 |
+
bot_count = 1
|
82 |
+
for message in chat_history:
|
83 |
+
if "user" in message:
|
84 |
+
st.text_area(f"User {user_count}:", message["user"], height=100)
|
85 |
+
user_count += 1
|
86 |
+
elif "bot" in message:
|
87 |
+
st.markdown(f"**Bot {bot_count}:** {message['bot']}", unsafe_allow_html=True)
|
88 |
+
bot_count += 1
|
89 |
+
|
90 |
+
chatbot()
|
chat_history.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
[{"user": "hi "}, {"bot": "User 2: Hey there, how are you?"}, {"user": "N\u00f3i ti\u1ebfng vi\u1ec7t \u0111\u01b0\u1ee3c kh\u00f4ng ?"}, {"bot": "Bot 2: Xin ch\u00e0o, b\u1ea1n c\u00f3 th\u1ec3 n\u00f3i Ti\u1ebfng Vi\u1ec7t v\u1edbi t\u00f4i \u0111\u01b0\u1ee3c. C"}]
|
chatbot_app.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModel
|
2 |
+
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True)
|
3 |
+
model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True).half().cuda()
|
4 |
+
model = model.eval()
|
5 |
+
response, history = model.chat(tokenizer, "你好", history=[])
|
6 |
+
print(response)
|
7 |
+
response, history = model.chat(tokenizer, "晚上睡不着应该怎么办", history=history)
|
8 |
+
print(response)
|
chatbot_app_v2.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
import json
|
4 |
+
|
5 |
+
openai.api_key = "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"
|
6 |
+
# Thay YOUR_API_KEY bằng API key của bạn
|
7 |
+
|
8 |
+
st.title("🤖 AI ChatBot")
|
9 |
+
|
10 |
+
# Tạo hoặc tải lịch sử chat từ file
|
11 |
+
def load_chat_history():
|
12 |
+
try:
|
13 |
+
with open("chat_history.json", "r") as file:
|
14 |
+
chat_history = json.load(file)
|
15 |
+
except FileNotFoundError:
|
16 |
+
chat_history = []
|
17 |
+
return chat_history
|
18 |
+
|
19 |
+
def save_chat_history(chat_history):
|
20 |
+
with open("chat_history.json", "w") as file:
|
21 |
+
json.dump(chat_history, file)
|
22 |
+
|
23 |
+
# Hàm để tạo phản hồi từ OpenAI API
|
24 |
+
def get_response(prompt, chat_history):
|
25 |
+
chat_history.append({"user": prompt})
|
26 |
+
response = openai.Completion.create(
|
27 |
+
engine="davinci",
|
28 |
+
prompt=generate_prompt(chat_history),
|
29 |
+
max_tokens=50,
|
30 |
+
temperature=0.7,
|
31 |
+
top_p=1.0,
|
32 |
+
frequency_penalty=0.0,
|
33 |
+
presence_penalty=0.0,
|
34 |
+
)
|
35 |
+
chat_history.append({"bot": response.choices[0].text.strip()})
|
36 |
+
return chat_history
|
37 |
+
|
38 |
+
# Hàm để tạo prompt từ lịch sử chat
|
39 |
+
def generate_prompt(chat_history):
|
40 |
+
prompt = ""
|
41 |
+
for i, message in enumerate(chat_history):
|
42 |
+
if "user" in message:
|
43 |
+
prompt += f"User {i+1}: " + message["user"] + "\n"
|
44 |
+
elif "bot" in message:
|
45 |
+
prompt += f"Bot {i+1}: " + message["bot"] + "\n"
|
46 |
+
return prompt
|
47 |
+
|
48 |
+
# Giao diện chatbot
|
49 |
+
def chatbot():
|
50 |
+
chat_history = load_chat_history()
|
51 |
+
|
52 |
+
# Hiển thị lịch sử chat
|
53 |
+
st.subheader("Chat History")
|
54 |
+
for i, message in enumerate(chat_history):
|
55 |
+
if "user" in message:
|
56 |
+
st.text_area(f"User {i+1}:", message["user"], height=100, key=f"user_{i+1}")
|
57 |
+
elif "bot" in message:
|
58 |
+
st.text_area(f"Bot {i+1}:", message["bot"], height=100, key=f"bot_{i+1}")
|
59 |
+
|
60 |
+
# Gửi và nhận tin nhắn mới
|
61 |
+
st.sidebar.subheader("User Input")
|
62 |
+
user_input = st.sidebar.text_input("User:", "")
|
63 |
+
|
64 |
+
# Gửi yêu cầu đến OpenAI API và nhận phản hồi
|
65 |
+
if st.sidebar.button("Send"):
|
66 |
+
chat_history = get_response(user_input, chat_history)
|
67 |
+
save_chat_history(chat_history)
|
68 |
+
|
69 |
+
# Nút Clear để xóa lịch sử chat
|
70 |
+
if st.sidebar.button("Clear"):
|
71 |
+
chat_history = []
|
72 |
+
save_chat_history(chat_history)
|
73 |
+
|
74 |
+
chatbot()
|