Spaces:
Running
Running
Commit ·
68cd5a6
1
Parent(s): 229101e
progress more (back to 3.20)
Browse files
app.py
CHANGED
|
@@ -98,32 +98,7 @@ roberta = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-
|
|
| 98 |
finbert_tone = pipeline("sentiment-analysis", model="yiyanghkust/finbert-tone")
|
| 99 |
|
| 100 |
|
| 101 |
-
def translate_text(llm, text):
|
| 102 |
-
template = """
|
| 103 |
-
Translate this Russian text into English:
|
| 104 |
-
"{text}"
|
| 105 |
-
|
| 106 |
-
Your response should contain only the English translation.
|
| 107 |
-
"""
|
| 108 |
-
prompt = PromptTemplate(template=template, input_variables=["text"])
|
| 109 |
-
chain = prompt | llm | RunnablePassthrough()
|
| 110 |
-
response = chain.invoke({"text": text})
|
| 111 |
-
|
| 112 |
-
# Handle different response types
|
| 113 |
-
if hasattr(response, 'content'): # If it's an AIMessage object
|
| 114 |
-
return response.content.strip()
|
| 115 |
-
elif isinstance(response, str): # If it's a string
|
| 116 |
-
return response.strip()
|
| 117 |
-
else:
|
| 118 |
-
return str(response).strip() # Convert any other type to string
|
| 119 |
|
| 120 |
-
def get_mapped_sentiment(result):
|
| 121 |
-
label = result['label'].lower()
|
| 122 |
-
if label in ["positive", "label_2", "pos", "pos_label"]:
|
| 123 |
-
return "Positive"
|
| 124 |
-
elif label in ["negative", "label_0", "neg", "neg_label"]:
|
| 125 |
-
return "Negative"
|
| 126 |
-
return "Neutral"
|
| 127 |
|
| 128 |
def analyze_sentiment(text):
|
| 129 |
finbert_result = get_mapped_sentiment(finbert(text, truncation=True, max_length=512)[0])
|
|
@@ -150,6 +125,42 @@ def fuzzy_deduplicate(df, column, threshold=65):
|
|
| 150 |
seen_texts.append(text)
|
| 151 |
indices_to_keep.append(i)
|
| 152 |
return df.iloc[indices_to_keep]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
def init_langchain_llm(model_choice):
|
| 155 |
try:
|
|
@@ -165,13 +176,13 @@ def init_langchain_llm(model_choice):
|
|
| 165 |
temperature=0.0
|
| 166 |
)
|
| 167 |
|
| 168 |
-
elif model_choice == "ChatGPT-
|
| 169 |
if 'openai_key' not in st.secrets:
|
| 170 |
st.error("OpenAI API key not found in secrets. Please add it with the key 'openai_key'.")
|
| 171 |
st.stop()
|
| 172 |
|
| 173 |
return ChatOpenAI(
|
| 174 |
-
model="gpt-4o",
|
| 175 |
openai_api_key=st.secrets['openai_key'],
|
| 176 |
temperature=0.0
|
| 177 |
)
|
|
@@ -457,7 +468,7 @@ def create_output_file(df, uploaded_file, llm):
|
|
| 457 |
|
| 458 |
def main():
|
| 459 |
with st.sidebar:
|
| 460 |
-
st.title("::: AI-анализ мониторинга новостей (v.3.
|
| 461 |
st.subheader("по материалам СКАН-ИНТЕРФАКС ")
|
| 462 |
|
| 463 |
model_choice = st.radio(
|
|
|
|
| 98 |
finbert_tone = pipeline("sentiment-analysis", model="yiyanghkust/finbert-tone")
|
| 99 |
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
def analyze_sentiment(text):
|
| 104 |
finbert_result = get_mapped_sentiment(finbert(text, truncation=True, max_length=512)[0])
|
|
|
|
| 125 |
seen_texts.append(text)
|
| 126 |
indices_to_keep.append(i)
|
| 127 |
return df.iloc[indices_to_keep]
|
| 128 |
+
def translate_text(llm, text):
|
| 129 |
+
try:
|
| 130 |
+
# Debug print
|
| 131 |
+
st.write(f"Debug - Model type: {type(llm)}")
|
| 132 |
+
st.write(f"Debug - Model attributes: {dir(llm)}")
|
| 133 |
+
|
| 134 |
+
messages = [
|
| 135 |
+
{"role": "system", "content": "You are a translator. Translate the given Russian text to English accurately and concisely."},
|
| 136 |
+
{"role": "user", "content": f"Translate this Russian text to English: {text}"}
|
| 137 |
+
]
|
| 138 |
+
|
| 139 |
+
# For different model types, we'll use different approaches
|
| 140 |
+
if isinstance(llm, ChatOpenAI):
|
| 141 |
+
try:
|
| 142 |
+
# Direct ChatCompletion call
|
| 143 |
+
response = llm.invoke(messages)
|
| 144 |
+
st.write(f"Debug - Response type: {type(response)}")
|
| 145 |
+
st.write(f"Debug - Response: {response}")
|
| 146 |
+
|
| 147 |
+
# Handle response based on its type
|
| 148 |
+
if hasattr(response, 'content'):
|
| 149 |
+
return response.content.strip()
|
| 150 |
+
elif isinstance(response, str):
|
| 151 |
+
return response.strip()
|
| 152 |
+
else:
|
| 153 |
+
return str(response).strip()
|
| 154 |
+
except Exception as e:
|
| 155 |
+
st.error(f"Translation API error: {str(e)}")
|
| 156 |
+
return text
|
| 157 |
+
else:
|
| 158 |
+
st.error(f"Unsupported model type: {type(llm)}")
|
| 159 |
+
return text
|
| 160 |
+
|
| 161 |
+
except Exception as e:
|
| 162 |
+
st.error(f"Translation error: {str(e)}")
|
| 163 |
+
return text # Return original text if translation fails
|
| 164 |
|
| 165 |
def init_langchain_llm(model_choice):
|
| 166 |
try:
|
|
|
|
| 176 |
temperature=0.0
|
| 177 |
)
|
| 178 |
|
| 179 |
+
elif model_choice == "ChatGPT-4-mini":
|
| 180 |
if 'openai_key' not in st.secrets:
|
| 181 |
st.error("OpenAI API key not found in secrets. Please add it with the key 'openai_key'.")
|
| 182 |
st.stop()
|
| 183 |
|
| 184 |
return ChatOpenAI(
|
| 185 |
+
model="gpt-4o-mini", # Changed from gpt-4o to gpt-4
|
| 186 |
openai_api_key=st.secrets['openai_key'],
|
| 187 |
temperature=0.0
|
| 188 |
)
|
|
|
|
| 468 |
|
| 469 |
def main():
|
| 470 |
with st.sidebar:
|
| 471 |
+
st.title("::: AI-анализ мониторинга новостей (v.3.20):::")
|
| 472 |
st.subheader("по материалам СКАН-ИНТЕРФАКС ")
|
| 473 |
|
| 474 |
model_choice = st.radio(
|