Spaces:
Sleeping
Sleeping
Commit ·
1254c79
1
Parent(s): 21b8a80
progress more 75
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import time
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
| 5 |
import io
|
| 6 |
from rapidfuzz import fuzz
|
| 7 |
import os
|
|
@@ -44,36 +45,39 @@ def init_langchain_llm():
|
|
| 44 |
|
| 45 |
def estimate_sentiment_and_impact(llm, news_text, entity):
|
| 46 |
template = """
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
2. Оцените потенциальное финансовое влияние в рублях для этого объекта в ближайшие 6 месяцев.
|
| 50 |
|
| 51 |
-
|
| 52 |
-
1.
|
| 53 |
-
2.
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
|
| 62 |
-
|
|
|
|
| 63 |
Sentiment: [Positive/Negative/Neutral]
|
| 64 |
-
Impact: [
|
| 65 |
-
Reasoning: [
|
| 66 |
"""
|
| 67 |
prompt = PromptTemplate(template=template, input_variables=["entity", "news"])
|
| 68 |
chain = prompt | llm | RunnablePassthrough()
|
| 69 |
response = chain.invoke({"entity": entity, "news": news_text})
|
| 70 |
|
| 71 |
sentiment = "Neutral"
|
| 72 |
-
impact = "
|
| 73 |
-
reasoning = "
|
| 74 |
|
| 75 |
if isinstance(response, str):
|
| 76 |
try:
|
|
|
|
| 77 |
if "Sentiment:" in response:
|
| 78 |
sentiment_part = response.split("Sentiment:")[1].split("\n")[0].strip().lower()
|
| 79 |
if "positive" in sentiment_part:
|
|
@@ -81,10 +85,26 @@ def estimate_sentiment_and_impact(llm, news_text, entity):
|
|
| 81 |
elif "negative" in sentiment_part:
|
| 82 |
sentiment = "Negative"
|
| 83 |
|
|
|
|
| 84 |
if "Impact:" in response and "Reasoning:" in response:
|
| 85 |
impact_part, reasoning_part = response.split("Reasoning:")
|
| 86 |
impact = impact_part.split("Impact:")[1].strip()
|
| 87 |
reasoning = reasoning_part.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
st.error(f"Error parsing LLM response: {str(e)}")
|
| 90 |
|
|
@@ -286,7 +306,7 @@ def main():
|
|
| 286 |
unsafe_allow_html=True
|
| 287 |
)
|
| 288 |
|
| 289 |
-
st.title("
|
| 290 |
|
| 291 |
if 'processed_df' not in st.session_state:
|
| 292 |
st.session_state.processed_df = None
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import time
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
+
from openpyxl.utils.dataframe import dataframe_to_rows
|
| 6 |
import io
|
| 7 |
from rapidfuzz import fuzz
|
| 8 |
import os
|
|
|
|
| 45 |
|
| 46 |
def estimate_sentiment_and_impact(llm, news_text, entity):
|
| 47 |
template = """
|
| 48 |
+
First, translate this Russian text into English:
|
| 49 |
+
"{news}"
|
|
|
|
| 50 |
|
| 51 |
+
Then, analyze the translated text about the entity "{entity}" and determine:
|
| 52 |
+
1. Sentiment (Positive/Negative/Neutral)
|
| 53 |
+
2. Estimate potential financial impact in Russian rubles for this entity in the next 6 months.
|
| 54 |
+
|
| 55 |
+
If precise monetary estimate is not possible, categorize the impact as one of the following:
|
| 56 |
+
1. "Significant risk of loss"
|
| 57 |
+
2. "Moderate risk of loss"
|
| 58 |
+
3. "Minor risk of loss"
|
| 59 |
+
4. "Probability of profit"
|
| 60 |
+
5. "Uncertain effect"
|
| 61 |
|
| 62 |
+
Provide a brief reasoning (maximum 100 words).
|
| 63 |
|
| 64 |
+
Your response should be in the following format:
|
| 65 |
+
Translation: [Your English translation]
|
| 66 |
Sentiment: [Positive/Negative/Neutral]
|
| 67 |
+
Impact: [Your estimate or category]
|
| 68 |
+
Reasoning: [Your reasoning]
|
| 69 |
"""
|
| 70 |
prompt = PromptTemplate(template=template, input_variables=["entity", "news"])
|
| 71 |
chain = prompt | llm | RunnablePassthrough()
|
| 72 |
response = chain.invoke({"entity": entity, "news": news_text})
|
| 73 |
|
| 74 |
sentiment = "Neutral"
|
| 75 |
+
impact = "Uncertain effect"
|
| 76 |
+
reasoning = "Unable to provide reasoning"
|
| 77 |
|
| 78 |
if isinstance(response, str):
|
| 79 |
try:
|
| 80 |
+
# Extract sentiment
|
| 81 |
if "Sentiment:" in response:
|
| 82 |
sentiment_part = response.split("Sentiment:")[1].split("\n")[0].strip().lower()
|
| 83 |
if "positive" in sentiment_part:
|
|
|
|
| 85 |
elif "negative" in sentiment_part:
|
| 86 |
sentiment = "Negative"
|
| 87 |
|
| 88 |
+
# Extract impact and reasoning
|
| 89 |
if "Impact:" in response and "Reasoning:" in response:
|
| 90 |
impact_part, reasoning_part = response.split("Reasoning:")
|
| 91 |
impact = impact_part.split("Impact:")[1].strip()
|
| 92 |
reasoning = reasoning_part.strip()
|
| 93 |
+
|
| 94 |
+
# Translate impact categories back to Russian
|
| 95 |
+
impact_mapping = {
|
| 96 |
+
"Significant risk of loss": "Значительный риск убытков",
|
| 97 |
+
"Moderate risk of loss": "Умеренный риск убытков",
|
| 98 |
+
"Minor risk of loss": "Незначительный риск убытков",
|
| 99 |
+
"Probability of profit": "Вероятность прибыли",
|
| 100 |
+
"Uncertain effect": "Неопределенный эффект"
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
for eng, rus in impact_mapping.items():
|
| 104 |
+
if eng.lower() in impact.lower():
|
| 105 |
+
impact = rus
|
| 106 |
+
break
|
| 107 |
+
|
| 108 |
except Exception as e:
|
| 109 |
st.error(f"Error parsing LLM response: {str(e)}")
|
| 110 |
|
|
|
|
| 306 |
unsafe_allow_html=True
|
| 307 |
)
|
| 308 |
|
| 309 |
+
st.title("::: анализ мониторинга новостей СКАН-ИНТЕРФАКС :::")
|
| 310 |
|
| 311 |
if 'processed_df' not in st.session_state:
|
| 312 |
st.session_state.processed_df = None
|