Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import subprocess
|
| 4 |
+
import threading
|
| 5 |
+
import sys
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
SCRIPT_CMD = "bash ./script.sh"
|
| 9 |
+
|
| 10 |
+
def respond(
|
| 11 |
+
message,
|
| 12 |
+
history: list[dict[str, str]],
|
| 13 |
+
system_message,
|
| 14 |
+
max_tokens,
|
| 15 |
+
temperature,
|
| 16 |
+
top_p,
|
| 17 |
+
hf_token: gr.OAuthToken,
|
| 18 |
+
):
|
| 19 |
+
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 20 |
+
|
| 21 |
+
messages = [{"role": "system", "content": system_message}]
|
| 22 |
+
messages.extend(history)
|
| 23 |
+
messages.append({"role": "user", "content": message})
|
| 24 |
+
|
| 25 |
+
response = ""
|
| 26 |
+
for msg in client.chat_completion(
|
| 27 |
+
messages,
|
| 28 |
+
max_tokens=max_tokens,
|
| 29 |
+
stream=True,
|
| 30 |
+
temperature=temperature,
|
| 31 |
+
top_p=top_p,
|
| 32 |
+
):
|
| 33 |
+
choices = msg.choices
|
| 34 |
+
token = ""
|
| 35 |
+
if len(choices) and choices[0].delta.content:
|
| 36 |
+
token = choices[0].delta.content
|
| 37 |
+
response += token
|
| 38 |
+
yield response
|
| 39 |
+
|
| 40 |
+
chatbot = gr.ChatInterface(
|
| 41 |
+
respond,
|
| 42 |
+
type="messages",
|
| 43 |
+
additional_inputs=[
|
| 44 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 45 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 46 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 47 |
+
gr.Slider(
|
| 48 |
+
minimum=0.1,
|
| 49 |
+
maximum=1.0,
|
| 50 |
+
value=0.95,
|
| 51 |
+
step=0.05,
|
| 52 |
+
label="Top-p (nucleus sampling)",
|
| 53 |
+
),
|
| 54 |
+
],
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
def stream_script(cmd):
|
| 58 |
+
p = subprocess.Popen(
|
| 59 |
+
cmd,
|
| 60 |
+
stdout=subprocess.PIPE,
|
| 61 |
+
stderr=subprocess.STDOUT,
|
| 62 |
+
shell=True,
|
| 63 |
+
text=True,
|
| 64 |
+
bufsize=1,
|
| 65 |
+
)
|
| 66 |
+
for line in p.stdout:
|
| 67 |
+
sys.stdout.write(line)
|
| 68 |
+
p.wait()
|
| 69 |
+
sys.stdout.write(f"[script exited with code {p.returncode}]\n")
|
| 70 |
+
|
| 71 |
+
with gr.Blocks() as demo:
|
| 72 |
+
with gr.Sidebar():
|
| 73 |
+
gr.LoginButton()
|
| 74 |
+
chatbot.render()
|
| 75 |
+
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
# Chạy Gradio không block
|
| 78 |
+
demo.launch(prevent_thread_lock=True)
|
| 79 |
+
|
| 80 |
+
# Chạy shell script song song
|
| 81 |
+
t_script = threading.Thread(target=stream_script, args=(SCRIPT_CMD,))
|
| 82 |
+
t_script.start()
|
| 83 |
+
|
| 84 |
+
# Giữ main thread sống, kể cả sau khi script xong
|
| 85 |
+
try:
|
| 86 |
+
while True:
|
| 87 |
+
time.sleep(1)
|
| 88 |
+
except KeyboardInterrupt:
|
| 89 |
+
sys.stdout.write("[shutdown requested]\n")
|