Spaces:
Sleeping
Sleeping
Commit
·
aa39cf1
1
Parent(s):
ec96c5b
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import RobertaForSequenceClassification, RobertaTokenizer
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
def model1():
|
7 |
+
# Your Model 1 code here
|
8 |
+
st.title("Emotion Analysis")
|
9 |
+
model_path = "mymodel.pth" # Replace with the actual path to your trained model
|
10 |
+
tokenizer = RobertaTokenizer.from_pretrained(model_path)
|
11 |
+
model = RobertaForSequenceClassification.from_pretrained(model_path)
|
12 |
+
|
13 |
+
# Set the model to evaluation mode
|
14 |
+
model.eval()
|
15 |
+
|
16 |
+
# Labels for your specific task
|
17 |
+
labels = ['surprise', 'fun', 'anger', 'boredom', 'hate', 'neutral', 'worry', 'enthusiasm', 'sadness','relief', 'empty', 'happiness', 'love'] # Replace with your actual label names
|
18 |
+
|
19 |
+
# Streamlit app
|
20 |
+
user_input = st.text_area("Enter text for analysis:")
|
21 |
+
if st.button("Analyze"):
|
22 |
+
if user_input:
|
23 |
+
# Tokenize and preprocess the input
|
24 |
+
input_ids = tokenizer.encode(user_input, return_tensors="pt")
|
25 |
+
# Make prediction
|
26 |
+
with torch.no_grad():
|
27 |
+
output = model(input_ids)
|
28 |
+
# Get predicted probabilities
|
29 |
+
probabilities = torch.sigmoid(output.logits)
|
30 |
+
|
31 |
+
# Check if the lengths match before creating the DataFrame
|
32 |
+
if len(labels) == len(probabilities[0]):
|
33 |
+
# Display the probabilities as individual bars
|
34 |
+
df = pd.DataFrame({
|
35 |
+
"Label": labels,
|
36 |
+
"Probability": probabilities[0].tolist()
|
37 |
+
})
|
38 |
+
|
39 |
+
st.bar_chart(df.set_index("Label"))
|
40 |
+
|
41 |
+
# Display the emotion labels and scores
|
42 |
+
st.subheader("Emotion Analysis Output:")
|
43 |
+
for i, result in enumerate(sorted(zip(labels, probabilities[0]), key=lambda x: x[1], reverse=True)):
|
44 |
+
label, score = result
|
45 |
+
st.write(f"{i + 1}. {label.capitalize()}: {score:.4f}")
|
46 |
+
else:
|
47 |
+
st.error("Error: The length of labels and probabilities does not match.")
|
48 |
+
else:
|
49 |
+
st.warning("Please enter text for analysis.")
|
50 |
+
|
51 |
+
if __name__ == "__main__":
|
52 |
+
model1()
|