Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
|
| 5 |
+
# Load pre-trained model for embedding
|
| 6 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 7 |
+
|
| 8 |
+
# Function to process uploaded files
|
| 9 |
+
def process_file(uploaded_file):
|
| 10 |
+
try:
|
| 11 |
+
if uploaded_file.name.endswith('.xlsx') or uploaded_file.name.endswith('.xls'):
|
| 12 |
+
df = pd.read_excel(uploaded_file)
|
| 13 |
+
elif uploaded_file.name.endswith('.csv'):
|
| 14 |
+
df = pd.read_csv(uploaded_file)
|
| 15 |
+
elif uploaded_file.name.endswith('.pdf'):
|
| 16 |
+
from PyPDF2 import PdfReader
|
| 17 |
+
reader = PdfReader(uploaded_file)
|
| 18 |
+
text = "".join(page.extract_text() for page in reader.pages)
|
| 19 |
+
df = pd.DataFrame([row.split() for row in text.splitlines()], columns=["Name", "Grade", "Marks"])
|
| 20 |
+
else:
|
| 21 |
+
st.error("Unsupported file format. Please upload Excel, CSV, or PDF.")
|
| 22 |
+
return None
|
| 23 |
+
return df
|
| 24 |
+
except Exception as e:
|
| 25 |
+
st.error(f"Error processing file: {e}")
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
# Main app
|
| 29 |
+
def main():
|
| 30 |
+
st.title("School Performance Analysis App")
|
| 31 |
+
st.write("Upload a document containing student grades and marks to analyze their performance.")
|
| 32 |
+
|
| 33 |
+
uploaded_file = st.file_uploader("Upload Word, Excel, or PDF file", type=["xlsx", "xls", "csv", "pdf"])
|
| 34 |
+
|
| 35 |
+
if uploaded_file:
|
| 36 |
+
df = process_file(uploaded_file)
|
| 37 |
+
if df is not None:
|
| 38 |
+
st.subheader("Uploaded Data")
|
| 39 |
+
st.write(df.head())
|
| 40 |
+
|
| 41 |
+
# Add embedding column
|
| 42 |
+
df['Embedding'] = df.apply(lambda row: model.encode(f"{row['Name']} {row['Grade']} {row['Marks']}"), axis=1)
|
| 43 |
+
|
| 44 |
+
# Top 10 students
|
| 45 |
+
top_students = df.sort_values(by="Marks", ascending=False).head(10)
|
| 46 |
+
st.subheader("Top 10 Students")
|
| 47 |
+
st.write(top_students[["Name", "Grade", "Marks"]])
|
| 48 |
+
|
| 49 |
+
# Search functionality
|
| 50 |
+
st.subheader("Search for a Student")
|
| 51 |
+
search_query = st.text_input("Enter the student's name or details:")
|
| 52 |
+
if search_query:
|
| 53 |
+
search_embedding = model.encode(search_query)
|
| 54 |
+
df['Similarity'] = df['Embedding'].apply(lambda emb: (emb @ search_embedding) / (emb.dot(emb) ** 0.5))
|
| 55 |
+
result = df.sort_values(by="Similarity", ascending=False).iloc[0]
|
| 56 |
+
st.write("Search Result:")
|
| 57 |
+
st.write(result[["Name", "Grade", "Marks"]])
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
main()
|