File size: 2,273 Bytes
87eacf1 d8bc695 1be00bd d8bc695 1be00bd d8bc695 2f9e0fe d8bc695 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
pip install transformers
.\venv\Scripts\activate # Windows
import streamlit as st
from transformers import pipeline, TFAutoModelForSequenceClassification, AutoTokenizer
from datasets import load_dataset
import pandas as pd
st.set_page_config(layout="wide")
# Load dataset
@st.cache_resource
def load_data():
dataset = load_dataset("WhiteAngelss/Turkce-Duygu-Analizi-Dataset")
return dataset
dataset = load_data()
st.title("Sentiment Analysis with Turkish Dataset")
# Show dataset examples
st.subheader("Sample Data")
sample_df = pd.DataFrame(dataset['train'])
st.write(sample_df.head())
# Set up model
model_list = ['WhiteAngelss/entity-word-sentiment-analysis']
st.sidebar.header("Select Sentiment Analysis Model")
model_checkpoint = st.sidebar.radio("", model_list)
st.sidebar.write("For details of models: 'https://huggingface.co/WhiteAngelss/entity-word-sentiment-analysis'")
st.sidebar.write("")
st.subheader("Select Text Input Method")
input_method = st.radio("", ('Select from Examples', 'Write or Paste New Text'))
if input_method == 'Select from Examples':
example_texts = dataset['train']['text'][:5] # Sample examples from the dataset
selected_text = st.selectbox('Select Text from List', example_texts)
st.subheader("Text to Analyze")
input_text = st.text_area("Selected Text", selected_text, height=128, max_chars=None)
elif input_method == "Write or Paste New Text":
st.subheader("Text to Analyze")
input_text = st.text_area('Write or Paste Text Below', value="", height=128, max_chars=None)
@st.cache_resource
def setModel(model_checkpoint):
model = TFAutoModelForSequenceClassification.from_pretrained(model_checkpoint)
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
return pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
Run_Button = st.button("Run")
if Run_Button and input_text:
sentiment_pipeline = setModel(model_checkpoint)
output = sentiment_pipeline(input_text)
st.subheader("Sentiment Analysis Results")
df = pd.DataFrame(output)
st.dataframe(df)
# Display the sentiment in a more user-friendly format
sentiment = output[0]['label']
score = output[0]['score']
st.write(f"Sentiment: {sentiment} (Score: {score:.2f})")
|