ahmertalal commited on
Commit
99cae8f
Β·
verified Β·
1 Parent(s): 746411c

main code file

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import time
4
+
5
+ # Hugging Face API key from secrets
6
+ API_KEY = st.secrets["API_KEY"]
7
+ HEADERS = {"Authorization": f"Bearer {API_KEY}"}
8
+
9
+ API_URLS = {
10
+ "Summarizer": "https://api-inference.huggingface.co/models/facebook/bart-large-cnn",
11
+ "Sentiment": "https://api-inference.huggingface.co/models/finiteautomata/bertweet-base-sentiment-analysis"
12
+ }
13
+
14
+ def query(api_url, payload):
15
+ try:
16
+ res = requests.post(api_url, headers=HEADERS, json=payload, timeout=60)
17
+ if res.status_code != 200:
18
+ return {"error": f"HTTP {res.status_code}: {res.text}"}
19
+ return res.json()
20
+ except Exception as e:
21
+ return {"error": str(e)}
22
+
23
+ st.set_page_config(page_title="NLP Toolkit", page_icon="🧠", layout="centered")
24
+ st.title("🧠 AI NLP Toolkit")
25
+ st.write("Summarization & Sentiment Analysis using Hugging Face APIs πŸš€")
26
+
27
+ tab1, tab2 = st.tabs(["πŸ“„ Summarizer", "πŸ“ Sentiment Analysis"])
28
+
29
+ with tab1:
30
+ text = st.text_area("Enter text to summarize:", height=200)
31
+ if st.button("Summarize"):
32
+ if not text.strip():
33
+ st.warning("⚠ Please enter some text.")
34
+ else:
35
+ with st.spinner("Generating summary..."):
36
+ time.sleep(1)
37
+ res = query(API_URLS["Summarizer"], {"inputs": text})
38
+ if "error" in res:
39
+ st.error(res["error"])
40
+ else:
41
+ st.success("βœ… Summary Generated")
42
+ st.write(res[0]['summary_text'])
43
+
44
+ with tab2:
45
+ text = st.text_area("Enter text for sentiment analysis:", height=200, key="sent_text")
46
+ if st.button("Analyze Sentiment"):
47
+ if not text.strip():
48
+ st.warning("⚠ Please enter some text.")
49
+ else:
50
+ with st.spinner("Analyzing sentiment..."):
51
+ time.sleep(1)
52
+ res = query(API_URLS["Sentiment"], {"inputs": text})
53
+ if "error" in res:
54
+ st.error(res["error"])
55
+ else:
56
+ st.success("βœ… Sentiment Analysis Complete")
57
+ for item in res[0]:
58
+ st.write(f"**{item['label']}** β†’ {item['score']:.2f}")