File size: 6,276 Bytes
f097dc8
 
 
 
 
 
01d137f
f097dc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01d137f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f097dc8
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import streamlit as st
import pandas as pd
from transformers import pipeline
from sklearn.metrics.pairwise import cosine_similarity
from sentence_transformers import SentenceTransformer
import numpy as np

# Set modern page configuration
st.set_page_config(page_title="News Analyzer", layout="wide")

# Inject custom CSS for sleek dark blue theme with black fonts
st.markdown("""
    <style>
    /* Global Styling */
    body {
        background: #0b132b;
        color: black;
        font-family: 'Arial', sans-serif;
    }
    
    /* Header Styling */
    .custom-header {
        background: linear-gradient(to right, #1f4068, #1b1b2f);
        padding: 1.5rem;
        border-radius: 12px;
        text-align: center;
        color: white;
        font-size: 30px;
        font-weight: bold;
        box-shadow: 0px 4px 15px rgba(0, 217, 255, 0.3);
    }
    /* Card Container */
    .glass-container {
        background: rgba(255, 255, 255, 0.08);
        border-radius: 15px;
        padding: 25px;
        backdrop-filter: blur(15px);
        box-shadow: 0px 4px 20px rgba(0, 217, 255, 0.2);
        transition: transform 0.3s ease-in-out;
    }
    .glass-container:hover {
        transform: scale(1.02);
    }
    /* Buttons */
    .stButton>button {
        background: linear-gradient(45deg, #0072ff, #00c6ff);
        color: black;
        border-radius: 8px;
        padding: 14px 28px;
        font-size: 18px;
        transition: 0.3s ease;
        border: none;
    }
    .stButton>button:hover {
        transform: scale(1.05);
        box-shadow: 0px 4px 10px rgba(0, 255, 255, 0.5);
    }
    /* Text Input */
    .stTextInput>div>div>input {
        background-color: rgba(255, 255, 255, 0.1);
        border-radius: 8px;
        color: black;
        padding: 12px;
        font-size: 18px;
    }
    /* Dataframe Container */
    .dataframe-container {
        background: rgba(255, 255, 255, 0.1);
        padding: 15px;
        border-radius: 12px;
        color: black;
    }
    /* Answer Display Box - Larger */
    .answer-box {
        background: rgba(0, 217, 255, 0.15);
        padding: 35px;
        border-radius: 15px;
        border: 2px solid rgba(0, 217, 255, 0.6);
        color: black;
        font-size: 22px;
        text-align: center;
        margin-bottom: 20px;
        min-height: 150px;
        box-shadow: 0px 2px 12px rgba(0, 217, 255, 0.3);
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.3s ease;
    }
    /* CSV Display Box */
    .csv-box {
        background: rgba(255, 255, 255, 0.1);
        padding: 15px;
        border-radius: 12px;
        margin-top: 20px;
        box-shadow: 0px 2px 12px rgba(0, 217, 255, 0.3);
    }
    </style>
    """, unsafe_allow_html=True)

# Modern Header
st.markdown("<div class='custom-header'> 🧩 AI-Powered News Analyzer</div>", unsafe_allow_html=True)

# Load the Hugging Face model
pipe = pipeline("question-answering", model="distilbert/distilbert-base-cased-distilled-squad")

# Initialize sentence transformer model
sentence_model = SentenceTransformer('all-MiniLM-L6-v2')  # Pre-trained sentence model

# Responsive Layout - Uses full width
col1, col2 = st.columns([1.1, 1])

# Left Section - File Upload & CSV/Excel Display
with col1:
    st.markdown("<div class='glass-container'>", unsafe_allow_html=True)
    st.subheader("📂 Upload News Data")
    uploaded_file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"])
    
    if uploaded_file is not None:
        # Determine the file extension
        file_extension = uploaded_file.name.split('.')[-1]
        
        if file_extension == 'csv':
            df = pd.read_csv(uploaded_file)
        elif file_extension == 'xlsx':
            df = pd.read_excel(uploaded_file)
        
        # Download button
        st.download_button(
            label="⬇️ Download Processed Data",
            data=df.to_csv(index=False).encode('utf-8'),
            file_name="output.csv",
            mime="text/csv"
        )

        # CSV Preview Box
        st.markdown("<div class='csv-box'><h4 style='color: black;'>📜 CSV/Excel Preview</h4>", unsafe_allow_html=True)
        st.dataframe(df, use_container_width=True)
        st.markdown("</div>", unsafe_allow_html=True)

    st.markdown("</div>", unsafe_allow_html=True)

# Right Section - Q&A Interface
with col2:
    st.markdown("<div class='glass-container'>", unsafe_allow_html=True)
    st.subheader("🤖 AI Assistant")

    # Answer Display Box (Initially Empty)
    answer_placeholder = st.empty()
    answer_placeholder.markdown("<div class='answer-box'></div>", unsafe_allow_html=True)

    # Question Input
    st.markdown("### 🔍 Ask Your Question:")
    user_question = st.text_input("Enter your question here", label_visibility="hidden")  # Hides the label
    
    # Button & Answer Display
    if st.button("🔮 Get Answer"):
        if user_question.strip() and uploaded_file is not None:
            with st.spinner("⏳ Wait, our agent will look into that..."):
                # Extract the 1st column as context (0-indexed)
                context = df.iloc[:, 0].dropna().tolist()
                
                # Generate embeddings for the context rows and the question
                context_embeddings = sentence_model.encode(context)
                question_embedding = sentence_model.encode([user_question])
                
                # Calculate cosine similarity
                similarities = cosine_similarity(question_embedding, context_embeddings)
                top_indices = similarities[0].argsort()[-5:][::-1]  # Get top 5 similar rows
                
                # Prepare the top 5 similar context rows
                top_context = "\n".join([context[i] for i in top_indices])
                
                # Get answer from Hugging Face model using top context
                result = pipe(question=user_question, context=top_context)
                answer = result['answer']
        else:
            answer = "⚠️ Please upload a valid file first!"

        answer_placeholder.markdown(f"<div class='answer-box'>{answer}</div>", unsafe_allow_html=True)

    st.markdown("</div>", unsafe_allow_html=True)