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