Spaces:
Running
Running
Commit ·
e3e54f2
1
Parent(s): 234ea97
3.48
Browse files
app.py
CHANGED
|
@@ -46,9 +46,27 @@ class FallbackLLMSystem:
|
|
| 46 |
st.error(f"Error initializing MT5: {str(e)}")
|
| 47 |
raise
|
| 48 |
|
| 49 |
-
def
|
| 50 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
inputs = self.tokenizer(
|
| 53 |
prompt,
|
| 54 |
return_tensors="pt",
|
|
@@ -65,35 +83,21 @@ class FallbackLLMSystem:
|
|
| 65 |
pad_token_id=self.tokenizer.pad_token_id
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
except Exception as e:
|
| 70 |
-
st.warning(f"Text generation error: {str(e)}")
|
| 71 |
-
raise
|
| 72 |
-
|
| 73 |
-
def detect_events(self, text, entity):
|
| 74 |
-
"""Detect events using MT5"""
|
| 75 |
-
prompt = f"""Task: Analyze news about company and determine event type.
|
| 76 |
-
Company: {entity}
|
| 77 |
-
News: {text}
|
| 78 |
-
Event types:
|
| 79 |
-
- Отчетность (financial reports)
|
| 80 |
-
- РЦБ (securities market events)
|
| 81 |
-
- Суд (legal actions)
|
| 82 |
-
- Нет (no significant events)
|
| 83 |
-
Format:
|
| 84 |
-
Тип: [event type]
|
| 85 |
-
Краткое описание: [two sentence description]"""
|
| 86 |
-
|
| 87 |
-
try:
|
| 88 |
-
response = self._generate_text(prompt)
|
| 89 |
-
|
| 90 |
-
event_type = "Нет"
|
| 91 |
-
summary = ""
|
| 92 |
|
|
|
|
| 93 |
if "Тип:" in response and "Краткое описание:" in response:
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
return event_type, summary
|
| 99 |
|
|
@@ -103,29 +107,62 @@ class FallbackLLMSystem:
|
|
| 103 |
|
| 104 |
def estimate_impact(self, text, entity):
|
| 105 |
"""Estimate impact using MT5"""
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
Impact categories:
|
| 110 |
-
- Значительный риск убытков
|
| 111 |
-
- Умеренный риск убытков
|
| 112 |
-
- Незначительный риск убытков
|
| 113 |
-
- Вероятность прибыли
|
| 114 |
-
- Неопределенный эффект
|
| 115 |
-
Format:
|
| 116 |
-
Impact: [category]
|
| 117 |
-
Reasoning: [two sentence explanation]"""
|
| 118 |
|
| 119 |
try:
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
if "Impact:" in response and "Reasoning:" in response:
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
return impact, reasoning
|
| 131 |
|
|
@@ -725,7 +762,7 @@ def create_output_file(df, uploaded_file, llm):
|
|
| 725 |
return output
|
| 726 |
def main():
|
| 727 |
with st.sidebar:
|
| 728 |
-
st.title("::: AI-анализ мониторинга новостей (v.3.
|
| 729 |
st.subheader("по материалам СКАН-ИНТЕРФАКС ")
|
| 730 |
|
| 731 |
|
|
|
|
| 46 |
st.error(f"Error initializing MT5: {str(e)}")
|
| 47 |
raise
|
| 48 |
|
| 49 |
+
def detect_events(self, text, entity):
|
| 50 |
+
"""Detect events using MT5"""
|
| 51 |
+
# Initialize default return values
|
| 52 |
+
event_type = "Нет"
|
| 53 |
+
summary = ""
|
| 54 |
+
|
| 55 |
try:
|
| 56 |
+
prompt = f"""<s>Analyze news about company {entity}:
|
| 57 |
+
|
| 58 |
+
{text}
|
| 59 |
+
|
| 60 |
+
Classify event type as one of:
|
| 61 |
+
- Отчетность (financial reports)
|
| 62 |
+
- РЦБ (securities market events)
|
| 63 |
+
- Суд (legal actions)
|
| 64 |
+
- Нет (no significant events)
|
| 65 |
+
|
| 66 |
+
Format response as:
|
| 67 |
+
Тип: [type]
|
| 68 |
+
Краткое описание: [summary]</s>"""
|
| 69 |
+
|
| 70 |
inputs = self.tokenizer(
|
| 71 |
prompt,
|
| 72 |
return_tensors="pt",
|
|
|
|
| 83 |
pad_token_id=self.tokenizer.pad_token_id
|
| 84 |
)
|
| 85 |
|
| 86 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
# Parse response
|
| 89 |
if "Тип:" in response and "Краткое описание:" in response:
|
| 90 |
+
parts = response.split("Краткое описание:")
|
| 91 |
+
type_part = parts[0]
|
| 92 |
+
if "Тип:" in type_part:
|
| 93 |
+
event_type = type_part.split("Тип:")[1].strip()
|
| 94 |
+
# Validate event type
|
| 95 |
+
valid_types = ["Отчетность", "РЦБ", "Суд", "Нет"]
|
| 96 |
+
if event_type not in valid_types:
|
| 97 |
+
event_type = "Нет"
|
| 98 |
+
|
| 99 |
+
if len(parts) > 1:
|
| 100 |
+
summary = parts[1].strip()
|
| 101 |
|
| 102 |
return event_type, summary
|
| 103 |
|
|
|
|
| 107 |
|
| 108 |
def estimate_impact(self, text, entity):
|
| 109 |
"""Estimate impact using MT5"""
|
| 110 |
+
# Initialize default return values
|
| 111 |
+
impact = "Неопределенный эффект"
|
| 112 |
+
reasoning = "Не удалось определить влияние"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
try:
|
| 115 |
+
prompt = f"""<s>Analyze impact of news about company {entity}:
|
| 116 |
+
|
| 117 |
+
{text}
|
| 118 |
+
|
| 119 |
+
Classify impact as one of:
|
| 120 |
+
- Значительный риск убытков
|
| 121 |
+
- Умеренный риск убытков
|
| 122 |
+
- Незначительный риск убытков
|
| 123 |
+
- Вероятность прибыли
|
| 124 |
+
- Неопределенный эффект
|
| 125 |
+
|
| 126 |
+
Format response as:
|
| 127 |
+
Impact: [category]
|
| 128 |
+
Reasoning: [explanation]</s>"""
|
| 129 |
+
|
| 130 |
+
inputs = self.tokenizer(
|
| 131 |
+
prompt,
|
| 132 |
+
return_tensors="pt",
|
| 133 |
+
padding=True,
|
| 134 |
+
truncation=True,
|
| 135 |
+
max_length=512
|
| 136 |
+
).to(self.device)
|
| 137 |
|
| 138 |
+
outputs = self.model.generate(
|
| 139 |
+
**inputs,
|
| 140 |
+
max_length=200,
|
| 141 |
+
num_return_sequences=1,
|
| 142 |
+
do_sample=False,
|
| 143 |
+
pad_token_id=self.tokenizer.pad_token_id
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 147 |
|
| 148 |
if "Impact:" in response and "Reasoning:" in response:
|
| 149 |
+
parts = response.split("Reasoning:")
|
| 150 |
+
impact_part = parts[0]
|
| 151 |
+
if "Impact:" in impact_part:
|
| 152 |
+
impact = impact_part.split("Impact:")[1].strip()
|
| 153 |
+
# Validate impact category
|
| 154 |
+
valid_impacts = [
|
| 155 |
+
"Значительный риск убытков",
|
| 156 |
+
"Умеренный риск убытков",
|
| 157 |
+
"Незначительный риск убытков",
|
| 158 |
+
"Вероятность прибыли",
|
| 159 |
+
"Неопределенный эффект"
|
| 160 |
+
]
|
| 161 |
+
if impact not in valid_impacts:
|
| 162 |
+
impact = "Неопределенный эффект"
|
| 163 |
+
|
| 164 |
+
if len(parts) > 1:
|
| 165 |
+
reasoning = parts[1].strip()
|
| 166 |
|
| 167 |
return impact, reasoning
|
| 168 |
|
|
|
|
| 762 |
return output
|
| 763 |
def main():
|
| 764 |
with st.sidebar:
|
| 765 |
+
st.title("::: AI-анализ мониторинга новостей (v.3.48):::")
|
| 766 |
st.subheader("по материалам СКАН-ИНТЕРФАКС ")
|
| 767 |
|
| 768 |
|