GRINMoE / app.py
JAMESPARK3's picture
Create app.py
da2134a verified
raw
history blame
1.8 kB
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']}")