Spaces:
Paused
Paused
| 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() | |