Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,48 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
|
| 3 |
-
# Page setup
|
| 4 |
-
st.set_page_config(page_title="
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
st.write("If you see this message, your Space is working correctly π")
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
else:
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
|
| 4 |
+
# β
Page setup
|
| 5 |
+
st.set_page_config(page_title="IMDB Sentiment Analyzer π¬", page_icon="π¬", layout="centered")
|
| 6 |
|
| 7 |
+
# Hugging Face model API
|
| 8 |
+
API_URL = "https://api-inference.huggingface.co/models/ibrahim313/my-imdb-sentiment-analyzer"
|
|
|
|
| 9 |
|
| 10 |
+
# Auth headers if private model
|
| 11 |
+
headers = {}
|
| 12 |
+
if "HF_TOKEN" in st.secrets:
|
| 13 |
+
headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}
|
| 14 |
+
|
| 15 |
+
def query(payload):
|
| 16 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 17 |
+
return response.json()
|
| 18 |
+
|
| 19 |
+
# --- UI ---
|
| 20 |
+
st.title("π¬ IMDB Sentiment Analyzer")
|
| 21 |
+
st.markdown("### Predict whether a movie review is **Positive π** or **Negative π**")
|
| 22 |
+
|
| 23 |
+
# Pre-filled example
|
| 24 |
+
default_text = "I really loved this movie, the acting was fantastic and the story was emotional."
|
| 25 |
+
user_input = st.text_area("βοΈ Enter your review below:", value=default_text, height=150)
|
| 26 |
+
|
| 27 |
+
if st.button("π Analyze Sentiment"):
|
| 28 |
+
if user_input.strip() == "":
|
| 29 |
+
st.warning("β οΈ Please enter some text")
|
| 30 |
else:
|
| 31 |
+
result = query({"inputs": user_input})
|
| 32 |
+
|
| 33 |
+
if isinstance(result, list) and len(result) > 0 and isinstance(result[0], list):
|
| 34 |
+
label = result[0][0]["label"]
|
| 35 |
+
score = result[0][0]["score"]
|
| 36 |
+
|
| 37 |
+
# Emoji for label
|
| 38 |
+
emoji = "π" if "pos" in label.lower() else "π"
|
| 39 |
+
|
| 40 |
+
st.markdown(f"### {emoji} Prediction: **{label}**")
|
| 41 |
+
st.progress(min(max(score, 0.0), 1.0)) # Clamp between 0-1
|
| 42 |
+
st.caption(f"Confidence: {score:.2%}")
|
| 43 |
+
else:
|
| 44 |
+
st.error(f"β οΈ Error from model: {result}")
|
| 45 |
+
|
| 46 |
+
# Footer
|
| 47 |
+
st.markdown("---")
|
| 48 |
+
st.caption("Built with β€οΈ using Streamlit + Hugging Face")
|