Spaces:
Sleeping
Sleeping
Commit
Β·
ee6db9e
1
Parent(s):
69396f3
add
Browse files- app.py +75 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
|
5 |
+
# λͺ¨λΈ λ‘λ (DialoGPT-medium μμ)
|
6 |
+
@st.cache_resource
|
7 |
+
def load_model(model_name="microsoft/DialoGPT-medium"):
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
10 |
+
return tokenizer, model
|
11 |
+
|
12 |
+
# μ± μ€ν ν¨μ
|
13 |
+
def main():
|
14 |
+
st.title("ChatGPT μ μ¬ λν λ°λͺ¨")
|
15 |
+
st.write("μ¬κΈ°λ DialoGPT λͺ¨λΈμ νμ©ν κ°λ¨ν λν ν
μ€νΈμ© λ°λͺ¨μ
λλ€.")
|
16 |
+
|
17 |
+
# μΈμ
μ€ν
μ΄νΈ μ΄κΈ°ν
|
18 |
+
if "chat_history_ids" not in st.session_state:
|
19 |
+
st.session_state["chat_history_ids"] = None
|
20 |
+
if "past_user_inputs" not in st.session_state:
|
21 |
+
st.session_state["past_user_inputs"] = []
|
22 |
+
if "generated_responses" not in st.session_state:
|
23 |
+
st.session_state["generated_responses"] = []
|
24 |
+
|
25 |
+
tokenizer, model = load_model()
|
26 |
+
|
27 |
+
# κΈ°μ‘΄ λν λ΄μ νμ
|
28 |
+
if st.session_state["past_user_inputs"]:
|
29 |
+
for user_text, bot_text in zip(st.session_state["past_user_inputs"], st.session_state["generated_responses"]):
|
30 |
+
# μ¬μ©μ λ©μμ§
|
31 |
+
with st.chat_message("user"):
|
32 |
+
st.write(user_text)
|
33 |
+
# λ΄ λ©μμ§
|
34 |
+
with st.chat_message("assistant"):
|
35 |
+
st.write(bot_text)
|
36 |
+
|
37 |
+
# μ±ν
μ
λ ₯μ°½
|
38 |
+
user_input = st.chat_input("λ©μμ§λ₯Ό μ
λ ₯νμΈμ...")
|
39 |
+
|
40 |
+
if user_input:
|
41 |
+
# μ¬μ©μ λ©μμ§ νμ
|
42 |
+
with st.chat_message("user"):
|
43 |
+
st.write(user_input)
|
44 |
+
|
45 |
+
# μ μ
λ ₯μ ν ν°ν
|
46 |
+
new_user_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
47 |
+
|
48 |
+
if st.session_state["chat_history_ids"] is not None:
|
49 |
+
# κΈ°μ‘΄ νμ€ν 리μ μ΄μ΄ λΆμ΄κΈ°
|
50 |
+
bot_input_ids = torch.cat([st.session_state["chat_history_ids"], new_user_input_ids], dim=-1)
|
51 |
+
else:
|
52 |
+
bot_input_ids = new_user_input_ids
|
53 |
+
|
54 |
+
# λͺ¨λΈ μΆλ‘
|
55 |
+
with torch.no_grad():
|
56 |
+
chat_history_ids = model.generate(
|
57 |
+
bot_input_ids,
|
58 |
+
max_length=1000,
|
59 |
+
pad_token_id=tokenizer.eos_token_id
|
60 |
+
)
|
61 |
+
|
62 |
+
# κ²°κ³Ό λμ½λ©
|
63 |
+
bot_text = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
64 |
+
|
65 |
+
# μΈμ
μ€ν
μ΄νΈμ λν λ΄μ© μ
λ°μ΄νΈ
|
66 |
+
st.session_state["past_user_inputs"].append(user_input)
|
67 |
+
st.session_state["generated_responses"].append(bot_text)
|
68 |
+
st.session_state["chat_history_ids"] = chat_history_ids
|
69 |
+
|
70 |
+
# λ΄ λ©μμ§ νμ
|
71 |
+
with st.chat_message("assistant"):
|
72 |
+
st.write(bot_text)
|
73 |
+
|
74 |
+
if __name__ == "__main__":
|
75 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
torch
|
3 |
+
transformers
|