Ashendilantha commited on
Commit
41fe177
Β·
verified Β·
1 Parent(s): c885670

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +131 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from transformers import pipeline
4
+ from PIL import Image
5
+
6
+ # Load Models
7
+ news_classifier = pipeline("text-classification", model="Oneli/News_Classification")
8
+ qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
9
+
10
+ # Label Mapping
11
+ label_mapping = {
12
+ "LABEL_0": "Business",
13
+ "LABEL_1": "Opinion",
14
+ "LABEL_2": "Political Gossip",
15
+ "LABEL_3": "Sports",
16
+ "LABEL_4": "World News"
17
+ }
18
+
19
+ # Store classified article for QA
20
+ context_storage = {"context": "", "bulk_context": "", "num_articles": 0}
21
+
22
+ # Define the functions
23
+ def classify_text(text):
24
+ result = news_classifier(text)[0]
25
+ category = label_mapping.get(result['label'], "Unknown")
26
+ confidence = round(result['score'] * 100, 2)
27
+
28
+ # Store context for QA
29
+ context_storage["context"] = text
30
+
31
+ return category, f"Confidence: {confidence}%"
32
+
33
+ def classify_csv(file_path):
34
+ try:
35
+ df = pd.read_csv(file_path, encoding="utf-8")
36
+
37
+ # Automatically detect the column containing text
38
+ text_column = df.columns[0] # Assume first column is the text column
39
+
40
+ df["Encoded Prediction"] = df[text_column].apply(lambda x: news_classifier(str(x))[0]['label'])
41
+ df["Decoded Prediction"] = df["Encoded Prediction"].map(label_mapping)
42
+ df["Confidence"] = df[text_column].apply(lambda x: round(news_classifier(str(x))[0]['score'] * 100, 2))
43
+
44
+ # Store all text as a single context for QA
45
+ context_storage["bulk_context"] = " ".join(df[text_column].dropna().astype(str).tolist())
46
+ context_storage["num_articles"] = len(df)
47
+
48
+ output_file = "output.csv"
49
+ df.to_csv(output_file, index=False)
50
+
51
+ return df, output_file
52
+ except Exception as e:
53
+ return None, f"Error: {str(e)}"
54
+
55
+ def chatbot_response(history, user_input, source):
56
+ user_input = user_input.lower()
57
+
58
+ # Select context based on source toggle
59
+ context = context_storage["context"] if source == "Single Article" else context_storage["bulk_context"]
60
+ num_articles = context_storage["num_articles"]
61
+
62
+ if "number of articles" in user_input or "how many articles" in user_input:
63
+ answer = f"There are {num_articles} articles in the uploaded CSV."
64
+ history.append([user_input, answer])
65
+ return history, ""
66
+
67
+ if context:
68
+ result = qa_pipeline(question=user_input, context=context)
69
+ answer = result["answer"]
70
+ history.append([user_input, answer])
71
+ return history, ""
72
+
73
+ # Default responses if no context is available
74
+ responses = {
75
+ "hello": "πŸ‘‹ Hello! How can I assist you with news today?",
76
+ "hi": "😊 Hi there! What do you want to know about news?",
77
+ "how are you": "πŸ€– I'm just a bot, but I'm here to help!",
78
+ "thank you": "πŸ™ You're welcome! Let me know if you need anything else.",
79
+ "news": "πŸ“° I can classify news into Business, Sports, Politics, and more!",
80
+ }
81
+
82
+ response = responses.get(user_input,
83
+ "πŸ€” I'm here to help with news classification and general info. Ask me about news topics!")
84
+ history.append([user_input, response])
85
+ return history, ""
86
+
87
+ # Streamlit App Layout
88
+ st.set_page_config(page_title="News Classifier", page_icon="πŸ“°")
89
+
90
+ # Load Cover Image
91
+ cover_image = Image.open("cover.png") # Ensure this image exists
92
+ st.image(cover_image, caption="News Classifier πŸ“’", use_column_width=True)
93
+
94
+ # Section for Single Article Classification
95
+ st.subheader("πŸ“° Single Article Classification")
96
+ text_input = st.text_area("Enter News Text", placeholder="Type or paste news content here...")
97
+ if st.button("πŸ” Classify"):
98
+ if text_input:
99
+ category, confidence = classify_text(text_input)
100
+ st.write(f"**Predicted Category:** {category}")
101
+ st.write(f"**Confidence Level:** {confidence}")
102
+ else:
103
+ st.warning("Please enter some text to classify.")
104
+
105
+ # Section for Bulk CSV Classification
106
+ st.subheader("πŸ“‚ Bulk Classification (CSV)")
107
+ file_input = st.file_uploader("Upload CSV File", type="csv")
108
+ if file_input:
109
+ df, output_file = classify_csv(file_input)
110
+ if df is not None:
111
+ st.dataframe(df)
112
+ st.download_button(
113
+ label="Download Processed CSV",
114
+ data=open(output_file, 'rb').read(),
115
+ file_name=output_file,
116
+ mime="text/csv"
117
+ )
118
+ else:
119
+ st.error(f"Error processing file: {output_file}")
120
+
121
+ # Section for Chatbot Interaction
122
+ st.subheader("πŸ’¬ AI Chat Assistant")
123
+ history = []
124
+ user_input = st.text_input("Ask about news classification or topics", placeholder="Type a message...")
125
+ source_toggle = st.radio("Select Context Source", ["Single Article", "Bulk Classification"])
126
+ if st.button("βœ‰ Send"):
127
+ history, bot_response = chatbot_response(history, user_input, source_toggle)
128
+ st.write("**Chatbot Response:**")
129
+ for q, a in history:
130
+ st.write(f"**Q:** {q}")
131
+ st.write(f"**A:** {a}")
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ pandas
3
+ transformers
4
+ torch
5
+ Pillow