Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
# 환경 변수에서 Hugging Face 토큰 가져오기
|
6 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
7 |
+
|
8 |
+
# Inference Client 설정
|
9 |
+
client = InferenceClient(
|
10 |
+
"https://<your-endpoint-url>", # Inference Endpoint URL
|
11 |
+
token=HF_TOKEN # Hugging Face 토큰을 환경 변수에서 불러옴
|
12 |
+
)
|
13 |
+
|
14 |
+
# Streamlit 앱 페이지 설정
|
15 |
+
st.set_page_config(page_title="GRIN-MoE Chat", page_icon="🤖")
|
16 |
+
st.title("GRIN-MoE와 대화해보세요!")
|
17 |
+
|
18 |
+
# 채팅 히스토리 유지
|
19 |
+
if 'messages' not in st.session_state:
|
20 |
+
st.session_state.messages = []
|
21 |
+
|
22 |
+
# 사용자 입력
|
23 |
+
user_input = st.text_input("입력 메시지를 작성하세요:")
|
24 |
+
|
25 |
+
# Stream을 처리하는 함수
|
26 |
+
def generate_streaming_response(prompt):
|
27 |
+
response_text = ""
|
28 |
+
for message in client.chat_completion(
|
29 |
+
messages=[{"role": "user", "content": prompt}],
|
30 |
+
max_tokens=500,
|
31 |
+
stream=True
|
32 |
+
):
|
33 |
+
delta = message.choices[0].delta.content
|
34 |
+
response_text += delta
|
35 |
+
yield delta
|
36 |
+
|
37 |
+
# 입력 메시지가 있을 때만 대화 처리
|
38 |
+
if user_input:
|
39 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
40 |
+
|
41 |
+
# Streamlit에서 채팅을 출력하는 영역
|
42 |
+
with st.spinner('GRIN-MoE가 응답하는 중...'):
|
43 |
+
response_text = ""
|
44 |
+
for delta in generate_streaming_response(user_input):
|
45 |
+
response_text += delta
|
46 |
+
st.write(response_text)
|
47 |
+
st.session_state.messages.append({"role": "assistant", "content": response_text})
|
48 |
+
|
49 |
+
# 이전 메시지 표시
|
50 |
+
if st.session_state.messages:
|
51 |
+
for msg in st.session_state.messages:
|
52 |
+
if msg["role"] == "user":
|
53 |
+
st.write(f"**사용자:** {msg['content']}")
|
54 |
+
else:
|
55 |
+
st.write(f"**GRIN-MoE:** {msg['content']}")
|