|
import gradio as gr |
|
from gradio_client import Client |
|
import requests |
|
import json |
|
|
|
|
|
client = Client("llamameta/Pixtral-Large-Instruct-2411") |
|
|
|
|
|
def load_system_role(role_name): |
|
with open('system_roles.json', 'r', encoding='utf-8') as file: |
|
roles = json.load(file) |
|
return roles.get(role_name, "Ты помощник по умолчанию.") |
|
|
|
|
|
def load_role_names(): |
|
with open('system_roles.json', 'r', encoding='utf-8') as file: |
|
roles = json.load(file) |
|
return list(roles.keys()) |
|
|
|
def respond( |
|
message, |
|
history: list[tuple[str, str]], |
|
system_role_name, |
|
max_tokens, |
|
temperature, |
|
top_p, |
|
): |
|
if not message: |
|
return history, "" |
|
system_role = load_system_role(system_role_name) |
|
|
|
messages = [{"role": "system", "content": system_role}] |
|
|
|
for val in history: |
|
if val[0]: |
|
messages.append({"role": "user", "content": val[0]}) |
|
if val[1]: |
|
messages.append({"role": "assistant", "content": val[1]}) |
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
response = client.predict( |
|
message=message, |
|
system_message=system_role, |
|
max_tokens=max_tokens, |
|
temperature=temperature, |
|
top_p=top_p, |
|
api_name="/chat" |
|
) |
|
|
|
|
|
history.append((message, response)) |
|
|
|
return history, "" |
|
|
|
|
|
role_names = load_role_names() |
|
|
|
|
|
css_url = "https://neurixyufi-aihub.static.hf.space/style.css" |
|
|
|
|
|
response = requests.get(css_url) |
|
css = response.text + " .gradio-container{max-width: 700px !important} h1{text-align:center} #component-4 { height: 70vh !important; }" |
|
|
|
|
|
with gr.Blocks(css=css) as demo: |
|
gr.Markdown("# Помощник") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
chatbot = gr.Chatbot(show_label=False) |
|
message = gr.Textbox(label="Введите ваше сообщение", placeholder="Введите ваше сообщение здесь...", lines=3, container=False) |
|
submit = gr.Button("Отправить") |
|
|
|
with gr.Accordion("Настройки помощника", open=False): |
|
with gr.Accordion(label="Помощник", open=False): |
|
helper_role = gr.Radio(show_label=True, label="Выберите помощника", interactive=True, choices=role_names, value=role_names[0]) |
|
max_tokens = gr.Slider(minimum=100, maximum=18000, value=18000, step=1, label="Максимальное количество новых токенов") |
|
temperature = gr.Slider(minimum=0.0, maximum=1.0, value=0.7, step=0.1, label="Температура") |
|
top_p = gr.Slider(minimum=0.0, maximum=1.0, value=0.95, step=0.05, label="Top-p (нуклеарное сэмплирование)") |
|
|
|
|
|
submit.click( |
|
fn=respond, |
|
inputs=[message, chatbot, helper_role, max_tokens, temperature, top_p], |
|
outputs=[chatbot, message] |
|
) |
|
|
|
|
|
message.submit( |
|
fn=respond, |
|
inputs=[message, chatbot, helper_role, max_tokens, temperature, top_p], |
|
outputs=[chatbot, message] |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.queue(max_size=250).launch(show_api=False, share=False) |
|
|