Spaces:
Sleeping
Sleeping
Studio
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,6 +19,15 @@ def summarize_document(file):
|
|
| 19 |
summary = sum_tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 20 |
return summary
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# -------------------------------
|
| 23 |
# Модель вопросов-ответов (Q&A)
|
| 24 |
# -------------------------------
|
|
@@ -55,57 +64,70 @@ def set_language(lang):
|
|
| 55 |
texts = {
|
| 56 |
"app_title": "# Interface for Summarization and Q&A",
|
| 57 |
"sum_section": "Document Summarization",
|
|
|
|
| 58 |
"sum_file_label": "Attach file for summarization",
|
|
|
|
|
|
|
| 59 |
"sum_button": "Summarize",
|
| 60 |
"sum_output": "Summarization",
|
| 61 |
"qa_section": "Document Q&A",
|
| 62 |
"qa_tab_file": "Upload File",
|
| 63 |
-
"qa_tab_text": "Enter Text",
|
| 64 |
"qa_file_label": "Attach file with document",
|
| 65 |
"qa_question_label": "Enter your question",
|
| 66 |
"qa_answer_button": "Get Answer",
|
| 67 |
"qa_answer_label": "Answer",
|
|
|
|
| 68 |
"qa_text_label": "Enter document text",
|
| 69 |
}
|
| 70 |
else: # Русский
|
| 71 |
texts = {
|
| 72 |
"app_title": "# Интерфейс для суммаризации и вопросов-ответов",
|
| 73 |
"sum_section": "Суммаризация документа",
|
|
|
|
| 74 |
"sum_file_label": "Прикрепить файл для суммаризации",
|
|
|
|
|
|
|
| 75 |
"sum_button": "Суммаризировать",
|
| 76 |
"sum_output": "Суммаризация",
|
| 77 |
"qa_section": "Вопрос-ответ по документу",
|
| 78 |
"qa_tab_file": "Загрузить файл",
|
| 79 |
-
"qa_tab_text": "Ввести текст",
|
| 80 |
"qa_file_label": "Прикрепить файл с документом",
|
| 81 |
"qa_question_label": "Введите вопрос",
|
| 82 |
"qa_answer_button": "Получить ответ",
|
| 83 |
"qa_answer_label": "Ответ",
|
|
|
|
| 84 |
"qa_text_label": "Введите текст документа",
|
| 85 |
}
|
| 86 |
return (
|
| 87 |
-
gr.update(visible=False),
|
| 88 |
-
gr.update(visible=True),
|
| 89 |
-
gr.update(value=texts["app_title"]),
|
| 90 |
-
|
| 91 |
-
gr.update(
|
| 92 |
-
gr.update(
|
| 93 |
-
gr.update(label=texts["
|
| 94 |
-
gr.update(value=
|
| 95 |
-
gr.update(label=texts["
|
| 96 |
-
gr.update(label=texts["
|
| 97 |
-
gr.update(label=texts["
|
| 98 |
-
gr.update(
|
| 99 |
-
gr.update(
|
| 100 |
-
|
| 101 |
-
gr.update(
|
| 102 |
-
gr.update(label=texts["
|
| 103 |
-
gr.update(
|
| 104 |
-
gr.update(label=texts["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
)
|
| 106 |
|
| 107 |
# -------------------------------
|
| 108 |
-
# Интерфейс Gradio с улучшенным
|
| 109 |
# -------------------------------
|
| 110 |
with gr.Blocks(css="""
|
| 111 |
body { background-color: #f0f2f5; }
|
|
@@ -115,7 +137,7 @@ with gr.Blocks(css="""
|
|
| 115 |
""") as demo:
|
| 116 |
gr.HTML("<style>body { background-color: #f0f2f5; } .gradio-container { border-radius: 10px; box-shadow: 0 0 15px rgba(0,0,0,0.1); padding: 20px; background-color: white; } h1, h2, h3 { color: #333; } .gr-button { background-color: #4CAF50; color: white; }</style>")
|
| 117 |
|
| 118 |
-
# Контейнер выбора языка (показывается
|
| 119 |
with gr.Column(visible=True) as lang_container:
|
| 120 |
lang_radio = gr.Radio(choices=["English", "Русский"], label="Select Language / Выберите язык", value="English")
|
| 121 |
start_button = gr.Button("Start / Начать")
|
|
@@ -124,16 +146,22 @@ with gr.Blocks(css="""
|
|
| 124 |
with gr.Column(visible=False) as main_container:
|
| 125 |
header_markdown = gr.Markdown("Placeholder Title")
|
| 126 |
with gr.Row():
|
| 127 |
-
# Левая колонка –
|
| 128 |
with gr.Column():
|
| 129 |
sum_section_md = gr.Markdown("Placeholder Summarization Section")
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
with gr.Column():
|
| 135 |
qa_section_md = gr.Markdown("Placeholder Q&A Section")
|
| 136 |
-
with gr.Tabs() as
|
| 137 |
with gr.Tab("Placeholder Tab 1") as file_tab:
|
| 138 |
file_input_qa = gr.File(label="Placeholder QA File Input", file_count="single", type="filepath")
|
| 139 |
question_input_file = gr.Textbox(label="Placeholder QA Question", placeholder="Your question here")
|
|
@@ -145,22 +173,30 @@ with gr.Blocks(css="""
|
|
| 145 |
answer_button_text = gr.Button("Placeholder QA Answer Button")
|
| 146 |
answer_output_text = gr.Textbox(label="Placeholder QA Answer Output", lines=5)
|
| 147 |
|
| 148 |
-
# При
|
| 149 |
start_button.click(
|
| 150 |
set_language,
|
| 151 |
inputs=[lang_radio],
|
| 152 |
outputs=[
|
| 153 |
lang_container, main_container,
|
| 154 |
-
header_markdown,
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
]
|
| 160 |
)
|
| 161 |
|
| 162 |
# Связываем функционал нейросетей
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
| 164 |
answer_button_file.click(fn=answer_question_file, inputs=[file_input_qa, question_input_file], outputs=answer_output_file)
|
| 165 |
answer_button_text.click(fn=answer_question_text, inputs=[context_input, question_input_text], outputs=answer_output_text)
|
| 166 |
|
|
|
|
| 19 |
summary = sum_tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 20 |
return summary
|
| 21 |
|
| 22 |
+
def summarize_text(text):
|
| 23 |
+
if not text:
|
| 24 |
+
return "Текст не предоставлен."
|
| 25 |
+
input_text = "summarize: " + text
|
| 26 |
+
inputs = sum_tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
|
| 27 |
+
summary_ids = sum_model.generate(inputs["input_ids"], max_length=150, num_beams=4, early_stopping=True)
|
| 28 |
+
summary = sum_tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 29 |
+
return summary
|
| 30 |
+
|
| 31 |
# -------------------------------
|
| 32 |
# Модель вопросов-ответов (Q&A)
|
| 33 |
# -------------------------------
|
|
|
|
| 64 |
texts = {
|
| 65 |
"app_title": "# Interface for Summarization and Q&A",
|
| 66 |
"sum_section": "Document Summarization",
|
| 67 |
+
"sum_tab_file": "Upload File",
|
| 68 |
"sum_file_label": "Attach file for summarization",
|
| 69 |
+
"sum_tab_text": "Enter Text",
|
| 70 |
+
"sum_text_label": "Enter document text",
|
| 71 |
"sum_button": "Summarize",
|
| 72 |
"sum_output": "Summarization",
|
| 73 |
"qa_section": "Document Q&A",
|
| 74 |
"qa_tab_file": "Upload File",
|
|
|
|
| 75 |
"qa_file_label": "Attach file with document",
|
| 76 |
"qa_question_label": "Enter your question",
|
| 77 |
"qa_answer_button": "Get Answer",
|
| 78 |
"qa_answer_label": "Answer",
|
| 79 |
+
"qa_tab_text": "Enter Text",
|
| 80 |
"qa_text_label": "Enter document text",
|
| 81 |
}
|
| 82 |
else: # Русский
|
| 83 |
texts = {
|
| 84 |
"app_title": "# Интерфейс для суммаризации и вопросов-ответов",
|
| 85 |
"sum_section": "Суммаризация документа",
|
| 86 |
+
"sum_tab_file": "Загрузить файл",
|
| 87 |
"sum_file_label": "Прикрепить файл для суммаризации",
|
| 88 |
+
"sum_tab_text": "Ввести текст",
|
| 89 |
+
"sum_text_label": "Введите текст документа",
|
| 90 |
"sum_button": "Суммаризировать",
|
| 91 |
"sum_output": "Суммаризация",
|
| 92 |
"qa_section": "Вопрос-ответ по документу",
|
| 93 |
"qa_tab_file": "Загрузить файл",
|
|
|
|
| 94 |
"qa_file_label": "Прикрепить файл с документом",
|
| 95 |
"qa_question_label": "Введите вопрос",
|
| 96 |
"qa_answer_button": "Получить ответ",
|
| 97 |
"qa_answer_label": "Ответ",
|
| 98 |
+
"qa_tab_text": "Ввести текст",
|
| 99 |
"qa_text_label": "Введите текст документа",
|
| 100 |
}
|
| 101 |
return (
|
| 102 |
+
gr.update(visible=False), # Скрыть выбор языка
|
| 103 |
+
gr.update(visible=True), # Показать основной интерфейс
|
| 104 |
+
gr.update(value=texts["app_title"]), # Заголовок приложения
|
| 105 |
+
# --- Обновление комп��нентов для суммаризации ---
|
| 106 |
+
gr.update(value="### " + texts["sum_section"]), # Заголовок секции суммаризации
|
| 107 |
+
gr.update(label=texts["sum_tab_file"]), # Название вкладки (файл)
|
| 108 |
+
gr.update(label=texts["sum_file_label"]), # Метка компонента File (суммаризация, файл)
|
| 109 |
+
gr.update(value=texts["sum_button"]), # Кнопка суммаризации (файл)
|
| 110 |
+
gr.update(label=texts["sum_output"]), # Вывод суммаризации (файл)
|
| 111 |
+
gr.update(label=texts["sum_tab_text"]), # Название вкладки (текст)
|
| 112 |
+
gr.update(label=texts["sum_text_label"]), # Метка компонента Text (суммаризация, текст)
|
| 113 |
+
gr.update(value=texts["sum_button"]), # Кнопка суммаризации (текст)
|
| 114 |
+
gr.update(label=texts["sum_output"]), # Вывод суммаризации (текст)
|
| 115 |
+
# --- Обновление компонентов для Q&A ---
|
| 116 |
+
gr.update(value="### " + texts["qa_section"]), # Заголовок секции Q&A
|
| 117 |
+
gr.update(label=texts["qa_tab_file"]), # Название вкладки Q&A (файл)
|
| 118 |
+
gr.update(label=texts["qa_tab_text"]), # Название вкладки Q&A (текст)
|
| 119 |
+
gr.update(label=texts["qa_file_label"]), # Метка компонента File (Q&A, файл)
|
| 120 |
+
gr.update(label=texts["qa_question_label"]), # Метка поля вопроса (Q&A, файл)
|
| 121 |
+
gr.update(value=texts["qa_answer_button"]), # Кнопка получения ответа (Q&A, файл)
|
| 122 |
+
gr.update(label=texts["qa_answer_label"]), # Вывод ответа (Q&A, файл)
|
| 123 |
+
gr.update(label=texts["qa_text_label"]), # Метка компонента Text (Q&A, текст)
|
| 124 |
+
gr.update(label=texts["qa_question_label"]), # Метка поля вопроса (Q&A, текст)
|
| 125 |
+
gr.update(value=texts["qa_answer_button"]), # Кнопка получения ответа (Q&A, текст)
|
| 126 |
+
gr.update(label=texts["qa_answer_label"]) # Вывод ответа (Q&A, текст)
|
| 127 |
)
|
| 128 |
|
| 129 |
# -------------------------------
|
| 130 |
+
# Интерфейс Gradio с улучшенным дизайном, выбором языка и вкладками для ввода
|
| 131 |
# -------------------------------
|
| 132 |
with gr.Blocks(css="""
|
| 133 |
body { background-color: #f0f2f5; }
|
|
|
|
| 137 |
""") as demo:
|
| 138 |
gr.HTML("<style>body { background-color: #f0f2f5; } .gradio-container { border-radius: 10px; box-shadow: 0 0 15px rgba(0,0,0,0.1); padding: 20px; background-color: white; } h1, h2, h3 { color: #333; } .gr-button { background-color: #4CAF50; color: white; }</style>")
|
| 139 |
|
| 140 |
+
# Контейнер выбора языка (первоначально показывается)
|
| 141 |
with gr.Column(visible=True) as lang_container:
|
| 142 |
lang_radio = gr.Radio(choices=["English", "Русский"], label="Select Language / Выберите язык", value="English")
|
| 143 |
start_button = gr.Button("Start / Начать")
|
|
|
|
| 146 |
with gr.Column(visible=False) as main_container:
|
| 147 |
header_markdown = gr.Markdown("Placeholder Title")
|
| 148 |
with gr.Row():
|
| 149 |
+
# Левая колонка – Суммаризация с двумя вкладками
|
| 150 |
with gr.Column():
|
| 151 |
sum_section_md = gr.Markdown("Placeholder Summarization Section")
|
| 152 |
+
with gr.Tabs() as sum_tabs:
|
| 153 |
+
with gr.Tab("Placeholder Tab 1") as sum_file_tab:
|
| 154 |
+
file_input_sum = gr.File(label="Placeholder File Input", file_count="single", type="filepath")
|
| 155 |
+
summarize_button_file = gr.Button("Placeholder Summarize Button")
|
| 156 |
+
summary_output_file = gr.Textbox(label="Placeholder Summarization Output", lines=10)
|
| 157 |
+
with gr.Tab("Placeholder Tab 2") as sum_text_tab:
|
| 158 |
+
text_input_sum = gr.Textbox(label="Placeholder Text Input", lines=10, placeholder="Enter document text here")
|
| 159 |
+
summarize_button_text = gr.Button("Placeholder Summarize Button")
|
| 160 |
+
summary_output_text = gr.Textbox(label="Placeholder Summarization Output", lines=10)
|
| 161 |
+
# Правая колонка – Q&A с двумя вкладками
|
| 162 |
with gr.Column():
|
| 163 |
qa_section_md = gr.Markdown("Placeholder Q&A Section")
|
| 164 |
+
with gr.Tabs() as qa_tabs:
|
| 165 |
with gr.Tab("Placeholder Tab 1") as file_tab:
|
| 166 |
file_input_qa = gr.File(label="Placeholder QA File Input", file_count="single", type="filepath")
|
| 167 |
question_input_file = gr.Textbox(label="Placeholder QA Question", placeholder="Your question here")
|
|
|
|
| 173 |
answer_button_text = gr.Button("Placeholder QA Answer Button")
|
| 174 |
answer_output_text = gr.Textbox(label="Placeholder QA Answer Output", lines=5)
|
| 175 |
|
| 176 |
+
# При выборе языка – обновляем все метки и показываем основной интерфейс
|
| 177 |
start_button.click(
|
| 178 |
set_language,
|
| 179 |
inputs=[lang_radio],
|
| 180 |
outputs=[
|
| 181 |
lang_container, main_container,
|
| 182 |
+
header_markdown,
|
| 183 |
+
# Компоненты для суммаризации (файл)
|
| 184 |
+
sum_section_md,
|
| 185 |
+
sum_file_tab, file_input_sum, summarize_button_file, summary_output_file,
|
| 186 |
+
# Компоненты для суммаризации (текст)
|
| 187 |
+
sum_text_tab, text_input_sum, summarize_button_text, summary_output_text,
|
| 188 |
+
# Компоненты для Q&A
|
| 189 |
+
qa_section_md,
|
| 190 |
+
file_tab, text_tab, file_input_qa, question_input_file, answer_button_file, answer_output_file,
|
| 191 |
+
context_input, question_input_text, answer_button_text, answer_output_text
|
| 192 |
]
|
| 193 |
)
|
| 194 |
|
| 195 |
# Связываем функционал нейросетей
|
| 196 |
+
# Суммаризация
|
| 197 |
+
summarize_button_file.click(fn=summarize_document, inputs=file_input_sum, outputs=summary_output_file)
|
| 198 |
+
summarize_button_text.click(fn=summarize_text, inputs=text_input_sum, outputs=summary_output_text)
|
| 199 |
+
# Q&A
|
| 200 |
answer_button_file.click(fn=answer_question_file, inputs=[file_input_qa, question_input_file], outputs=answer_output_file)
|
| 201 |
answer_button_text.click(fn=answer_question_text, inputs=[context_input, question_input_text], outputs=answer_output_text)
|
| 202 |
|