bai_test_02 / app.py
gooookim's picture
Update app.py
bfb3e24 verified
import gradio as gr
from huggingface_hub import InferenceClient
from huggingface_hub.utils import HfHubHTTPError
def respond(
message,
history: list[dict[str, str]],
system_message,
max_tokens,
temperature,
top_p,
hf_token_text, # additional_inputs์—์„œ ์ž…๋ ฅ๋ฐ›์Œ
):
try:
if not hf_token_text or not str(hf_token_text).strip():
raise gr.Error("Hugging Face ํ† ํฐ์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. (hf_...)")
client = InferenceClient(token=str(hf_token_text).strip())
messages = [{"role": "system", "content": system_message}]
messages.extend(history)
messages.append({"role": "user", "content": message})
response = ""
stream = client.chat.completions.create(
model="openai/gpt-oss-20b", # โœ… 20B๋กœ ๋ณ€๊ฒฝ
messages=messages,
max_tokens=int(max_tokens),
stream=True,
temperature=float(temperature),
top_p=float(top_p),
)
for chunk in stream:
token = ""
try:
token = chunk.choices[0].delta.content or ""
except Exception:
token = ""
if token:
response += token
yield response
except HfHubHTTPError as e:
yield f"[์˜ค๋ฅ˜] Hugging Face ์š”์ฒญ ์‹คํŒจ: {e}"
except Exception as e:
yield f"[์˜ค๋ฅ˜] ๋ชจ๋ธ ํ˜ธ์ถœ ์‹คํŒจ: {type(e).__name__}: {e}"
with gr.Blocks() as demo:
with gr.Accordion("์„ค์ •", open=False):
system_message = gr.Textbox(
value="You are a friendly Chatbot.",
label="System message"
)
# โœ… 20B ๊ธฐ์ค€: ์ถœ๋ ฅ ํ† ํฐ ์ƒํ•œ ์ถ•์†Œ (ํ˜„์‹ค์ /์•ˆ์ •์ )
max_tokens = gr.Slider(
minimum=1,
maximum=4096, # โ† 20B์— ์ ํ•ฉํ•œ ์ƒํ•œ
value=1024,
step=1,
label="Max new tokens"
)
# โœ… ๊ธฐ๋ณธ๊ฐ’ 1.0 ์œ ์ง€ (์•ˆ์ •์ )
temperature = gr.Slider(
minimum=0.0,
maximum=2.0,
value=1.0,
step=0.05,
label="Temperature"
)
# โœ… ๊ธฐ๋ณธ๊ฐ’ 1.0 ์œ ์ง€
top_p = gr.Slider(
minimum=0.1,
maximum=1.0,
value=1.0,
step=0.05,
label="Top-p (nucleus sampling)"
)
hf_token_text = gr.Textbox(
label="HF Token (hf_...)",
type="password"
)
chatbot = gr.ChatInterface(
respond,
type="messages",
additional_inputs=[
system_message,
max_tokens,
temperature,
top_p,
hf_token_text,
],
)
if __name__ == "__main__":
demo.launch()