Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import os
|
|
| 6 |
|
| 7 |
# --- CONFIGURATION ---
|
| 8 |
EXCEL_CSV_URL = "https://docs.google.com/spreadsheets/d/1b_gBz-_TZZNJLIM5sYyYJQfBVNua7dFf/export?format=csv"
|
| 9 |
-
GROQ_API_KEY = os.getenv("GROQ_API_KEY") #
|
| 10 |
GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 11 |
LLAMA3_MODEL = "llama3-8b-8192"
|
| 12 |
|
|
@@ -17,24 +17,39 @@ def load_excel_data():
|
|
| 17 |
response = requests.get(EXCEL_CSV_URL)
|
| 18 |
response.raise_for_status()
|
| 19 |
df = pd.read_csv(BytesIO(response.content))
|
| 20 |
-
st.write("β
**Columns Detected in Sheet:**", list(df.columns)) # Help with debugging
|
| 21 |
return df
|
| 22 |
except Exception as e:
|
| 23 |
st.error(f"Error loading sheet: {e}")
|
| 24 |
return pd.DataFrame()
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return result
|
| 30 |
|
| 31 |
def generate_summary_from_data(student_row):
|
| 32 |
content = student_row.to_dict(orient="records")[0]
|
| 33 |
prompt = (
|
| 34 |
-
f"
|
| 35 |
-
f"{content}\n
|
| 36 |
-
f"
|
|
|
|
|
|
|
| 37 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
headers = {
|
| 40 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
@@ -58,9 +73,10 @@ def main():
|
|
| 58 |
st.set_page_config("π Student Result Lookup", layout="centered")
|
| 59 |
st.title("π Student Result Generator with AI")
|
| 60 |
|
| 61 |
-
st.markdown("Enter your **Roll Number**
|
| 62 |
|
| 63 |
roll_number = st.text_input("Roll Number", placeholder="e.g., k0000")
|
|
|
|
| 64 |
|
| 65 |
if st.button("Generate Result"):
|
| 66 |
with st.spinner("π₯ Loading data and generating result..."):
|
|
@@ -68,14 +84,14 @@ def main():
|
|
| 68 |
if data.empty:
|
| 69 |
return
|
| 70 |
try:
|
| 71 |
-
result =
|
| 72 |
if not result.empty:
|
| 73 |
st.success("β
Student found. Generating summary...")
|
| 74 |
summary = generate_summary_from_data(result)
|
| 75 |
st.markdown("### π Result Summary:")
|
| 76 |
st.markdown(summary)
|
| 77 |
else:
|
| 78 |
-
st.warning("β No student found with that roll number.")
|
| 79 |
except Exception as e:
|
| 80 |
st.error(f"β Error: {e}")
|
| 81 |
|
|
|
|
| 6 |
|
| 7 |
# --- CONFIGURATION ---
|
| 8 |
EXCEL_CSV_URL = "https://docs.google.com/spreadsheets/d/1b_gBz-_TZZNJLIM5sYyYJQfBVNua7dFf/export?format=csv"
|
| 9 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Ensure this is set in your environment
|
| 10 |
GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 11 |
LLAMA3_MODEL = "llama3-8b-8192"
|
| 12 |
|
|
|
|
| 17 |
response = requests.get(EXCEL_CSV_URL)
|
| 18 |
response.raise_for_status()
|
| 19 |
df = pd.read_csv(BytesIO(response.content))
|
|
|
|
| 20 |
return df
|
| 21 |
except Exception as e:
|
| 22 |
st.error(f"Error loading sheet: {e}")
|
| 23 |
return pd.DataFrame()
|
| 24 |
|
| 25 |
+
def search_student(data, roll_number, student_name):
|
| 26 |
+
df = data.copy()
|
| 27 |
+
# Normalize column names
|
| 28 |
+
df.columns = [col.strip().lower().replace(" ", "_") for col in df.columns]
|
| 29 |
+
# Normalize input
|
| 30 |
+
roll_number = roll_number.strip().lower()
|
| 31 |
+
student_name = student_name.strip().lower()
|
| 32 |
+
# Search for matching row
|
| 33 |
+
result = df[
|
| 34 |
+
(df['roll_number'].astype(str).str.lower() == roll_number) &
|
| 35 |
+
(df['student_name'].astype(str).str.lower() == student_name)
|
| 36 |
+
]
|
| 37 |
return result
|
| 38 |
|
| 39 |
def generate_summary_from_data(student_row):
|
| 40 |
content = student_row.to_dict(orient="records")[0]
|
| 41 |
prompt = (
|
| 42 |
+
f"Generate a detailed student result summary in the following format:\n\n"
|
| 43 |
+
f"Student Name: {content.get('student_name', '')}\n"
|
| 44 |
+
f"Roll Number: {content.get('roll_number', '')}\n"
|
| 45 |
+
f"Class: {content.get('class', '')}\n"
|
| 46 |
+
f"Subjects and Marks:\n"
|
| 47 |
)
|
| 48 |
+
# Add subjects and marks
|
| 49 |
+
for key, value in content.items():
|
| 50 |
+
if key not in ['student_name', 'roll_number', 'class']:
|
| 51 |
+
prompt += f"{key.replace('_', ' ').title()}: {value}\n"
|
| 52 |
+
prompt += "\nInclude total marks, percentage, and a performance remark."
|
| 53 |
|
| 54 |
headers = {
|
| 55 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
|
|
| 73 |
st.set_page_config("π Student Result Lookup", layout="centered")
|
| 74 |
st.title("π Student Result Generator with AI")
|
| 75 |
|
| 76 |
+
st.markdown("Enter your **Roll Number** and **Student Name** to generate your result.")
|
| 77 |
|
| 78 |
roll_number = st.text_input("Roll Number", placeholder="e.g., k0000")
|
| 79 |
+
student_name = st.text_input("Student Name", placeholder="e.g., Ahmed Raza")
|
| 80 |
|
| 81 |
if st.button("Generate Result"):
|
| 82 |
with st.spinner("π₯ Loading data and generating result..."):
|
|
|
|
| 84 |
if data.empty:
|
| 85 |
return
|
| 86 |
try:
|
| 87 |
+
result = search_student(data, roll_number, student_name)
|
| 88 |
if not result.empty:
|
| 89 |
st.success("β
Student found. Generating summary...")
|
| 90 |
summary = generate_summary_from_data(result)
|
| 91 |
st.markdown("### π Result Summary:")
|
| 92 |
st.markdown(summary)
|
| 93 |
else:
|
| 94 |
+
st.warning("β No student found with that roll number and name.")
|
| 95 |
except Exception as e:
|
| 96 |
st.error(f"β Error: {e}")
|
| 97 |
|