Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
def respond(message, history: list[dict[str,str]], system_message, max_tokens, temperature, top_p, hf_token
|
| 5 |
if not hf_token:
|
| 6 |
yield "⚠️ You must login first! (Bạn phải đăng nhập trước!)"
|
| 7 |
return
|
|
|
|
| 8 |
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 9 |
|
| 10 |
messages = [{"role": "system", "content": system_message}]
|
|
@@ -13,14 +14,14 @@ def respond(message, history: list[dict[str,str]], system_message, max_tokens, t
|
|
| 13 |
|
| 14 |
response = ""
|
| 15 |
try:
|
| 16 |
-
for
|
| 17 |
messages,
|
| 18 |
max_tokens=max_tokens,
|
| 19 |
stream=True,
|
| 20 |
temperature=temperature,
|
| 21 |
top_p=top_p,
|
| 22 |
):
|
| 23 |
-
token =
|
| 24 |
response += token
|
| 25 |
yield response
|
| 26 |
except Exception as e:
|
|
@@ -28,19 +29,24 @@ def respond(message, history: list[dict[str,str]], system_message, max_tokens, t
|
|
| 28 |
|
| 29 |
with gr.Blocks(css=".gradio-container {background-color: #1a1a1a; color: #eee;}") as demo:
|
| 30 |
with gr.Row():
|
|
|
|
| 31 |
with gr.Column(scale=3, min_width=300):
|
| 32 |
gr.Markdown("## ⚡ Login & Settings (Đăng nhập & Cài đặt)")
|
| 33 |
-
hf_token = gr.LoginButton(
|
|
|
|
| 34 |
system_message = gr.Textbox(value="You are a friendly Chatbot. (Bạn là chatbot thân thiện.)", label="System message", lines=2)
|
| 35 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max tokens")
|
| 36 |
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
| 37 |
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
|
|
|
|
|
|
| 38 |
with gr.Column(scale=7):
|
| 39 |
chatbot = gr.Chatbot(label="AI Chat (Trò chuyện với AI)").style(height=600)
|
| 40 |
with gr.Row():
|
| 41 |
msg = gr.Textbox(placeholder="Type your message here... (Gõ tin nhắn vào đây)", lines=1)
|
| 42 |
submit = gr.Button("Send (Gửi)")
|
| 43 |
|
|
|
|
| 44 |
submit.click(
|
| 45 |
respond,
|
| 46 |
inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p, hf_token],
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
def respond(message, history: list[dict[str,str]], system_message, max_tokens, temperature, top_p, hf_token):
|
| 5 |
if not hf_token:
|
| 6 |
yield "⚠️ You must login first! (Bạn phải đăng nhập trước!)"
|
| 7 |
return
|
| 8 |
+
|
| 9 |
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 10 |
|
| 11 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 14 |
|
| 15 |
response = ""
|
| 16 |
try:
|
| 17 |
+
for message_chunk in client.chat_completion(
|
| 18 |
messages,
|
| 19 |
max_tokens=max_tokens,
|
| 20 |
stream=True,
|
| 21 |
temperature=temperature,
|
| 22 |
top_p=top_p,
|
| 23 |
):
|
| 24 |
+
token = message_chunk.choices[0].delta.get("content","") if message_chunk.choices else ""
|
| 25 |
response += token
|
| 26 |
yield response
|
| 27 |
except Exception as e:
|
|
|
|
| 29 |
|
| 30 |
with gr.Blocks(css=".gradio-container {background-color: #1a1a1a; color: #eee;}") as demo:
|
| 31 |
with gr.Row():
|
| 32 |
+
# Sidebar
|
| 33 |
with gr.Column(scale=3, min_width=300):
|
| 34 |
gr.Markdown("## ⚡ Login & Settings (Đăng nhập & Cài đặt)")
|
| 35 |
+
hf_token = gr.LoginButton()
|
| 36 |
+
gr.Markdown("Login to HF (Đăng nhập HF)")
|
| 37 |
system_message = gr.Textbox(value="You are a friendly Chatbot. (Bạn là chatbot thân thiện.)", label="System message", lines=2)
|
| 38 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max tokens")
|
| 39 |
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
| 40 |
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
| 41 |
+
|
| 42 |
+
# Chat area
|
| 43 |
with gr.Column(scale=7):
|
| 44 |
chatbot = gr.Chatbot(label="AI Chat (Trò chuyện với AI)").style(height=600)
|
| 45 |
with gr.Row():
|
| 46 |
msg = gr.Textbox(placeholder="Type your message here... (Gõ tin nhắn vào đây)", lines=1)
|
| 47 |
submit = gr.Button("Send (Gửi)")
|
| 48 |
|
| 49 |
+
# Bind events
|
| 50 |
submit.click(
|
| 51 |
respond,
|
| 52 |
inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p, hf_token],
|