File size: 13,457 Bytes
7e14e6f
 
 
 
1254c79
7e14e6f
 
03eddb7
4673e91
03eddb7
261f952
367d42f
1075b3f
03eddb7
 
 
 
 
 
 
 
 
 
 
 
7e14e6f
03eddb7
 
 
 
 
 
 
45f1473
03eddb7
 
 
 
 
 
 
 
 
 
fe6b622
aebf0a2
 
1254c79
 
aebf0a2
1254c79
 
 
 
 
 
 
 
 
 
fe6b622
1254c79
aebf0a2
1254c79
 
aebf0a2
1254c79
 
aebf0a2
 
 
 
 
 
1254c79
 
aebf0a2
 
 
1254c79
aebf0a2
 
 
 
 
 
 
1254c79
aebf0a2
 
 
 
1254c79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aebf0a2
 
 
 
9e97a7c
 
 
 
 
 
 
 
 
 
03eddb7
9e97a7c
 
 
 
03eddb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e97a7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d683d7
 
 
 
 
aebf0a2
 
 
0d683d7
 
 
9e97a7c
0d683d7
aebf0a2
 
 
 
 
 
 
 
 
 
0d683d7
 
 
aebf0a2
 
 
 
 
 
 
 
0d683d7
 
 
3cedc42
 
 
0d683d7
 
9e97a7c
03eddb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc222e3
aebf0a2
9e97a7c
 
aebf0a2
 
 
bc222e3
 
 
 
 
aebf0a2
bc222e3
 
9e97a7c
 
bc222e3
9e97a7c
 
 
 
 
bc222e3
 
 
 
 
 
 
 
 
9e97a7c
 
 
 
 
 
bc222e3
9e97a7c
bc222e3
9e97a7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4673e91
7e14e6f
bc222e3
 
 
 
 
03eddb7
 
 
 
 
bc222e3
 
 
 
 
 
 
 
1254c79
45f1473
 
 
7e14e6f
c289d70
7e14e6f
45f1473
c58ea62
 
45f1473
7e14e6f
2d683e0
03eddb7
 
 
0d683d7
d7392b8
0d683d7
45f1473
03eddb7
c58ea62
 
 
261f952
0d683d7
d7392b8
7e14e6f
03eddb7
7e14e6f
03eddb7
45f1473
 
261f952
7e14e6f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import streamlit as st
import pandas as pd
import time
import matplotlib.pyplot as plt
from openpyxl.utils.dataframe import dataframe_to_rows
import io
from rapidfuzz import fuzz
import os
from openpyxl import load_workbook
from langchain_community.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain_core.runnables import RunnablePassthrough

def fuzzy_deduplicate(df, column, threshold=65):
    seen_texts = []
    indices_to_keep = []
    for i, text in enumerate(df[column]):
        if pd.isna(text):
            indices_to_keep.append(i)
            continue
        text = str(text)
        if not seen_texts or all(fuzz.ratio(text, seen) < threshold for seen in seen_texts):
            seen_texts.append(text)
            indices_to_keep.append(i)
    return df.iloc[indices_to_keep]

def init_langchain_llm():
    try:
        if 'groq_key' in st.secrets:
            groq_api_key = st.secrets['groq_key']
        else:
            st.error("Groq API key not found in Hugging Face secrets. Please add it with the key 'groq_key'.")
            st.stop()

        llm = ChatOpenAI(
            base_url="https://api.groq.com/openai/v1",
            model="llama-3.1-70b-versatile",
            api_key=groq_api_key,
            temperature=0.0
        )
        return llm
    except Exception as e:
        st.error(f"Error initializing the Groq LLM: {str(e)}")
        st.stop()

