Spaces:
Running
Running
| import os | |
| import random | |
| import requests | |
| import gradio as gr | |
| import json | |
| # Загрузка CSS стилей | |
| css_url = "https://neurixyufi-aihub.static.hf.space/styles.css" | |
| try: | |
| response = requests.get(css_url) | |
| response.raise_for_status() | |
| css = response.text + " h1{text-align:center} #component-3 { height: 70vh !important; }" | |
| except requests.exceptions.RequestException as e: | |
| print(f"Ошибка при загрузке CSS: {e}") | |
| css = " h1{text-align:center} #component-3 { height: 70vh !important; }" # Используем базовый стиль, если загрузка CSS не удалась | |
| # Функция для отправки запроса к API searchgpt | |
| def chat_with_searchgpt(user_message="", history=[], max_tokens=2500, temperature=0.7, top_p=0.95): | |
| if user_message == "": | |
| return history, user_message | |
| api_url = os.getenv("API_BASE", "") | |
| headers = { | |
| "Content-Type": "application/json" | |
| } | |
| # Формируем массив сообщений | |
| messages = [] | |
| for msg in history: | |
| messages.append({"role": "user", "content": msg[0]}) | |
| messages.append({"role": "assistant", "content": msg[1]}) | |
| messages.append({"role": "user", "content": user_message}) | |
| data = { | |
| "messages": messages, | |
| "model": "searchgpt" | |
| } | |
| try: | |
| response = requests.post(api_url, headers=headers, json=data) | |
| response.raise_for_status() # Поднимаем исключение, если статус ответа не 200 | |
| response_data = response.json() | |
| bot_reply = response_data.get("response", "Извините, не удалось получить ответ.") | |
| except requests.exceptions.RequestException as e: | |
| bot_reply = f"Ошибка при запросе к API: {e}" | |
| data = response.json() | |
| history.append((user_message, data['choices'][0]['message']['content'])) | |
| return history, "" | |
| # Определение интерфейса | |
| with gr.Blocks(css=css) as demo: | |
| gr.Markdown("# ИИ Поиск") | |
| with gr.Column(): | |
| chatbot = gr.Chatbot(label="Чат", show_share_button=False, type='messages') | |
| msg = gr.Textbox(show_label=False, placeholder="Введите ваш запрос...", lines=2) | |
| send = gr.Button("Отправить", variant="primary") | |
| with gr.Accordion("Настройки помощника", open=False): | |
| max_tokens = gr.Slider(minimum=100, maximum=20000, value=2500, 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 (нуклеарное сэмплирование)") | |
| # Функция для очистки чата | |
| def clear_chat(): | |
| return [], "" | |
| # Обработка отправки сообщения | |
| msg.submit(chat_with_searchgpt, [msg, chatbot, max_tokens, temperature, top_p], [chatbot, msg], concurrency_limit=250) | |
| send.click(chat_with_searchgpt, [msg, chatbot, max_tokens, temperature, top_p], [chatbot, msg], concurrency_limit=250) | |
| # Запуск интерфейса | |
| demo.launch(show_api=False, share=False) | |