Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import mimetypes
|
3 |
+
import pdfminer.high_level
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
7 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
8 |
+
|
9 |
+
def load_content(file, text):
|
10 |
+
if text.strip():
|
11 |
+
return text
|
12 |
+
if file is None:
|
13 |
+
return ""
|
14 |
+
mime = mimetypes.guess_type(file.name)[0]
|
15 |
+
binary = file.read()
|
16 |
+
if mime and "pdf" in mime:
|
17 |
+
return pdfminer.high_level.extract_text(file)
|
18 |
+
return binary.decode("utf-8", errors="ignore")
|
19 |
+
|
20 |
+
def detect_sentiment(file, text):
|
21 |
+
content = load_content(file, text).strip()
|
22 |
+
if not content:
|
23 |
+
return "Нет текста"
|
24 |
+
return f"Тональность: {classifier(content)[0]['label']}"
|
25 |
+
|
26 |
+
def summarize_text(file, text):
|
27 |
+
content = load_content(file, text).strip()
|
28 |
+
if not content:
|
29 |
+
return "Нет текста"
|
30 |
+
return summarizer(content, max_length=65, min_length=25, do_sample=False)[0]['summary_text']
|
31 |
+
|
32 |
+
def full_analysis(file, text):
|
33 |
+
content = load_content(file, text).strip()
|
34 |
+
if not content:
|
35 |
+
return "Нет текста", "Нет текста"
|
36 |
+
sentiment = f"Тональность: {classifier(content)[0]['label']}"
|
37 |
+
summary = summarizer(content, max_length=65, min_length=25, do_sample=False)[0]['summary_text']
|
38 |
+
return sentiment, summary
|
39 |
+
|
40 |
+
def reset_fields():
|
41 |
+
return "", None, "", ""
|
42 |
+
|
43 |
+
with gr.Blocks() as demo:
|
44 |
+
gr.Markdown("## ReviewSmart — анализ отзывов с помощью NLP")
|
45 |
+
|
46 |
+
with gr.Row():
|
47 |
+
input_text = gr.Textbox(label="Текст отзыва", lines=8, placeholder="Введите или загрузите отзыв...")
|
48 |
+
input_file = gr.File(label="Файл (.pdf, .txt)", file_types=[".pdf", ".txt"])
|
49 |
+
|
50 |
+
with gr.Row():
|
51 |
+
btn_sentiment = gr.Button("Определить тональность")
|
52 |
+
btn_summary = gr.Button("Создать резюме")
|
53 |
+
btn_both = gr.Button("Анализировать оба")
|
54 |
+
btn_clear = gr.Button("Очистить")
|
55 |
+
|
56 |
+
with gr.Row():
|
57 |
+
sentiment_box = gr.Textbox(label="Результат анализа тональности", lines=2)
|
58 |
+
summary_box = gr.Textbox(label="Результат резюмирования", lines=4)
|
59 |
+
|
60 |
+
btn_sentiment.click(fn=detect_sentiment, inputs=[input_file, input_text], outputs=sentiment_box)
|
61 |
+
btn_summary.click(fn=summarize_text, inputs=[input_file, input_text], outputs=summary_box)
|
62 |
+
btn_both.click(fn=full_analysis, inputs=[input_file, input_text], outputs=[sentiment_box, summary_box])
|
63 |
+
btn_clear.click(fn=reset_fields, outputs=[input_text, input_file, sentiment_box, summary_box])
|
64 |
+
|
65 |
+
demo.launch()
|