679 / app.py
Pokilondron56's picture
Update app.py
3f3b7ee verified
import gradio as gr
from huggingface_hub import InferenceClient
def respond(
message,
history: list[dict[str, str]],
system_message,
max_tokens,
temperature,
top_p,
hf_token: gr.OAuthToken,
):
"""
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
"""
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
messages = [{"role": "system", "content": system_message}]
messages.extend(history)
messages.append({"role": "user", "content": message})
response = ""
for message in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
choices = message.choices
token = ""
if len(choices) and choices[0].delta.content:
token = choices[0].delta.content
response += token
yield response
with gr.Blocks() as demo:
gr.Markdown("#Pok.Bot")
gr.Markdown("Задай вопрос ПОК.Боту! (Он отвечает с сарказмом 😎)")
with gr.Row():
prompt = gr.Textbox(
label="Твой вопрос",
placeholder="Спроси о...",
lines=2
)
temperature = gr.Slider(
label="Температура",
minimum=0.1,
maximum=1.5,
value=0.9,
step=0.1
)
with gr.Row():
max_length = gr.Slider(
label="Макс. длина ответа",
minimum=10,
maximum=100,
value=50,
step=10
)
submit_btn = gr.Button("Спросить ПОК.Бота")
output = gr.Textbox(
label="Ответ ПОК.Бота",
lines=5,
interactive=False
)
# Привязываем кнопку к функции
submit_btn.click(
fn=respond ,
inputs=[prompt, temperature, max_length],
outputs=output
)
with gr.Blocks() as demo:
with gr.Sidebar():
gr.LoginButton()
demo.render
if __name__ == "__main__":
demo.launch()