Spaces:
Running
Running
File size: 3,412 Bytes
fc396f9 eb28b5b fc396f9 49fa9d1 fc396f9 49fa9d1 fc396f9 cbebc0d 75b394e fc396f9 649e9fd fc396f9 0420086 4b10ab1 fc396f9 4d87603 3f5305f fc396f9 716eac0 649e9fd fc396f9 cbebc0d aab6f6c cbebc0d fc396f9 649e9fd fc396f9 cbebc0d 7cc7a4d fc396f9 649e9fd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import os
import random
import requests
import gradio as gr
import json
# Загрузка CSS стилей
css_url = "https://neurixyufi-aihub.static.hf.space/style.css"
try:
response = requests.get(css_url)
response.raise_for_status() # Поднимаем исключение, если статус ответа не 200
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)
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)
|