def estimate_sentiment_and_impact(llm, news_text, entity):
    template = """
    First, translate this Russian text into English:
    "{news}"
    
    Then, analyze the translated text about the entity "{entity}" and determine:
    1. Sentiment (Positive/Negative/Neutral)
    2. Estimate potential financial impact in Russian rubles for this entity in the next 6 months.
    
    If precise monetary estimate is not possible, categorize the impact as one of the following:
    1. "Significant risk of loss" 
    2. "Moderate risk of loss"
    3. "Minor risk of loss"
    4. "Probability of profit"
    5. "Uncertain effect"

    Provide a brief reasoning (maximum 100 words).

    Your response should be in the following format:
    Translation: [Your English translation]
    Sentiment: [Positive/Negative/Neutral]
    Impact: [Your estimate or category]
    Reasoning: [Your reasoning]
    """
    prompt = PromptTemplate(template=template, input_variables=["entity", "news"])
    chain = prompt | llm | RunnablePassthrough()
    response = chain.invoke({"entity": entity, "news": news_text})
    
    sentiment = "Neutral"
    impact = "Uncertain effect"
    reasoning = "Unable to provide reasoning"
    
    if isinstance(response, str):
        try:
            # Extract sentiment
            if "Sentiment:" in response:
                sentiment_part = response.split("Sentiment:")[1].split("\n")[0].strip().lower()
                if "positive" in sentiment_part:
                    sentiment = "Positive"
                elif "negative" in sentiment_part:
                    sentiment = "Negative"
            
            # Extract impact and reasoning
            if "Impact:" in response and "Reasoning:" in response:
                impact_part, reasoning_part = response.split("Reasoning:")
                impact = impact_part.split("Impact:")[1].strip()
                reasoning = reasoning_part.strip()

                # Translate impact categories back to Russian
                impact_mapping = {
                    "Significant risk of loss": "Значительный риск убытков",
                    "Moderate risk of loss": "Умеренный риск убытков",
                    "Minor risk of loss": "Незначительный риск убытков",
                    "Probability of profit": "Вероятность прибыли",
                    "Uncertain effect": "Неопределенный эффект"
                }
                
                for eng, rus in impact_mapping.items():
                    if eng.lower() in impact.lower():
                        impact = rus
                        break
                
        except Exception as e:
            st.error(f"Error parsing LLM response: {str(e)}")
    
    return sentiment, impact, reasoning

def format_elapsed_time(seconds):
    hours, remainder = divmod(int(seconds), 3600)
    minutes, seconds = divmod(remainder, 60)
    
    time_parts = []
    if hours > 0:
        time_parts.append(f"{hours} час{'ов' if hours != 1 else ''}")
    if minutes > 0:
        time_parts.append(f"{minutes} минут{'' if minutes == 1 else 'ы' if 2 <= minutes <= 4 else ''}")
    if seconds > 0 or not time_parts:
        time_parts.append(f"{seconds} секунд{'а' if seconds == 1 else 'ы' if 2 <= seconds <= 4 else ''}")
    
    return " ".join(time_parts)

def generate_sentiment_visualization(df):
    negative_df = df[df['Sentiment'] == 'Negative']
    
    if negative_df.empty:
        st.warning("Не обнаружено негативных упоминаний. Отображаем общую статистику по объектам.")
        entity_counts = df['Объект'].value_counts()
    else:
        entity_counts = negative_df['Объект'].value_counts()
    
    if len(entity_counts) == 0:
        st.warning("Нет данных для визуализации.")
        return None
    
    fig, ax = plt.subplots(figsize=(12, max(6, len(entity_counts) * 0.5)))
    entity_counts.plot(kind='barh', ax=ax)
    ax.set_title('Количество негативных упоминаний по объектам')
    ax.set_xlabel('Количество упоминаний')
    plt.tight_layout()
    return fig

def process_file(uploaded_file):
    df = pd.read_excel(uploaded_file, sheet_name='Публикации')
    
    required_columns = ['Объект', 'Заголовок', 'Выдержки из текста']
    missing_columns = [col for col in required_columns if col not in df.columns]
    if missing_columns:
        st.error(f"Error: The following required columns are missing from the input file: {', '.join(missing_columns)}")
        st.stop()
    
    original_news_count = len(df)
    df = df.groupby('Объект').apply(
        lambda x: fuzzy_deduplicate(x, 'Выдержки из текста', 65)
    ).reset_index(drop=True)

    remaining_news_count = len(df)
    duplicates_removed = original_news_count - remaining_news_count
    st.write(f"Из {original_news_count} новостных сообщений удалены {duplicates_removed} дублирующих. Осталось {remaining_news_count}.")

    llm = init_langchain_llm()
    if not llm:
        st.error("Не удалось инициализировать нейросеть. Пожалуйста, проверьте настройки и попробуйте снова.")
        st.stop()

    df['Sentiment'] = ''
    df['Impact'] = ''
    df['Reasoning'] = ''
    
    progress_bar = st.progress(0)
    status_text = st.empty()
    
    for index, row in df.iterrows():
        sentiment, impact, reasoning = estimate_sentiment_and_impact(
            llm, 
            row['Выдержки из текста'], 
            row['Объект']
        )
        
        df.at[index, 'Sentiment'] = sentiment
        df.at[index, 'Impact'] = impact
        df.at[index, 'Reasoning'] = reasoning
        
        progress = (index + 1) / len(df)
        progress_bar.progress(progress)
        status_text.text(f"Проанализировано {index + 1} из {len(df)} новостей")
        
        st.write(f"Объект: {row['Объект']}")
        st.write(f"Новость: {row['Заголовок']}")
        st.write(f"Тональность: {sentiment}")
        st.write(f"Эффект: {impact}")
        st.write(f"Обоснование: {reasoning}")
        st.write("---")

    progress_bar.empty()
    status_text.empty()

    visualization = generate_sentiment_visualization(df)
    if visualization:
        st.pyplot(visualization)

    return df

