embed786's picture
Update app.py
45558ba verified
import streamlit as st
import requests
# βœ… Page setup
st.set_page_config(page_title="IMDB Sentiment Analyzer 🎬", page_icon="🎬", layout="centered")
# Hugging Face model API
API_URL = "https://api-inference.huggingface.co/models/ibrahim313/my-imdb-sentiment-analyzer"
#HF_TOKEN
# Auth headers if private model
#headers = {}
#if "hfsecret" in st.secrets:
print(f"token : {st.secrets['hfsecret']}")
headers = {"Authorization": f"Bearer {st.secrets['hfsecret']}"}
def query(payload):
response = requests.post(API_URL, headers="", json=payload)
return response.json()
# --- UI ---
st.title("🎬 IMDB Sentiment Analyzer")
st.markdown("### Predict whether a movie review is **Positive πŸ˜€** or **Negative 😞**")
# Pre-filled example
default_text = "I really loved this movie, the acting was fantastic and the story was emotional."
user_input = st.text_area("✍️ Enter your review below:", value=default_text, height=150)
if st.button("πŸ” Analyze Sentiment"):
if user_input.strip() == "":
st.warning("⚠️ Please enter some text")
else:
result = query({"inputs": user_input})
if isinstance(result, list) and len(result) > 0 and isinstance(result[0], list):
label = result[0][0]["label"]
score = result[0][0]["score"]
# Emoji for label
emoji = "πŸ˜€" if "pos" in label.lower() else "😞"
st.markdown(f"### {emoji} Prediction: **{label}**")
st.progress(min(max(score, 0.0), 1.0)) # Clamp between 0-1
st.caption(f"Confidence: {score:.2%}")
else:
st.error(f"⚠️ Error from model: {result}")
# Footer
st.markdown("---")
st.caption("Built with ❀️ using Streamlit + Hugging Face")