Spaces:
Runtime error
Runtime error
Jainesh212
commited on
Commit
·
31b46d4
1
Parent(s):
a9f43b4
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import transformers
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Define sentiment analysis models
|
6 |
+
models = {
|
7 |
+
"DistilBERT": transformers.pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english"),
|
8 |
+
"BERT": transformers.pipeline("sentiment-analysis", model="bert-base-uncased-finetuned-sst-2-english"),
|
9 |
+
"RoBERTa": transformers.pipeline("sentiment-analysis", model="roberta-base-openai-detector"),
|
10 |
+
}
|
11 |
+
|
12 |
+
# Define function to analyze sentiment using selected model
|
13 |
+
def analyze_sentiment(text, model_name):
|
14 |
+
model = models[model_name]
|
15 |
+
result = model(text)[0]
|
16 |
+
return result['label'], result['score']
|
17 |
+
|
18 |
+
# Define Streamlit app
|
19 |
+
def app():
|
20 |
+
st.title("Sentiment Analysis App")
|
21 |
+
|
22 |
+
# User input
|
23 |
+
text = st.text_area("Enter text to analyze", max_chars=1024)
|
24 |
+
|
25 |
+
# Sentiment analysis
|
26 |
+
if st.button("Analyze"):
|
27 |
+
st.write("Analyzing sentiment...")
|
28 |
+
with st.spinner("Wait for it..."):
|
29 |
+
results = []
|
30 |
+
for model_name in models:
|
31 |
+
label, score = analyze_sentiment(text, model_name)
|
32 |
+
results.append((model_name, label, score))
|
33 |
+
st.success("Sentiment analysis complete!")
|
34 |
+
st.write("Results:")
|
35 |
+
for model_name, label, score in results:
|
36 |
+
st.write(f"- {model_name}: {label} ({score:.2f})")
|
37 |
+
|
38 |
+
# Advanced features
|
39 |
+
if st.beta_expander("Advanced Options", expanded=False):
|
40 |
+
model_name = st.selectbox("Select model", list(models.keys()))
|
41 |
+
if st.button("Test model"):
|
42 |
+
label, score = analyze_sentiment("This is a test.", model_name)
|
43 |
+
st.write(f"Test result: {label} ({score:.2f})")
|
44 |
+
|
45 |
+
# Run Streamlit app
|
46 |
+
if __name__ == "__main__":
|
47 |
+
app()
|