Spaces:
Running
Running
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']}") | |