Chamodi123 commited on
Commit
f097dc8
·
verified ·
1 Parent(s): dfbbcfa

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +181 -0
app.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from transformers import pipeline
4
+ from sklearn.metrics.pairwise import cosine_similarity
5
+ from sentence_transformers import SentenceTransformer
6
+ import numpy as np
7
+ ##
8
+ # Set modern page configuration
9
+ st.set_page_config(page_title="News Analyzer", layout="wide")
10
+
11
+ # Inject custom CSS for sleek dark blue theme with black fonts
12
+ st.markdown("""
13
+ <style>
14
+ /* Global Styling */
15
+ body {
16
+ background: #0b132b;
17
+ color: black;
18
+ font-family: 'Arial', sans-serif;
19
+ }
20
+
21
+ /* Header Styling */
22
+ .custom-header {
23
+ background: linear-gradient(to right, #1f4068, #1b1b2f);
24
+ padding: 1.5rem;
25
+ border-radius: 12px;
26
+ text-align: center;
27
+ color: white;
28
+ font-size: 30px;
29
+ font-weight: bold;
30
+ box-shadow: 0px 4px 15px rgba(0, 217, 255, 0.3);
31
+ }
32
+ /* Card Container */
33
+ .glass-container {
34
+ background: rgba(255, 255, 255, 0.08);
35
+ border-radius: 15px;
36
+ padding: 25px;
37
+ backdrop-filter: blur(15px);
38
+ box-shadow: 0px 4px 20px rgba(0, 217, 255, 0.2);
39
+ transition: transform 0.3s ease-in-out;
40
+ }
41
+ .glass-container:hover {
42
+ transform: scale(1.02);
43
+ }
44
+ /* Buttons */
45
+ .stButton>button {
46
+ background: linear-gradient(45deg, #0072ff, #00c6ff);
47
+ color: black;
48
+ border-radius: 8px;
49
+ padding: 14px 28px;
50
+ font-size: 18px;
51
+ transition: 0.3s ease;
52
+ border: none;
53
+ }
54
+ .stButton>button:hover {
55
+ transform: scale(1.05);
56
+ box-shadow: 0px 4px 10px rgba(0, 255, 255, 0.5);
57
+ }
58
+ /* Text Input */
59
+ .stTextInput>div>div>input {
60
+ background-color: rgba(255, 255, 255, 0.1);
61
+ border-radius: 8px;
62
+ color: black;
63
+ padding: 12px;
64
+ font-size: 18px;
65
+ }
66
+ /* Dataframe Container */
67
+ .dataframe-container {
68
+ background: rgba(255, 255, 255, 0.1);
69
+ padding: 15px;
70
+ border-radius: 12px;
71
+ color: black;
72
+ }
73
+ /* Answer Display Box - Larger */
74
+ .answer-box {
75
+ background: rgba(0, 217, 255, 0.15);
76
+ padding: 35px;
77
+ border-radius: 15px;
78
+ border: 2px solid rgba(0, 217, 255, 0.6);
79
+ color: black;
80
+ font-size: 22px;
81
+ text-align: center;
82
+ margin-bottom: 20px;
83
+ min-height: 150px;
84
+ box-shadow: 0px 2px 12px rgba(0, 217, 255, 0.3);
85
+ display: flex;
86
+ align-items: center;
87
+ justify-content: center;
88
+ transition: all 0.3s ease;
89
+ }
90
+ /* CSV Display Box */
91
+ .csv-box {
92
+ background: rgba(255, 255, 255, 0.1);
93
+ padding: 15px;
94
+ border-radius: 12px;
95
+ margin-top: 20px;
96
+ box-shadow: 0px 2px 12px rgba(0, 217, 255, 0.3);
97
+ }
98
+ </style>
99
+ """, unsafe_allow_html=True)
100
+
101
+ # Modern Header
102
+ st.markdown("<div class='custom-header'> 🧩 AI-Powered News Analyzer</div>", unsafe_allow_html=True)
103
+
104
+ # Load the Hugging Face model
105
+ pipe = pipeline("question-answering", model="distilbert/distilbert-base-cased-distilled-squad")
106
+
107
+ # Initialize sentence transformer model
108
+ sentence_model = SentenceTransformer('all-MiniLM-L6-v2') # Pre-trained sentence model
109
+
110
+ # Responsive Layout - Uses full width
111
+ col1, col2 = st.columns([1.1, 1])
112
+
113
+ # Left Section - File Upload & CSV/Excel Display
114
+ with col1:
115
+ st.markdown("<div class='glass-container'>", unsafe_allow_html=True)
116
+ st.subheader("📂 Upload News Data")
117
+ uploaded_file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"])
118
+
119
+ if uploaded_file is not None:
120
+ # Determine the file extension
121
+ file_extension = uploaded_file.name.split('.')[-1]
122
+
123
+ if file_extension == 'csv':
124
+ df = pd.read_csv(uploaded_file)
125
+ elif file_extension == 'xlsx':
126
+ df = pd.read_excel(uploaded_file)
127
+
128
+ # Download button
129
+ st.download_button(
130
+ label="⬇️ Download Processed Data",
131
+ data=df.to_csv(index=False).encode('utf-8'),
132
+ file_name="output.csv",
133
+ mime="text/csv"
134
+ )
135
+
136
+ # CSV Preview Box
137
+ st.markdown("<div class='csv-box'><h4 style='color: black;'>📜 CSV/Excel Preview</h4>", unsafe_allow_html=True)
138
+ st.dataframe(df, use_container_width=True)
139
+ st.markdown("</div>", unsafe_allow_html=True)
140
+
141
+ st.markdown("</div>", unsafe_allow_html=True)
142
+
143
+ # Right Section - Q&A Interface
144
+ with col2:
145
+ st.markdown("<div class='glass-container'>", unsafe_allow_html=True)
146
+ st.subheader("🤖 AI Assistant")
147
+
148
+ # Answer Display Box (Initially Empty)
149
+ answer_placeholder = st.empty()
150
+ answer_placeholder.markdown("<div class='answer-box'></div>", unsafe_allow_html=True)
151
+
152
+ # Question Input
153
+ st.markdown("### 🔍 Ask Your Question:")
154
+ user_question = st.text_input("Enter your question here", label_visibility="hidden") # Hides the label
155
+
156
+ # Button & Answer Display
157
+ if st.button("🔮 Get Answer"):
158
+ if user_question.strip() and uploaded_file is not None:
159
+ # Extract the 3rd column from the dataframe
160
+ context = df.iloc[:, 2].dropna().tolist() # Get the 3rd column as context (0-based index)
161
+
162
+ # Generate embeddings for the context (3rd column rows) and the question
163
+ context_embeddings = sentence_model.encode(context)
164
+ question_embedding = sentence_model.encode([user_question])
165
+
166
+ # Calculate cosine similarity
167
+ similarities = cosine_similarity(question_embedding, context_embeddings)
168
+ top_indices = similarities[0].argsort()[-5:][::-1] # Get top 5 similar rows
169
+
170
+ # Prepare the top 5 similar context rows
171
+ top_context = "\n".join([context[i] for i in top_indices])
172
+
173
+ # Get answer from Hugging Face model using top context
174
+ result = pipe(question=user_question, context=top_context)
175
+ answer = result['answer']
176
+ else:
177
+ answer = "⚠️ Please upload a valid file first!"
178
+
179
+ answer_placeholder.markdown(f"<div class='answer-box'>{answer}</div>", unsafe_allow_html=True)
180
+
181
+ st.markdown("</div>", unsafe_allow_html=True)