Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
|
@@ -17,48 +21,40 @@ def respond(
|
|
| 17 |
):
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
for
|
| 21 |
-
if
|
| 22 |
-
messages.append({"role": "user", "content":
|
| 23 |
-
if
|
| 24 |
-
messages.append({"role": "assistant", "content":
|
| 25 |
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
response = ""
|
| 29 |
|
| 30 |
-
for
|
| 31 |
-
messages,
|
| 32 |
max_tokens=max_tokens,
|
| 33 |
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a
|
| 50 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
],
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# Lấy token từ biến môi trường (đã tạo trong Secrets với tên HF_TOKEN)
|
| 6 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Khởi tạo client với model GPT-OSS-120B
|
| 9 |
+
client = InferenceClient(
|
| 10 |
+
model="openai/gpt-oss-120b",
|
| 11 |
+
token=HF_TOKEN,
|
| 12 |
+
)
|
| 13 |
|
| 14 |
def respond(
|
| 15 |
message,
|
|
|
|
| 21 |
):
|
| 22 |
messages = [{"role": "system", "content": system_message}]
|
| 23 |
|
| 24 |
+
for user_msg, bot_msg in history:
|
| 25 |
+
if user_msg:
|
| 26 |
+
messages.append({"role": "user", "content": user_msg})
|
| 27 |
+
if bot_msg:
|
| 28 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 29 |
|
| 30 |
messages.append({"role": "user", "content": message})
|
| 31 |
|
| 32 |
response = ""
|
| 33 |
|
| 34 |
+
for response_chunk in client.chat_completion(
|
| 35 |
+
messages=messages,
|
| 36 |
max_tokens=max_tokens,
|
| 37 |
stream=True,
|
| 38 |
temperature=temperature,
|
| 39 |
top_p=top_p,
|
| 40 |
):
|
| 41 |
+
delta = response_chunk.choices[0].delta.content
|
| 42 |
+
if delta:
|
| 43 |
+
response += delta
|
| 44 |
+
yield response
|
| 45 |
|
| 46 |
+
# UI với Gradio
|
|
|
|
|
|
|
|
|
|
| 47 |
demo = gr.ChatInterface(
|
| 48 |
respond,
|
| 49 |
additional_inputs=[
|
| 50 |
+
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
| 51 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
],
|
| 55 |
+
title="Chat with GPT-OSS-120B",
|
| 56 |
+
description="Inference via Hugging Face Hub – powered by openai/gpt-oss-120b"
|
| 57 |
)
|
| 58 |
|
|
|
|
| 59 |
if __name__ == "__main__":
|
| 60 |
demo.launch()
|