Ashendilantha commited on
Commit
023e889
Β·
verified Β·
1 Parent(s): 613983d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -41
app.py CHANGED
@@ -8,34 +8,7 @@ from nltk.stem import WordNetLemmatizer
8
  from transformers import pipeline
9
  from PIL import Image
10
 
11
- # βœ… Set page config as the first Streamlit command (default to wide)
12
- st.set_page_config(page_title="News Classifier", page_icon="πŸ“°", layout="wide")
13
-
14
- # βœ… Use JavaScript to detect screen size and update session state
15
- device_type_script = """
16
- <script>
17
- let screen_width = window.innerWidth;
18
- let device_type = screen_width < 768 ? "Mobile" : "PC";
19
- let streamlitVar = window.parent;
20
- streamlitVar.postMessage({device_type: device_type}, "*");
21
- </script>
22
- """
23
- st.markdown(device_type_script, unsafe_allow_html=True)
24
-
25
- # βœ… Initialize session state
26
- if "device_type" not in st.session_state:
27
- st.session_state.device_type = "PC" # Default to PC
28
-
29
- # βœ… Allow user to manually select device type (Optional)
30
- st.session_state.device_type = st.radio("Select your device type", ["Mobile", "PC"], index=0 if st.session_state.device_type == "PC" else 1)
31
-
32
- # βœ… Apply UI adjustments based on device type
33
- if st.session_state.device_type == "Mobile":
34
- st.write("πŸ“± Mobile Mode Activated (UI adjusted)")
35
- else:
36
- st.write("πŸ’» PC Mode Activated (Full-width UI)")
37
-
38
- # βœ… Continue with the rest of the app
39
  nltk.download('stopwords')
40
  nltk.download('wordnet')
41
  nltk.download('omw-1.4')
@@ -56,13 +29,6 @@ label_mapping = {
56
  # Store classified article for QA
57
  context_storage = {"context": "", "bulk_context": "", "num_articles": 0}
58
 
59
- # Detect device type
60
- device_type = st.radio("Are you using a mobile device or a PC?", ["Mobile", "PC"])
61
- if device_type == "Mobile":
62
- st.set_page_config(layout="centered")
63
- else:
64
- st.set_page_config(layout="wide")
65
-
66
  # Text Cleaning Functions
67
  def clean_text(text):
68
  text = text.lower()
@@ -125,6 +91,35 @@ def chatbot_response(history, user_input, text_input=None, file_input=None):
125
  return history, answer
126
 
127
  # Streamlit App Layout
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  cover_image = Image.open("cover.png") # Ensure this image exists
129
  st.image(cover_image, caption="News Classifier πŸ“’", use_container_width=True)
130
 
@@ -134,8 +129,8 @@ text_input = st.text_area("Enter News Text", placeholder="Type or paste news con
134
  if st.button("πŸ” Classify"):
135
  if text_input:
136
  category, confidence = classify_text(text_input)
137
- st.write(f"*Predicted Category:* {category}")
138
- st.write(f"*Confidence Level:* {confidence}")
139
  else:
140
  st.warning("Please enter some text to classify.")
141
 
@@ -162,8 +157,7 @@ user_input = st.text_input("Ask about news classification or topics", placeholde
162
  source_toggle = st.radio("Select Context Source", ["Single Article", "Bulk Classification"])
163
  if st.button("βœ‰ Send"):
164
  history, bot_response = chatbot_response(history, user_input, text_input=text_input if source_toggle == "Single Article" else None, file_input=file_input if source_toggle == "Bulk Classification" else None)
165
- st.write("*Chatbot Response:*")
166
  for q, a in history:
167
- st.write(f"*Q:* {q}")
168
- st.write(f"*A:* {a}")
169
-
 
8
  from transformers import pipeline
9
  from PIL import Image
10
 
11
+ # Download required NLTK data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  nltk.download('stopwords')
13
  nltk.download('wordnet')
14
  nltk.download('omw-1.4')
 
29
  # Store classified article for QA
30
  context_storage = {"context": "", "bulk_context": "", "num_articles": 0}
31
 
 
 
 
 
 
 
 
32
  # Text Cleaning Functions
33
  def clean_text(text):
34
  text = text.lower()
 
91
  return history, answer
92
 
93
  # Streamlit App Layout
94
+ st.set_page_config(page_title="News Classifier", page_icon="πŸ“°")
95
+
96
+ # Custom CSS for responsive design (desktop/mobile optimization)
97
+ st.markdown(
98
+ """
99
+ <style>
100
+ @media only screen and (max-width: 600px) {
101
+ .stApp {
102
+ padding: 10px;
103
+ }
104
+ }
105
+
106
+ @media only screen and (min-width: 601px) {
107
+ .stApp {
108
+ padding: 20px;
109
+ }
110
+ }
111
+
112
+ .stButton {
113
+ margin-top: 20px;
114
+ }
115
+
116
+ .stTextInput {
117
+ width: 100%;
118
+ }
119
+ </style>
120
+ """, unsafe_allow_html=True
121
+ )
122
+
123
  cover_image = Image.open("cover.png") # Ensure this image exists
124
  st.image(cover_image, caption="News Classifier πŸ“’", use_container_width=True)
125
 
 
129
  if st.button("πŸ” Classify"):
130
  if text_input:
131
  category, confidence = classify_text(text_input)
132
+ st.write(f"Predicted Category: {category}")
133
+ st.write(f"Confidence Level: {confidence}")
134
  else:
135
  st.warning("Please enter some text to classify.")
136
 
 
157
  source_toggle = st.radio("Select Context Source", ["Single Article", "Bulk Classification"])
158
  if st.button("βœ‰ Send"):
159
  history, bot_response = chatbot_response(history, user_input, text_input=text_input if source_toggle == "Single Article" else None, file_input=file_input if source_toggle == "Bulk Classification" else None)
160
+ st.write("Chatbot Response:")
161
  for q, a in history:
162
+ st.write(f"Q: {q}")
163
+ st.write(f"A:Β {a}")