Spaces:
Sleeping
Sleeping
AndreySokolov01
commited on
Commit
•
7cdf99a
1
Parent(s):
8d15bc3
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,52 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
# ...
|
4 |
-
|
5 |
-
with gr.Blocks() as demo:
|
6 |
-
with gr.Row():
|
7 |
-
chatbot = gr.Chatbot()
|
8 |
-
with gr.Column():
|
9 |
-
user_input = gr.Textbox(label="Введите текст из чата")
|
10 |
-
generate_button = gr.Button("Сохранить в PDF")
|
11 |
-
pdf_link = gr.Textbox(label="Ссылка на PDF")
|
12 |
-
|
13 |
-
generate_button.click(generate_pdf, inputs=[user_input], outputs=[pdf_link])
|
14 |
-
|
15 |
import pdfkit
|
16 |
|
|
|
17 |
def generate_pdf(text):
|
18 |
-
# Создаем HTML
|
19 |
html_content = f"""
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
body {{
|
26 |
font-family: Arial, sans-serif;
|
27 |
font-size: 14px;
|
28 |
line-height: 1.5;
|
29 |
}}
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
"""
|
38 |
|
39 |
-
# Сохраняем HTML
|
40 |
-
|
|
|
|
|
|
|
41 |
html_file.write(html_content)
|
42 |
|
43 |
# Конвертируем HTML в PDF
|
44 |
-
pdfkit.from_file(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import pdfkit
|
3 |
|
4 |
+
# Функция для генерации PDF из введенного текста
|
5 |
def generate_pdf(text):
|
6 |
+
# Создаем HTML-контент с текстом
|
7 |
html_content = f"""
|
8 |
+
<!DOCTYPE html>
|
9 |
+
<html>
|
10 |
+
<head>
|
11 |
+
<title>Диалог из чата</title>
|
12 |
+
<style>
|
13 |
body {{
|
14 |
font-family: Arial, sans-serif;
|
15 |
font-size: 14px;
|
16 |
line-height: 1.5;
|
17 |
}}
|
18 |
+
</style>
|
19 |
+
</head>
|
20 |
+
<body>
|
21 |
+
<h1>Диалог из чата</h1>
|
22 |
+
<p>{text}</p>
|
23 |
+
</body>
|
24 |
+
</html>
|
25 |
"""
|
26 |
|
27 |
+
# Сохраняем HTML-контент во временный файл
|
28 |
+
html_path = "/mnt/data/chat_dialog.html"
|
29 |
+
pdf_path = "/mnt/data/chat_dialog.pdf"
|
30 |
+
|
31 |
+
with open(html_path, "w", encoding="utf-8") as html_file:
|
32 |
html_file.write(html_content)
|
33 |
|
34 |
# Конвертируем HTML в PDF
|
35 |
+
pdfkit.from_file(html_path, pdf_path)
|
36 |
+
|
37 |
+
return pdf_path
|
38 |
+
|
39 |
+
# Интерфейс Gradio
|
40 |
+
with gr.Blocks() as demo:
|
41 |
+
with gr.Row():
|
42 |
+
chatbot = gr.Chatbot()
|
43 |
+
with gr.Column():
|
44 |
+
user_input = gr.Textbox(label="Введите текст из чата")
|
45 |
+
generate_button = gr.Button("Сохранить в PDF")
|
46 |
+
pdf_link = gr.Textbox(label="Ссылка на PDF")
|
47 |
+
|
48 |
+
# Устанавливаем действие для кнопки
|
49 |
+
generate_button.click(generate_pdf, inputs=[user_input], outputs=[pdf_link])
|
50 |
|
51 |
+
# Запуск приложения
|
52 |
+
demo.launch()
|