Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,7 @@ def readLMwords():
|
|
| 20 |
|
| 21 |
def sentiment_analysis(sentence, model_name):
|
| 22 |
model_name = "CCCC/"+model_name
|
| 23 |
-
|
| 24 |
template = '{"placeholder":"text_a"} Shares are {"mask"}.'
|
| 25 |
classes = ['positive', 'neutral', 'negative']
|
| 26 |
positive,negative,neutral = readLMwords()
|
|
@@ -36,17 +36,19 @@ def sentiment_analysis(sentence, model_name):
|
|
| 36 |
"CCCC/RoBERTa_English_FinancialNews_tuned":"roberta",
|
| 37 |
}
|
| 38 |
|
| 39 |
-
if 'Chinese' in
|
| 40 |
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-zh-en")
|
| 41 |
model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-zh-en")
|
| 42 |
|
| 43 |
translated_tokens = model.generate(
|
| 44 |
-
**tokenizer(
|
| 45 |
)
|
| 46 |
-
|
| 47 |
for t in translated_tokens:
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
testdata = []
|
| 52 |
for i,sentence in enumerate(sentences):
|
|
@@ -81,7 +83,7 @@ def sentiment_analysis(sentence, model_name):
|
|
| 81 |
for step, inputs in enumerate(test_dataloader):
|
| 82 |
logits = prompt_model(inputs)
|
| 83 |
result.extend(torch.argmax(logits, dim=-1))
|
| 84 |
-
output = '\n'.join([classes[i] for i in result])
|
| 85 |
return str(output)
|
| 86 |
|
| 87 |
|
|
|
|
| 20 |
|
| 21 |
def sentiment_analysis(sentence, model_name):
|
| 22 |
model_name = "CCCC/"+model_name
|
| 23 |
+
raw_sentences = sentence.strip().split('\n')
|
| 24 |
template = '{"placeholder":"text_a"} Shares are {"mask"}.'
|
| 25 |
classes = ['positive', 'neutral', 'negative']
|
| 26 |
positive,negative,neutral = readLMwords()
|
|
|
|
| 36 |
"CCCC/RoBERTa_English_FinancialNews_tuned":"roberta",
|
| 37 |
}
|
| 38 |
|
| 39 |
+
if 'Chinese' in model_name:
|
| 40 |
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-zh-en")
|
| 41 |
model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-zh-en")
|
| 42 |
|
| 43 |
translated_tokens = model.generate(
|
| 44 |
+
**tokenizer(raw_sentences, return_tensors="pt", padding=True)
|
| 45 |
)
|
| 46 |
+
sentences_translated = []
|
| 47 |
for t in translated_tokens:
|
| 48 |
+
sentences_translated.append(tokenizer.decode(t, skip_special_tokens=True))
|
| 49 |
+
sentences = sentences_translated
|
| 50 |
+
else:
|
| 51 |
+
sentences = raw_sentences
|
| 52 |
|
| 53 |
testdata = []
|
| 54 |
for i,sentence in enumerate(sentences):
|
|
|
|
| 83 |
for step, inputs in enumerate(test_dataloader):
|
| 84 |
logits = prompt_model(inputs)
|
| 85 |
result.extend(torch.argmax(logits, dim=-1))
|
| 86 |
+
output = '\n'.join([f"{classes[res]}, {raw_sentences[i]}" for i,res in enumerate(result)])
|
| 87 |
return str(output)
|
| 88 |
|
| 89 |
|