Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import PyPDF2
|
| 4 |
+
import json
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
# Parse uploaded transcript file
|
| 8 |
+
def parse_transcript(file):
|
| 9 |
+
if file.name.endswith('.csv'):
|
| 10 |
+
df = pd.read_csv(file.name)
|
| 11 |
+
elif file.name.endswith(('.xls', '.xlsx')):
|
| 12 |
+
df = pd.read_excel(file.name)
|
| 13 |
+
elif file.name.endswith('.pdf'):
|
| 14 |
+
reader = PyPDF2.PdfReader(file)
|
| 15 |
+
text = ""
|
| 16 |
+
for page in reader.pages:
|
| 17 |
+
text += page.extract_text() or ""
|
| 18 |
+
df = pd.DataFrame({'Transcript_Text': [text]})
|
| 19 |
+
else:
|
| 20 |
+
raise ValueError("Unsupported file format. Use .csv, .xlsx, or .pdf")
|
| 21 |
+
return df
|
| 22 |
+
|
| 23 |
+
# Extract student info
|
| 24 |
+
def extract_transcript_info(df):
|
| 25 |
+
transcript_text = df['Transcript_Text'].iloc[0] if 'Transcript_Text' in df.columns else ''
|
| 26 |
+
info = {}
|
| 27 |
+
gpa_match = re.search(r'(GPA|Grade Point Average)[^\d]*(\d+\.\d+)', transcript_text, re.IGNORECASE)
|
| 28 |
+
if gpa_match:
|
| 29 |
+
info['GPA'] = gpa_match.group(2)
|
| 30 |
+
grade_match = re.search(r'Grade:?\s*(\d{1,2})', transcript_text, re.IGNORECASE)
|
| 31 |
+
if grade_match:
|
| 32 |
+
info['Grade_Level'] = grade_match.group(1)
|
| 33 |
+
courses = re.findall(r'(?i)\b([A-Z][a-zA-Z\s&/]+)\s+(\d{1,3})\b', transcript_text)
|
| 34 |
+
if courses:
|
| 35 |
+
info['Courses'] = list(set([c[0].strip() for c in courses]))
|
| 36 |
+
return info
|
| 37 |
+
|
| 38 |
+
# Learning style questions
|
| 39 |
+
def learning_style_quiz(q1, q2, q3):
|
| 40 |
+
scores = {'visual': 0, 'auditory': 0, 'reading/writing': 0, 'kinesthetic': 0}
|
| 41 |
+
mapping = [q1, q2, q3]
|
| 42 |
+
for answer in mapping:
|
| 43 |
+
scores[answer] += 1
|
| 44 |
+
best = max(scores, key=scores.get)
|
| 45 |
+
return best.capitalize()
|
| 46 |
+
|
| 47 |
+
# Save all answers into profile
|
| 48 |
+
def save_profile(file, q1, q2, q3, about_me, blog_text, blog_opt_in):
|
| 49 |
+
df = parse_transcript(file)
|
| 50 |
+
transcript_info = extract_transcript_info(df)
|
| 51 |
+
learning_type = learning_style_quiz(q1, q2, q3)
|
| 52 |
+
|
| 53 |
+
if not blog_opt_in and blog_text.strip() == "":
|
| 54 |
+
blog_text = "[User chose to skip this section]"
|
| 55 |
+
|
| 56 |
+
profile = {
|
| 57 |
+
"transcript": df.to_dict(orient='records'),
|
| 58 |
+
"transcript_info": transcript_info,
|
| 59 |
+
"learning_style": learning_type,
|
| 60 |
+
"about_me": about_me,
|
| 61 |
+
"blog": blog_text
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
with open("student_profile.json", "w") as f:
|
| 65 |
+
json.dump(profile, f, indent=4)
|
| 66 |
+
|
| 67 |
+
return f"✅ Profile saved! Your learning style is: {learning_type}"
|
| 68 |
+
|
| 69 |
+
# Build Gradio UI
|
| 70 |
+
with gr.Blocks() as demo:
|
| 71 |
+
gr.Markdown("## 🎓 Personalized AI Student Assistant")
|
| 72 |
+
|
| 73 |
+
with gr.Row():
|
| 74 |
+
file = gr.File(label="📄 Upload Your Transcript (.csv, .xlsx, .pdf)")
|
| 75 |
+
|
| 76 |
+
with gr.Column():
|
| 77 |
+
gr.Markdown("### 🧠 Learning Style Discovery")
|
| 78 |
+
q1 = gr.Radio(["visual", "auditory", "reading/writing", "kinesthetic"], label="1. How do you prefer to learn new topics?")
|
| 79 |
+
q2 = gr.Radio(["visual", "auditory", "reading/writing", "kinesthetic"], label="2. How do you remember lists best?")
|
| 80 |
+
q3 = gr.Radio(["visual", "auditory", "reading/writing", "kinesthetic"], label="3. Favorite way to study?")
|
| 81 |
+
|
| 82 |
+
with gr.Column():
|
| 83 |
+
gr.Markdown("### ❤️ About You")
|
| 84 |
+
about_me = gr.Textbox(lines=6, label="Answer a few questions: \n1. What’s a fun fact about you? \n2. Favorite music/artist? \n3. Your dream job?")
|
| 85 |
+
|
| 86 |
+
blog_opt_in = gr.Checkbox(label="I want to write a personal blog for better personalization")
|
| 87 |
+
blog_text = gr.Textbox(lines=5, label="✍️ Optional: Write a mini blog about your life", visible=True)
|
| 88 |
+
|
| 89 |
+
submit = gr.Button("📥 Save My Profile")
|
| 90 |
+
output = gr.Textbox(label="Status")
|
| 91 |
+
|
| 92 |
+
submit.click(fn=save_profile,
|
| 93 |
+
inputs=[file, q1, q2, q3, about_me, blog_text, blog_opt_in],
|
| 94 |
+
outputs=[output])
|
| 95 |
+
|
| 96 |
+
if __name__ == '__main__':
|
| 97 |
+
demo.launch()
|