import streamlit as st import requests import time # Hugging Face API key from secrets API_KEY = st.secrets["API_KEY"] HEADERS = {"Authorization": f"Bearer {API_KEY}"} API_URLS = { "Summarizer": "https://api-inference.huggingface.co/models/facebook/bart-large-cnn", "Sentiment": "https://api-inference.huggingface.co/models/finiteautomata/bertweet-base-sentiment-analysis" } def query(api_url, payload): try: res = requests.post(api_url, headers=HEADERS, json=payload, timeout=60) if res.status_code != 200: return {"error": f"HTTP {res.status_code}: {res.text}"} return res.json() except Exception as e: return {"error": str(e)} st.set_page_config(page_title="NLP Toolkit", page_icon="🧠", layout="centered") st.title("🧠 AI NLP Toolkit") st.write("Summarization & Sentiment Analysis using Hugging Face APIs 🚀") tab1, tab2 = st.tabs(["📄 Summarizer", "📝 Sentiment Analysis"]) with tab1: text = st.text_area("Enter text to summarize:", height=200) if st.button("Summarize"): if not text.strip(): st.warning("⚠ Please enter some text.") else: with st.spinner("Generating summary..."): time.sleep(1) res = query(API_URLS["Summarizer"], {"inputs": text}) if "error" in res: st.error(res["error"]) else: st.success("✅ Summary Generated") st.write(res[0]['summary_text']) with tab2: text = st.text_area("Enter text for sentiment analysis:", height=200, key="sent_text") if st.button("Analyze Sentiment"): if not text.strip(): st.warning("⚠ Please enter some text.") else: with st.spinner("Analyzing sentiment..."): time.sleep(1) res = query(API_URLS["Sentiment"], {"inputs": text}) if "error" in res: st.error(res["error"]) else: st.success("✅ Sentiment Analysis Complete") for item in res[0]: st.write(f"**{item['label']}** → {item['score']:.2f}")