| | import streamlit as st |
| | import requests |
| | import json |
| |
|
| | st.set_page_config( |
| | page_title="Sentiment Analysis App", |
| | page_icon="π", |
| | layout="centered" |
| | ) |
| |
|
| | st.title("Sentiment Analysis App") |
| | st.write("Enter text to analyze its sentiment using Hugging Face's API") |
| |
|
| | |
| | api_key = st.text_input("Enter your Hugging Face API key:", type="password", help="Your Hugging Face API token") |
| |
|
| | |
| | model_options = { |
| | "DistilBERT (SST-2)": "distilbert/distilbert-base-uncased-finetuned-sst-2-english", |
| | "Twitter-roBERTa-base": "cardiffnlp/twitter-roberta-base-sentiment", |
| | "BERT-base-multilingual": "nlptown/bert-base-multilingual-uncased-sentiment" |
| | } |
| | selected_model = st.selectbox("Select a sentiment analysis model:", options=list(model_options.keys())) |
| |
|
| | |
| | text_input = st.text_area("Enter text to analyze:", height=150) |
| |
|
| | |
| | def analyze_sentiment(text, model, api_key): |
| | API_URL = f"https://api-inference.huggingface.co/models/{model}" |
| | headers = { |
| | "Authorization": f"Bearer {api_key}" |
| | } |
| | |
| | payload = { |
| | "inputs": text, |
| | } |
| | |
| | try: |
| | response = requests.post(API_URL, headers=headers, json=payload) |
| | return response.json() |
| | except Exception as e: |
| | return {"error": str(e)} |
| |
|
| | |
| | if st.button("Analyze Sentiment"): |
| | if not api_key: |
| | st.error("Please enter your Hugging Face API key") |
| | elif not text_input: |
| | st.error("Please enter some text to analyze") |
| | else: |
| | with st.spinner("Analyzing sentiment..."): |
| | selected_model_path = model_options[selected_model] |
| | result = analyze_sentiment(text_input, selected_model_path, api_key) |
| | |
| | |
| | try: |
| | if "error" in result: |
| | st.error(f"Error: {result['error']}") |
| | elif isinstance(result, list) and len(result) > 0: |
| | |
| | if isinstance(result[0], list): |
| | items = result[0] |
| | else: |
| | items = result |
| | |
| | |
| | highest_item = max(items, key=lambda x: x['score']) |
| | score = highest_item['score'] |
| | label = highest_item['label'].lower() |
| | |
| | |
| | st.subheader("Sentiment:") |
| | col1, col2 = st.columns([1, 3]) |
| | |
| | |
| | if 'positive' in label or 'pos' in label or '5' in label or '4' in label: |
| | if score > 0.9: |
| | emoji = "π" |
| | elif score > 0.7: |
| | emoji = "π" |
| | else: |
| | emoji = "π" |
| | sentiment_text = f"Positive ({score:.2f})" |
| | elif 'negative' in label or 'neg' in label or '1' in label or '2' in label: |
| | if score > 0.9: |
| | emoji = "π‘" |
| | elif score > 0.7: |
| | emoji = "π " |
| | else: |
| | emoji = "βΉ" |
| | sentiment_text = f"Negative ({score:.2f})" |
| | else: |
| | emoji = "π" |
| | sentiment_text = f"Neutral ({score:.2f})" |
| | |
| | with col1: |
| | st.markdown(f"<h1 style='font-size:4rem; text-align:center;'>{emoji}</h1>", unsafe_allow_html=True) |
| | with col2: |
| | st.markdown(f"<h2>{sentiment_text}</h2>", unsafe_allow_html=True) |
| | |
| | |
| | st.progress(score) |
| | else: |
| | st.warning("Unexpected response format. Please check your API key and try again.") |
| | st.json(result) |
| | except Exception as e: |
| | st.error(f"Error processing results: {str(e)}") |
| | st.json(result) |
| |
|
| | |
| | st.markdown("---") |
| | st.markdown("Built with Streamlit and Hugging Face API") |
| |
|