pentarosarium commited on
Commit
85ab85d
·
1 Parent(s): 118a5c5
Files changed (1) hide show
  1. app.py +42 -42
app.py CHANGED
@@ -88,42 +88,6 @@ class EventDetector:
88
  return "Negative"
89
  return "Neutral"
90
 
91
- def process_file(file):
92
- try:
93
- df = pd.read_excel(file, sheet_name='Публикации')
94
- detector = EventDetector()
95
- processed_rows = []
96
- total = len(df)
97
-
98
- for idx, row in df.iterrows():
99
- text = str(row.get('Выдержки из текста', ''))
100
- entity = str(row.get('Объект', ''))
101
-
102
- event_type, event_summary = detector.detect_events(text, entity)
103
- sentiment = detector.analyze_sentiment(text)
104
-
105
- processed_rows.append({
106
- 'Объект': entity,
107
- 'Заголовок': str(row.get('Заголовок', '')),
108
- 'Sentiment': sentiment,
109
- 'Event_Type': event_type,
110
- 'Event_Summary': event_summary,
111
- 'Текст': text
112
- })
113
-
114
- # Log progress to console instead of updating UI directly
115
- if idx % 10 == 0:
116
- logger.info(f"Processed {idx}/{total} rows")
117
-
118
- result_df = pd.DataFrame(processed_rows)
119
- logger.info("File processing complete!")
120
- return result_df
121
-
122
- except Exception as e:
123
- logger.error(f"File processing error: {e}")
124
- gr.Error(f"Error processing file: {str(e)}")
125
- return pd.DataFrame(columns=['Объект', 'Заголовок', 'Sentiment', 'Event_Type', 'Event_Summary', 'Текст'])
126
-
127
  def create_visualizations(df):
128
  if df is None or df.empty:
129
  return None, None
@@ -153,7 +117,7 @@ def create_visualizations(df):
153
 
154
  def create_interface():
155
  with gr.Blocks(theme=gr.themes.Soft()) as app:
156
- gr.Markdown("# AI-анализ мониторинга новостей v.1.06")
157
 
158
  with gr.Row():
159
  file_input = gr.File(
@@ -188,14 +152,14 @@ def create_interface():
188
  with gr.Column():
189
  events_plot = gr.Plot(label="Распределение событий")
190
 
191
- def analyze(file):
192
- if file is None:
193
  gr.Warning("Пожалуйста, загрузите файл")
194
  return None, None, None, "Ожидание файла..."
195
  try:
196
- # Instead of updating progress directly, we return new values
197
- temp_file = file.name
198
- df = process_file(temp_file)
199
 
200
  if df.empty:
201
  return None, None, None, "Нет данных для обработки"
@@ -216,6 +180,42 @@ def create_interface():
216
 
217
  return app
218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
  if __name__ == "__main__":
220
  app = create_interface()
221
  app.launch(share=True)
 
88
  return "Negative"
89
  return "Neutral"
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  def create_visualizations(df):
92
  if df is None or df.empty:
93
  return None, None
 
117
 
118
  def create_interface():
119
  with gr.Blocks(theme=gr.themes.Soft()) as app:
120
+ gr.Markdown("# AI-анализ мониторинга новостей v.1.08")
121
 
122
  with gr.Row():
123
  file_input = gr.File(
 
152
  with gr.Column():
153
  events_plot = gr.Plot(label="Распределение событий")
154
 
155
+ def analyze(file_bytes):
156
+ if file_bytes is None:
157
  gr.Warning("Пожалуйста, загрузите файл")
158
  return None, None, None, "Ожидание файла..."
159
  try:
160
+ # Convert bytes to BytesIO for pandas to read
161
+ file_obj = io.BytesIO(file_bytes)
162
+ df = process_file(file_obj)
163
 
164
  if df.empty:
165
  return None, None, None, "Нет данных для обработки"
 
180
 
181
  return app
182
 
183
+ def process_file(file_obj):
184
+ try:
185
+ # Read Excel directly from BytesIO object
186
+ df = pd.read_excel(file_obj, sheet_name='Публикации')
187
+ detector = EventDetector()
188
+ processed_rows = []
189
+ total = len(df)
190
+
191
+ for idx, row in df.iterrows():
192
+ text = str(row.get('Выдержки из текста', ''))
193
+ entity = str(row.get('Объект', ''))
194
+
195
+ event_type, event_summary = detector.detect_events(text, entity)
196
+ sentiment = detector.analyze_sentiment(text)
197
+
198
+ processed_rows.append({
199
+ 'Объект': entity,
200
+ 'Заголовок': str(row.get('Заголовок', '')),
201
+ 'Sentiment': sentiment,
202
+ 'Event_Type': event_type,
203
+ 'Event_Summary': event_summary,
204
+ 'Текст': text
205
+ })
206
+
207
+ if idx % 10 == 0:
208
+ logger.info(f"Processed {idx}/{total} rows")
209
+
210
+ result_df = pd.DataFrame(processed_rows)
211
+ logger.info("File processing complete!")
212
+ return result_df
213
+
214
+ except Exception as e:
215
+ logger.error(f"File processing error: {e}")
216
+ gr.Error(f"Error processing file: {str(e)}")
217
+ return pd.DataFrame(columns=['Объект', 'Заголовок', 'Sentiment', 'Event_Type', 'Event_Summary', 'Текст'])
218
+
219
  if __name__ == "__main__":
220
  app = create_interface()
221
  app.launch(share=True)