alako commited on
Commit
2a6d12e
1 Parent(s): cbecaeb

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +35 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
+
5
+ # Use caching to optimize resource usage
6
+ @st.cache_resource
7
+ def load_model():
8
+ model_name = 'cardiffnlp/twitter-xlm-roberta-base-sentiment'
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
10
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
11
+ labels = ['Negative', 'Neutral', 'Positive']
12
+ return tokenizer, model, labels
13
+
14
+ tokenizer, model, labels = load_model()
15
+
16
+ def analyze_sentiment(text):
17
+ inputs = tokenizer(text, return_tensors='pt', truncation=True)
18
+ with torch.no_grad():
19
+ outputs = model(**inputs)
20
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
21
+ sentiment = labels[torch.argmax(probs)]
22
+ confidence = torch.max(probs).item()
23
+ return sentiment, confidence
24
+
25
+ st.title('Multilingual Sentiment Analysis')
26
+
27
+ text = st.text_area('Enter text in any language:')
28
+
29
+ if st.button('Analyze'):
30
+ if text:
31
+ sentiment, confidence = analyze_sentiment(text)
32
+ st.write(f"**Sentiment:** {sentiment}")
33
+ st.write(f"**Confidence:** {confidence:.2f}")
34
+ else:
35
+ st.write("Please enter some text to analyze.")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ transformers
3
+ torch
4
+ sentencepiece