Spaces:
Sleeping
Sleeping
Studio
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,6 @@ sum_model = AutoModelForSeq2SeqLM.from_pretrained("LaciaStudio/Lacia_sum_small_v
|
|
| 11 |
def summarize_document(file):
|
| 12 |
if file is None:
|
| 13 |
return "Файл не загружен."
|
| 14 |
-
# Открываем файл и читаем его содержимое
|
| 15 |
with open(file, "r", encoding="utf-8") as f:
|
| 16 |
text = f.read()
|
| 17 |
input_text = "summarize: " + text
|
|
@@ -49,35 +48,121 @@ def answer_question_text(context, question):
|
|
| 49 |
return answer_question(context, question)
|
| 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 |
if __name__ == "__main__":
|
| 83 |
demo.launch()
|
|
|
|
| 11 |
def summarize_document(file):
|
| 12 |
if file is None:
|
| 13 |
return "Файл не загружен."
|
|
|
|
| 14 |
with open(file, "r", encoding="utf-8") as f:
|
| 15 |
text = f.read()
|
| 16 |
input_text = "summarize: " + text
|
|
|
|
| 48 |
return answer_question(context, question)
|
| 49 |
|
| 50 |
# -------------------------------
|
| 51 |
+
# Функция установки языка и обновления интерфейса
|
| 52 |
# -------------------------------
|
| 53 |
+
def set_language(lang):
|
| 54 |
+
if lang == "English":
|
| 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 |
+
gr.update(value="### " + texts["sum_section"]), # секция с��ммаризации
|
| 91 |
+
gr.update(label=texts["sum_file_label"]), # метка загрузки файла для суммаризации
|
| 92 |
+
gr.update(value=texts["sum_button"]), # текст кнопки суммаризации
|
| 93 |
+
gr.update(label=texts["sum_output"]), # метка поля суммаризации
|
| 94 |
+
gr.update(value="### " + texts["qa_section"]), # секция Q&A
|
| 95 |
+
gr.update(label=texts["qa_tab_file"]), # название вкладки «Загрузить файл»
|
| 96 |
+
gr.update(label=texts["qa_tab_text"]), # название вкладки «Ввести текст»
|
| 97 |
+
gr.update(label=texts["qa_file_label"]), # метка загрузки файла для Q&A
|
| 98 |
+
gr.update(label=texts["qa_question_label"]), # метка поля ввода вопроса (файл)
|
| 99 |
+
gr.update(value=texts["qa_answer_button"]), # текст кнопки ответа (файл)
|
| 100 |
+
gr.update(label=texts["qa_answer_label"]), # метка поля с ответом (файл)
|
| 101 |
+
gr.update(label=texts["qa_text_label"]), # метка поля ввода текста для Q&A
|
| 102 |
+
gr.update(label=texts["qa_question_label"]), # метка поля ввода вопроса (текст)
|
| 103 |
+
gr.update(value=texts["qa_answer_button"]), # текст кнопки ответа (текст)
|
| 104 |
+
gr.update(label=texts["qa_answer_label"]) # метка поля с ответом (текст)
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
# -------------------------------
|
| 108 |
+
# Интерфейс Gradio с улучшенным дизайном и выбором языка
|
| 109 |
+
# -------------------------------
|
| 110 |
+
with gr.Blocks(css="""
|
| 111 |
+
body { background-color: #f0f2f5; }
|
| 112 |
+
.gradio-container { border-radius: 10px; box-shadow: 0 0 15px rgba(0,0,0,0.1); padding: 20px; background-color: white; }
|
| 113 |
+
h1, h2, h3 { color: #333; }
|
| 114 |
+
.gr-button { background-color: #4CAF50; color: white; }
|
| 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 / Начать")
|
| 122 |
+
|
| 123 |
+
# Основной интерфейс (изначально скрыт)
|
| 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 |
+
file_input_sum = gr.File(label="Placeholder File Input", file_count="single", type="filepath")
|
| 131 |
+
summarize_button = gr.Button("Placeholder Summarize Button")
|
| 132 |
+
summary_output = gr.Textbox(label="Placeholder Summarization Output", lines=10)
|
| 133 |
+
# Правая колонка – Q&A
|
| 134 |
+
with gr.Column():
|
| 135 |
+
qa_section_md = gr.Markdown("Placeholder Q&A Section")
|
| 136 |
+
with gr.Tabs() as tabs:
|
| 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")
|
| 140 |
+
answer_button_file = gr.Button("Placeholder QA Answer Button")
|
| 141 |
+
answer_output_file = gr.Textbox(label="Placeholder QA Answer Output", lines=5)
|
| 142 |
+
with gr.Tab("Placeholder Tab 2") as text_tab:
|
| 143 |
+
context_input = gr.Textbox(label="Placeholder Context Input", lines=10, placeholder="Document text here")
|
| 144 |
+
question_input_text = gr.Textbox(label="Placeholder QA Question", placeholder="Your question here")
|
| 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, sum_section_md, file_input_sum,
|
| 155 |
+
summarize_button, summary_output, qa_section_md,
|
| 156 |
+
file_tab, text_tab, file_input_qa, question_input_file,
|
| 157 |
+
answer_button_file, answer_output_file, context_input,
|
| 158 |
+
question_input_text, answer_button_text, answer_output_text
|
| 159 |
+
]
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
# Связываем функционал нейросетей
|
| 163 |
+
summarize_button.click(fn=summarize_document, inputs=file_input_sum, outputs=summary_output)
|
| 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 |
+
|
| 167 |
if __name__ == "__main__":
|
| 168 |
demo.launch()
|