pentarosarium commited on
Commit
428b349
·
1 Parent(s): cd6115e
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -63,12 +63,12 @@ class ProcessControl:
63
 
64
  class EventDetector:
65
  def __init__(self):
 
66
  try:
67
- # Initialize sentiment models with GPU support
68
  device = "cuda" if torch.cuda.is_available() else "cpu"
69
  logger.info(f"Initializing models on device: {device}")
70
 
71
- # Initialize sentiment models
72
  self.finbert = pipeline(
73
  "sentiment-analysis",
74
  model="ProsusAI/finbert",
@@ -91,7 +91,7 @@ class EventDetector:
91
  max_length=512
92
  )
93
 
94
- # Initialize MT5 model for event detection
95
  self.model_name = "google/mt5-small"
96
  self.tokenizer = AutoTokenizer.from_pretrained(
97
  self.model_name,
@@ -101,13 +101,19 @@ class EventDetector:
101
 
102
  self.device = device
103
  self.initialized = True
104
- logger.info(f"All models initialized successfully on {device}")
105
 
106
  except Exception as e:
107
  logger.error(f"Error in EventDetector initialization: {str(e)}")
108
  raise
109
 
 
 
 
 
 
110
  def analyze_sentiment(self, text):
 
111
  try:
112
  if not text or not isinstance(text, str):
113
  return "Neutral"
@@ -149,6 +155,7 @@ class EventDetector:
149
  return "Neutral"
150
 
151
  def detect_events(self, text, entity):
 
152
  if not text or not entity:
153
  return "Нет", "Invalid input"
154
 
@@ -201,7 +208,7 @@ Summary: [2-3 sentence summary]</s>"""
201
  do_sample=False,
202
  pad_token_id=self.tokenizer.pad_token_id,
203
  eos_token_id=self.tokenizer.eos_token_id,
204
- no_repeat_ngram_size=3 # Prevent repetition
205
  )
206
 
207
  response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
@@ -209,10 +216,8 @@ Summary: [2-3 sentence summary]</s>"""
209
  # Extract summary
210
  if "Summary:" in response:
211
  summary = response.split("Summary:")[1].strip()
212
- # Clean up any remaining prompt artifacts
213
  summary = summary.replace('<s>', '').replace('</s>', '').strip()
214
  else:
215
- # Create a structured summary based on event type
216
  if detected_event == 'Отчетность':
217
  summary = f"Компания {entity} опубликовала финансовые показатели."
218
  elif detected_event == 'РЦБ':
@@ -222,7 +227,6 @@ Summary: [2-3 sentence summary]</s>"""
222
 
223
  return detected_event, summary
224
 
225
- # If no keywords matched
226
  return "Нет", "No significant event detected"
227
 
228
  except Exception as e:
@@ -380,7 +384,7 @@ def create_interface():
380
  control = ProcessControl()
381
 
382
  with gr.Blocks(theme=gr.themes.Soft()) as app:
383
- gr.Markdown("# AI-анализ мониторинга новостей v.1.23")
384
 
385
  with gr.Row():
386
  file_input = gr.File(
 
63
 
64
  class EventDetector:
65
  def __init__(self):
66
+ """Initialize models with GPU support"""
67
  try:
68
+ # Initialize sentiment models
69
  device = "cuda" if torch.cuda.is_available() else "cpu"
70
  logger.info(f"Initializing models on device: {device}")
71
 
 
72
  self.finbert = pipeline(
73
  "sentiment-analysis",
74
  model="ProsusAI/finbert",
 
91
  max_length=512
92
  )
93
 
94
+ # Initialize MT5 model
95
  self.model_name = "google/mt5-small"
96
  self.tokenizer = AutoTokenizer.from_pretrained(
97
  self.model_name,
 
101
 
102
  self.device = device
103
  self.initialized = True
104
+ logger.info("All models initialized successfully")
105
 
106
  except Exception as e:
107
  logger.error(f"Error in EventDetector initialization: {str(e)}")
108
  raise
109
 
110
+ @spaces.GPU(duration=30)
111
+ def initialize_models(self):
112
+ """Keep this method for compatibility, now just returns initialization status"""
113
+ return self.initialized
114
+
115
  def analyze_sentiment(self, text):
116
+ """Rest of the analyze_sentiment method remains the same"""
117
  try:
118
  if not text or not isinstance(text, str):
119
  return "Neutral"
 
155
  return "Neutral"
156
 
157
  def detect_events(self, text, entity):
158
+ """Rest of the detect_events method remains the same"""
159
  if not text or not entity:
160
  return "Нет", "Invalid input"
161
 
 
208
  do_sample=False,
209
  pad_token_id=self.tokenizer.pad_token_id,
210
  eos_token_id=self.tokenizer.eos_token_id,
211
+ no_repeat_ngram_size=3
212
  )
213
 
214
  response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
 
216
  # Extract summary
217
  if "Summary:" in response:
218
  summary = response.split("Summary:")[1].strip()
 
219
  summary = summary.replace('<s>', '').replace('</s>', '').strip()
220
  else:
 
221
  if detected_event == 'Отчетность':
222
  summary = f"Компания {entity} опубликовала финансовые показатели."
223
  elif detected_event == 'РЦБ':
 
227
 
228
  return detected_event, summary
229
 
 
230
  return "Нет", "No significant event detected"
231
 
232
  except Exception as e:
 
384
  control = ProcessControl()
385
 
386
  with gr.Blocks(theme=gr.themes.Soft()) as app:
387
+ gr.Markdown("# AI-анализ мониторинга новостей v.1.24")
388
 
389
  with gr.Row():
390
  file_input = gr.File(