def create_analysis_data(df):
    analysis_data = []
    for _, row in df.iterrows():
        if row['Sentiment'] == 'Negative':
            analysis_data.append([
                row['Объект'], 
                row['Заголовок'], 
                'РИСК УБЫТКА', 
                row['Impact'],
                row['Reasoning'],
                row['Выдержки из текста']
            ])
    return pd.DataFrame(analysis_data, columns=[
        'Объект', 
        'Заголовок', 
        'Признак', 
        'Оценка влияния',
        'Обоснование',
        'Текст сообщения'
    ])

def create_output_file(df, uploaded_file):
    wb = load_workbook("sample_file.xlsx")
    
    summary_df = pd.DataFrame({
        'Объект': df['Объект'].unique(),
        'Всего новостей': df.groupby('Объект').size(),
        'Негативные': df[df['Sentiment'] == 'Negative'].groupby('Объект').size().fillna(0).astype(int),
        'Позитивные': df[df['Sentiment'] == 'Positive'].groupby('Объект').size().fillna(0).astype(int),
        'Преобладающий эффект': df.groupby('Объект')['Impact'].agg(
            lambda x: x.value_counts().index[0] if len(x) > 0 else 'Неопределенный'
        )
    })
    
    summary_df = summary_df.sort_values('Негативные', ascending=False)
    
    ws = wb['Сводка']
    for r_idx, row in enumerate(dataframe_to_rows(summary_df, index=False, header=True), start=4):
        for c_idx, value in enumerate(row, start=5):
            ws.cell(row=r_idx, column=c_idx, value=value)
    
    significant_data = []
    for _, row in df.iterrows():
        if row['Sentiment'] in ['Negative', 'Positive']:
            significant_data.append([
                row['Объект'],
                'релевантен',
                row['Sentiment'],
                row['Impact'],
                row['Заголовок'],
                row['Выдержки из текста']
            ])
    
    ws = wb['Значимые']
    for r_idx, row in enumerate(significant_data, start=3):
        for c_idx, value in enumerate(row, start=3):
            ws.cell(row=r_idx, column=c_idx, value=value)
    
    analysis_df = create_analysis_data(df)
    ws = wb['Анализ']
    for r_idx, row in enumerate(dataframe_to_rows(analysis_df, index=False, header=True), start=4):
        for c_idx, value in enumerate(row, start=5):
            ws.cell(row=r_idx, column=c_idx, value=value)
    
    original_df = pd.read_excel(uploaded_file, sheet_name='Публикации')
    ws = wb['Публикации']
    for r_idx, row in enumerate(dataframe_to_rows(original_df, index=False, header=True), start=1):
        for c_idx, value in enumerate(row, start=1):
            ws.cell(row=r_idx, column=c_idx, value=value)
    
    if 'Тех.приложение' not in wb.sheetnames:
        wb.create_sheet('Тех.приложение')
    ws = wb['Тех.приложение']
    for r_idx, row in enumerate(dataframe_to_rows(df, index=False, header=True), start=1):
        for c_idx, value in enumerate(row, start=1):
            ws.cell(row=r_idx, column=c_idx, value=value)
    
    output = io.BytesIO()
    wb.save(output)
    output.seek(0)
    return output

def main():
    st.markdown(
        """
        <style>
        .signature {
            position: fixed;
            right: 12px;
            bottom: 12px;
            font-size: 14px;
            color: #FF0000;
            opacity: 0.9;
            z-index: 999;
        }
        </style>
        <div class="signature">denis.pokrovsky.npff</div>
        """,
        unsafe_allow_html=True
    )
    
    st.title("::: анализ мониторинга новостей СКАН-ИНТЕРФАКС :::")
    
    if 'processed_df' not in st.session_state:
        st.session_state.processed_df = None
    
    uploaded_file = st.file_uploader("Выбирайте Excel-файл", type="xlsx")
    
    if uploaded_file is not None and st.session_state.processed_df is None:
        start_time = time.time()
        
        st.session_state.processed_df = process_file(uploaded_file)
        
        st.subheader("Предпросмотр данных")
        preview_df = st.session_state.processed_df[['Объект', 'Заголовок', 'Sentiment', 'Impact']].head()
        st.dataframe(preview_df)
        
        analysis_df = create_analysis_data(st.session_state.processed_df)
        st.subheader("Анализ")
        st.dataframe(analysis_df)
        
        output = create_output_file(st.session_state.processed_df, uploaded_file)
        
        end_time = time.time()
        elapsed_time = end_time - start_time
        formatted_time = format_elapsed_time(elapsed_time)
        st.success(f"Обработка и анализ завершены за {formatted_time}.")

        st.download_button(
            label="Скачать результат анализа",
            data=output,
            file_name="результат_анализа.xlsx",
            mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        )

if __name__ == "__main__":
    main()