File size: 1,801 Bytes
da2134a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
from huggingface_hub import InferenceClient
import streamlit as st

# ν™˜κ²½ λ³€μˆ˜μ—μ„œ Hugging Face 토큰 κ°€μ Έμ˜€κΈ°
HF_TOKEN = os.getenv("HF_TOKEN")

# Inference Client μ„€μ •
client = InferenceClient(
    "https://<your-endpoint-url>",  # Inference Endpoint URL
    token=HF_TOKEN  # Hugging Face 토큰을 ν™˜κ²½ λ³€μˆ˜μ—μ„œ 뢈러옴
)

# Streamlit μ•± νŽ˜μ΄μ§€ μ„€μ •
st.set_page_config(page_title="GRIN-MoE Chat", page_icon="πŸ€–")
st.title("GRIN-MoE와 λŒ€ν™”ν•΄λ³΄μ„Έμš”!")

# μ±„νŒ… νžˆμŠ€ν† λ¦¬ μœ μ§€
if 'messages' not in st.session_state:
    st.session_state.messages = []

# μ‚¬μš©μž μž…λ ₯
user_input = st.text_input("μž…λ ₯ λ©”μ‹œμ§€λ₯Ό μž‘μ„±ν•˜μ„Έμš”:")

# Stream을 μ²˜λ¦¬ν•˜λŠ” ν•¨μˆ˜
def generate_streaming_response(prompt):
    response_text = ""
    for message in client.chat_completion(
        messages=[{"role": "user", "content": prompt}],
        max_tokens=500,
        stream=True
    ):
        delta = message.choices[0].delta.content
        response_text += delta
        yield delta

# μž…λ ₯ λ©”μ‹œμ§€κ°€ μžˆμ„ λ•Œλ§Œ λŒ€ν™” 처리
if user_input:
    st.session_state.messages.append({"role": "user", "content": user_input})

    # Streamlitμ—μ„œ μ±„νŒ…μ„ 좜λ ₯ν•˜λŠ” μ˜μ—­
    with st.spinner('GRIN-MoEκ°€ μ‘λ‹΅ν•˜λŠ” 쀑...'):
        response_text = ""
        for delta in generate_streaming_response(user_input):
            response_text += delta
            st.write(response_text)
        st.session_state.messages.append({"role": "assistant", "content": response_text})

# 이전 λ©”μ‹œμ§€ ν‘œμ‹œ
if st.session_state.messages:
    for msg in st.session_state.messages:
        if msg["role"] == "user":
            st.write(f"**μ‚¬μš©μž:** {msg['content']}")
        else:
            st.write(f"**GRIN-MoE:** {msg['content']}")