File size: 4,739 Bytes
8ed172b
 
 
9fe83f7
 
 
8ed172b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fe83f7
 
 
 
 
 
8ed172b
9fe83f7
8ed172b
 
 
 
 
 
 
 
9fe83f7
 
 
 
 
 
 
 
 
8ed172b
 
 
55257fd
8ed172b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f043858
8ed172b
f043858
 
 
 
 
 
8ed172b
f043858
 
 
8ed172b
 
 
f043858
 
 
 
bb5e3b0
8ed172b
 
bb5e3b0
8ed172b
 
 
f043858
 
 
 
 
 
 
8ed172b
 
 
 
 
9fe83f7
8ed172b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# app.py
import gradio as gr
from g4f.client import Client
import base64
from PIL import Image
import io

client = Client()

# --- Функции для очистки ответов ---
import re
from typing import List, Tuple, Any

def strip_md_refs(text: str) -> str:
    """
    Удаляет из строки все конструкции вида [[N]](URL),
    где N – произвольное число (может быть и несколько цифр).
    Возвращает «чистый» текст без ссылок.
    """
    pattern = r'\[\[\d+\]\]\([^\)]*\)'
    return re.sub(pattern, '', text)

def strip_citations(text: str) -> str:
    """
    Удаляет из текста все строки вида:
    а также любые блоки, начинающиеся с символа '>' (цитаты).
    Возвращает «чистый» текст без «концовки».
    """
    text = re.sub(r'^\s*>.*(?:\n|$)', '', text, flags=re.MULTILINE)
    text = re.sub(r'^\s*\[\d+\]\s*.+(?:\n|$)', '', text, flags=re.MULTILINE)
    text = re.sub(r'\n{2,}', '\n\n', text).strip()
    return text

def image_to_base64(image):
    """Конвертирует изображение в base64 строку"""
    buffered = io.BytesIO()
    image.save(buffered, format="PNG")
    return base64.b64encode(buffered.getvalue()).decode('utf-8')

# --- Обработчик диалога ---
def respond(message, history, system_prompt, image=None):
    # Используем пользовательский промт из интерфейса
    messages = [{"role": "system", "content": system_prompt}]
    
    # Добавляем историю диалога
    for human, assistant in history:
        messages.append({"role": "user", "content": human})
        messages.append({"role": "assistant", "content": assistant})
    
    # Формируем текущее сообщение
    current_message = {"role": "user", "content": message}
    
    # Если есть изображение, добавляем его к сообщению
    if image is not None:
        image_base64 = image_to_base64(image)
        current_message["images"] = [image_base64]
    
    messages.append(current_message)

    try:
        responsed = client.chat.completions.create(
            model="gpt-4",
            messages=messages,
            temperature=0.8,
            top_p=0.95,
            max_tokens=581691,
            presence_penalty=0.2,
            frequency_penalty=0.1,
        )
        bot_message = strip_citations(f"{strip_md_refs(responsed.choices[0].message.content)}")

    except Exception as e:
        bot_message = f"Ошибка: {str(e)}"
    return history + [[message, bot_message]]

# --- Интерфейс ---
with gr.Blocks(title="ESP Brain") as demo:
    gr.Markdown("## ESP Brain - Настраиваемый ассистент")

   # Поле для ввода системного промта
    system_prompt_input = gr.Textbox(
    label="Системный промт",
    value=""" """,
    lines=5,
    max_lines=10,
    placeholder="Введите системный промт для настройки поведения ассистента..."
)

    chatbot = gr.Chatbot(
    height=600,
)

    with gr.Row():
        txt = gr.Textbox(
        placeholder="Напиши сообщение...",
        show_label=False,
        scale=8
    )
        submit_btn = gr.Button("Отправить", scale=2)

    with gr.Row():
        retry_btn = gr.Button("🔄 Повторить")
        undo_btn = gr.Button("↩️ Отменить")
        clear_btn = gr.Button("🗑️ Очистить")

# Логика
    def submit_message(message, history, system_prompt):
        return respond(message, history, system_prompt)

    txt.submit(fn=submit_message, inputs=[txt, chatbot, system_prompt_input], outputs=chatbot)
    submit_btn.click(fn=submit_message, inputs=[txt, chatbot, system_prompt_input], outputs=chatbot)


    def retry_last(history, system_prompt):
        if history:
            last_user_msg = history[-1][0]
            new_history = history[:-1]
            return respond(last_user_msg, new_history, system_prompt, None)
        return history

    def undo_last(history):
        return history[:-1]

    retry_btn.click(fn=retry_last, inputs=[chatbot, system_prompt_input], outputs=chatbot, queue=False)
    undo_btn.click(fn=undo_last, inputs=chatbot, outputs=chatbot, queue=False)
    clear_btn.click(lambda: [], outputs=chatbot, queue=False)

# --- Запуск ---
if __name__ == "__main__":
    demo.queue()
    demo.launch(
        share=True,
        ssr_mode=False,
        debug=True
    )