Update app.py
Browse files
app.py
CHANGED
@@ -19,19 +19,14 @@ def send_message(message, image=None):
|
|
19 |
css = """
|
20 |
footer {visibility: hidden !important;}
|
21 |
.chat-container {height: 90vh; display: flex; flex-direction: column;}
|
22 |
-
.chat-messages {flex: 1; overflow-y: auto; padding: 10px;}
|
23 |
.input-group {display: flex; align-items: center; padding: 10px;}
|
24 |
.image-preview {max-width: 200px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1);}
|
25 |
"""
|
26 |
|
27 |
# Создаем интерфейс
|
28 |
with gr.Blocks(css=css) as demo:
|
29 |
-
|
30 |
-
container=True,
|
31 |
-
scroll="auto",
|
32 |
-
height="auto",
|
33 |
-
max_height="400px"
|
34 |
-
)
|
35 |
with gr.Row():
|
36 |
input_message = gr.Textbox(placeholder="Введите ваше сообщение здесь...", lines=2, interactive=True, max_lines=5)
|
37 |
send_button = gr.Button("Отправить")
|
@@ -46,29 +41,31 @@ with gr.Blocks(css=css) as demo:
|
|
46 |
attach_button.style(visible=False)
|
47 |
image = Image.open(io.BytesIO(file_info["content"]))
|
48 |
img_str = encode_image_to_base64(image)
|
49 |
-
return f"data:image/jpeg;base64,{img_str}"
|
50 |
else:
|
51 |
clear_button.style(visible=False)
|
52 |
attach_button.style(visible=True)
|
53 |
-
return
|
54 |
|
55 |
def clear_image():
|
56 |
attach_button.reset()
|
57 |
clear_button.style(visible=False)
|
58 |
attach_button.style(visible=True)
|
59 |
-
return
|
60 |
|
61 |
def handle_send(message, image=None):
|
62 |
response, img_str = send_message(message, image)
|
63 |
-
new_message = f"
|
64 |
-
|
65 |
-
|
|
|
|
|
66 |
input_message.reset()
|
67 |
clear_image()
|
68 |
|
69 |
send_button.click(handle_send, inputs=[input_message, attach_button], outputs=[])
|
70 |
-
attach_button.change(preview_image, inputs=[attach_button], outputs=[
|
71 |
-
clear_button.click(clear_image, inputs=[], outputs=[
|
72 |
|
73 |
# Запускаем интерфейс
|
74 |
demo.launch()
|
|
|
19 |
css = """
|
20 |
footer {visibility: hidden !important;}
|
21 |
.chat-container {height: 90vh; display: flex; flex-direction: column;}
|
22 |
+
.chat-messages {flex: 1; overflow-y: auto; padding: 10px; background-color: #f0f0f0; border-radius: 10px; margin-bottom: 10px;}
|
23 |
.input-group {display: flex; align-items: center; padding: 10px;}
|
24 |
.image-preview {max-width: 200px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1);}
|
25 |
"""
|
26 |
|
27 |
# Создаем интерфейс
|
28 |
with gr.Blocks(css=css) as demo:
|
29 |
+
chat_history_container = gr.HTML("<div class='chat-messages'>Привет! Я Помогатор, готов помочь тебе с любыми вопросами!) 😊</div>")
|
|
|
|
|
|
|
|
|
|
|
30 |
with gr.Row():
|
31 |
input_message = gr.Textbox(placeholder="Введите ваше сообщение здесь...", lines=2, interactive=True, max_lines=5)
|
32 |
send_button = gr.Button("Отправить")
|
|
|
41 |
attach_button.style(visible=False)
|
42 |
image = Image.open(io.BytesIO(file_info["content"]))
|
43 |
img_str = encode_image_to_base64(image)
|
44 |
+
return f"<div class='chat-messages'><img class='image-preview' src='data:image/jpeg;base64,{img_str}' /></div>"
|
45 |
else:
|
46 |
clear_button.style(visible=False)
|
47 |
attach_button.style(visible=True)
|
48 |
+
return "<div class='chat-messages'></div>"
|
49 |
|
50 |
def clear_image():
|
51 |
attach_button.reset()
|
52 |
clear_button.style(visible=False)
|
53 |
attach_button.style(visible=True)
|
54 |
+
return "<div class='chat-messages'></div>"
|
55 |
|
56 |
def handle_send(message, image=None):
|
57 |
response, img_str = send_message(message, image)
|
58 |
+
new_message = f"<div class='chat-messages'><strong>Вы:</strong> {message}</div>" if message else ""
|
59 |
+
if img_str:
|
60 |
+
new_message += f"<div class='chat-messages'><img class='image-preview' src='data:image/jpeg;base64,{img_str}' /></div>"
|
61 |
+
new_message += f"<div class='chat-messages'><strong>Помогатор:</strong> {response}</div>"
|
62 |
+
chat_history_container.update(new_message + chat_history_container.value)
|
63 |
input_message.reset()
|
64 |
clear_image()
|
65 |
|
66 |
send_button.click(handle_send, inputs=[input_message, attach_button], outputs=[])
|
67 |
+
attach_button.change(preview_image, inputs=[attach_button], outputs=[chat_history_container])
|
68 |
+
clear_button.click(clear_image, inputs=[], outputs=[chat_history_container])
|
69 |
|
70 |
# Запускаем интерфейс
|
71 |
demo.launch()
|