Spaces:
Sleeping
Sleeping
Commit
·
c9585ee
1
Parent(s):
35473e1
v.1.55
Browse files
app.py
CHANGED
@@ -230,24 +230,23 @@ class EventDetector:
|
|
230 |
return "Neutral"
|
231 |
|
232 |
def estimate_impact(self, text, entity):
|
233 |
-
"""Estimate impact using Groq
|
234 |
try:
|
235 |
if not self.groq:
|
236 |
return "Неопределенный эффект", "Groq API недоступен"
|
237 |
|
238 |
template = """
|
239 |
-
You are a financial analyst. Analyze this news about {entity}
|
240 |
|
241 |
News: {news}
|
242 |
|
243 |
-
|
244 |
-
1. "Значительный риск убытков"
|
245 |
-
2. "Умеренный риск убытков"
|
246 |
-
3. "Незначительный риск убытков"
|
247 |
-
4. "
|
248 |
-
5. "Неопределенный эффект" (Uncertain effect)
|
249 |
|
250 |
-
Format
|
251 |
Impact: [category]
|
252 |
Reasoning: [explanation in 2-3 sentences]
|
253 |
"""
|
@@ -267,6 +266,15 @@ class EventDetector:
|
|
267 |
parts = response_text.split("Reasoning:")
|
268 |
impact = parts[0].split("Impact:")[1].strip()
|
269 |
reasoning = parts[1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
else:
|
271 |
impact = "Неопределенный эффект"
|
272 |
reasoning = "Не удалось определить влияние"
|
@@ -279,32 +287,28 @@ class EventDetector:
|
|
279 |
|
280 |
@spaces.GPU(duration=60)
|
281 |
def process_text(self, text, entity):
|
282 |
-
"""Process text
|
283 |
try:
|
284 |
-
# Translate text
|
285 |
translated_text = self._translate_text(text)
|
|
|
286 |
|
287 |
-
# Analyze sentiment
|
288 |
-
sentiment = self.analyze_sentiment(translated_text)
|
289 |
-
|
290 |
-
# Initialize impact and reasoning
|
291 |
impact = "Неопределенный эффект"
|
292 |
reasoning = ""
|
293 |
|
294 |
-
#
|
295 |
-
|
296 |
-
|
297 |
-
impact, reasoning = self.estimate_impact(translated_text, entity)
|
298 |
-
reasoning = self.rutranslator(reasoning)[0]['translation_text']
|
299 |
|
300 |
-
#
|
301 |
-
|
302 |
-
|
303 |
-
|
|
|
|
|
304 |
|
305 |
return {
|
306 |
'translated_text': translated_text,
|
307 |
-
'sentiment':
|
308 |
'impact': impact,
|
309 |
'reasoning': reasoning,
|
310 |
'event_type': event_type,
|
@@ -316,7 +320,7 @@ class EventDetector:
|
|
316 |
return {
|
317 |
'translated_text': '',
|
318 |
'sentiment': 'Neutral',
|
319 |
-
'impact': 'Неопределенный эффект',
|
320 |
'reasoning': f'Ошибка обработки: {str(e)}',
|
321 |
'event_type': 'Нет',
|
322 |
'event_summary': ''
|
@@ -653,7 +657,7 @@ def create_interface():
|
|
653 |
# Create state for file data
|
654 |
current_file = gr.State(None)
|
655 |
|
656 |
-
gr.Markdown("# AI-анализ мониторинга новостей v.1.
|
657 |
|
658 |
with gr.Row():
|
659 |
file_input = gr.File(
|
|
|
230 |
return "Neutral"
|
231 |
|
232 |
def estimate_impact(self, text, entity):
|
233 |
+
"""Estimate impact using Groq ensuring sentiment-impact alignment"""
|
234 |
try:
|
235 |
if not self.groq:
|
236 |
return "Неопределенный эффект", "Groq API недоступен"
|
237 |
|
238 |
template = """
|
239 |
+
You are a financial risk analyst. Analyze this negative news about {entity}:
|
240 |
|
241 |
News: {news}
|
242 |
|
243 |
+
Based on the severity of negative implications, classify the impact as:
|
244 |
+
1. "Значительный риск убытков" - for severe negative events with high financial impact
|
245 |
+
2. "Умеренный риск убытков" - for moderate negative events with medium financial impact
|
246 |
+
3. "Незначительный риск убытков" - for minor negative events with low financial impact
|
247 |
+
4. "Неопределенный эффект" - if impact cannot be reliably assessed
|
|
|
248 |
|
249 |
+
Format response exactly as:
|
250 |
Impact: [category]
|
251 |
Reasoning: [explanation in 2-3 sentences]
|
252 |
"""
|
|
|
266 |
parts = response_text.split("Reasoning:")
|
267 |
impact = parts[0].split("Impact:")[1].strip()
|
268 |
reasoning = parts[1].strip()
|
269 |
+
|
270 |
+
# Validate impact category
|
271 |
+
valid_impacts = {
|
272 |
+
"Значительный риск убытков",
|
273 |
+
"Умеренный риск убытков",
|
274 |
+
"Незначительный риск убытков",
|
275 |
+
"Неопределенный эффект"
|
276 |
+
}
|
277 |
+
impact = impact if impact in valid_impacts else "Неопределенный эффект"
|
278 |
else:
|
279 |
impact = "Неопределенный эффект"
|
280 |
reasoning = "Не удалось определить влияние"
|
|
|
287 |
|
288 |
@spaces.GPU(duration=60)
|
289 |
def process_text(self, text, entity):
|
290 |
+
"""Process text with Groq-driven sentiment analysis"""
|
291 |
try:
|
|
|
292 |
translated_text = self._translate_text(text)
|
293 |
+
initial_sentiment = self.analyze_sentiment(translated_text)
|
294 |
|
|
|
|
|
|
|
|
|
295 |
impact = "Неопределенный эффект"
|
296 |
reasoning = ""
|
297 |
|
298 |
+
# Always get Groq analysis for all texts
|
299 |
+
impact, reasoning = self.estimate_impact(translated_text, entity)
|
300 |
+
reasoning = self.rutranslator(reasoning)[0]['translation_text']
|
|
|
|
|
301 |
|
302 |
+
# Override sentiment based on Groq impact
|
303 |
+
final_sentiment = initial_sentiment
|
304 |
+
if impact == "Вероятность прибыли":
|
305 |
+
final_sentiment = "Positive"
|
306 |
+
|
307 |
+
event_type, event_summary = self.detect_events(text, entity)
|
308 |
|
309 |
return {
|
310 |
'translated_text': translated_text,
|
311 |
+
'sentiment': final_sentiment,
|
312 |
'impact': impact,
|
313 |
'reasoning': reasoning,
|
314 |
'event_type': event_type,
|
|
|
320 |
return {
|
321 |
'translated_text': '',
|
322 |
'sentiment': 'Neutral',
|
323 |
+
'impact': 'Неопределенный эффект',
|
324 |
'reasoning': f'Ошибка обработки: {str(e)}',
|
325 |
'event_type': 'Нет',
|
326 |
'event_summary': ''
|
|
|
657 |
# Create state for file data
|
658 |
current_file = gr.State(None)
|
659 |
|
660 |
+
gr.Markdown("# AI-анализ мониторинга новостей v.1.55")
|
661 |
|
662 |
with gr.Row():
|
663 |
file_input = gr.File(
|