import gradio as gr import pandas as pd import PyPDF2 import json import re # Parse uploaded transcript file def parse_transcript(file): if file.name.endswith('.csv'): df = pd.read_csv(file.name) elif file.name.endswith(('.xls', '.xlsx')): df = pd.read_excel(file.name) elif file.name.endswith('.pdf'): reader = PyPDF2.PdfReader(file) text = "" for page in reader.pages: text += page.extract_text() or "" df = pd.DataFrame({'Transcript_Text': [text]}) else: raise ValueError("Unsupported file format. Use .csv, .xlsx, or .pdf") return df # Extract student info def extract_transcript_info(df): transcript_text = df['Transcript_Text'].iloc[0] if 'Transcript_Text' in df.columns else '' info = {} gpa_match = re.search(r'(GPA|Grade Point Average)[^\d]*(\d+\.\d+)', transcript_text, re.IGNORECASE) if gpa_match: info['GPA'] = gpa_match.group(2) grade_match = re.search(r'Grade:?\s*(\d{1,2})', transcript_text, re.IGNORECASE) if grade_match: info['Grade_Level'] = grade_match.group(1) courses = re.findall(r'(?i)\b([A-Z][a-zA-Z\s&/]+)\s+(\d{1,3})\b', transcript_text) if courses: info['Courses'] = list(set([c[0].strip() for c in courses])) return info # Learning style questions - from educationplanner.org learning_style_questions = [ "When you are learning something new, you prefer to:", "When you are at home, you like to:", "When you spell a word, you remember it by:", "When you read, you:", "When you write, you:", "When you listen to music, you:", "When you work at solving a problem, you:", "When you give someone directions, you:", "When you are concentrating, you:", "When you meet someone new, you remember them by:" ] learning_style_answers = [ ["Watch a demonstration", "Listen to instructions", "Read about it"], ["Watch TV or play games", "Listen to music or talk", "Read books or write"], ["Seeing the word", "Hearing the word", "Writing the word"], ["See the characters in your mind", "Hear the words in your mind", "Focus on the meaning of the words"], ["Prefer diagrams or charts", "Prefer to discuss ideas", "Prefer to write detailed explanations"], ["Enjoy the sound and rhythm", "Remember lyrics easily", "Analyze the meaning"], ["Visualize the problem", "Talk it through", "Write out steps"], ["Use maps or draw it", "Give verbal directions", "Write down directions"], ["Create mental pictures", "Repeat things aloud", "Take notes or read quietly"], ["Facial features", "Voice or name", "Spelling or written form"] ] style_count_map = { 0: 'visual', 1: 'auditory', 2: 'reading/writing' } # Quiz logic to analyze learning style def learning_style_quiz(*answers): scores = {'visual': 0, 'auditory': 0, 'reading/writing': 0} for ans in answers: scores[ans] += 1 best = max(scores, key=scores.get) return best.capitalize() # PanoramaEd categories and multiple choice questions get_to_know_categories = { "All About Me": [ ("What’s your favorite way to spend a day off?", []), ("If you could only eat one food for the rest of your life, what would it be?", []), ("Do you have any pets? If so, what are their names?", []), ("If you could travel anywhere in the world, where would you go?", []), ("What’s your favorite holiday or tradition?", []) ], "Hopes and Dreams": [ ("What do you want to be when you grow up?", []), ("What’s something you hope to achieve this year?", []), ("If you could change the world in one way, what would you do?", []), ("What are you most proud of?", []), ("What’s a big dream you have for your future?", []) ], "School Life": [ ("What’s your favorite subject in school?", []), ("What’s something that makes learning easier for you?", []), ("Do you prefer working alone or in groups?", []), ("What helps you feel confident in class?", []), ("What’s something you’re good at in school?", []) ], "Relationships": [ ("Who do you look up to and why?", []), ("Who is someone that makes you feel safe and supported?", []), ("Do you have a best friend? What do you like to do together?", []), ("What’s one thing you wish people knew about you?", []), ("What’s something kind you’ve done for someone else?", []) ] } # Save all answers into profile def save_profile(file, *inputs): quiz_answers = inputs[:len(learning_style_questions)] about_me = inputs[len(learning_style_questions)] blog_opt_in = inputs[len(learning_style_questions)+1] blog_text = inputs[len(learning_style_questions)+2] category_answers = inputs[len(learning_style_questions)+3:] df = parse_transcript(file) transcript_info = extract_transcript_info(df) learning_type = learning_style_quiz(*quiz_answers) if not blog_opt_in and blog_text.strip() == "": blog_text = "[User chose to skip this section]" question_texts = [q for cat in get_to_know_categories.values() for q, _ in cat] responses = dict(zip(question_texts, category_answers)) profile = { "transcript": df.to_dict(orient='records'), "transcript_info": transcript_info, "learning_style": learning_type, "about_me": about_me, "get_to_know_answers": responses, "blog": blog_text } with open("student_profile.json", "w") as f: json.dump(profile, f, indent=4) return f"✅ Profile saved! Your learning style is: {learning_type}" # Build Gradio UI with gr.Blocks() as demo: gr.Markdown("## 🎓 Personalized AI Student Assistant") with gr.Row(): file = gr.File(label="📄 Upload Your Transcript (.csv, .xlsx, .pdf)") with gr.Column(): gr.Markdown("### 🧠 Learning Style Discovery") quiz_components = [] for i, (question, options) in enumerate(zip(learning_style_questions, learning_style_answers)): quiz_components.append(gr.Radio( choices=options, label=f"{i+1}. {question}" )) with gr.Column(): gr.Markdown("### ❤️ About You") 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?") blog_opt_in = gr.Checkbox(label="I want to write a personal blog for better personalization") blog_text = gr.Textbox(lines=5, label="✍️ Optional: Write a mini blog about your life", visible=True) category_inputs = [] for category, questions in get_to_know_categories.items(): gr.Markdown(f"### 📘 {category}") for q_text, _ in questions: category_inputs.append(gr.Textbox(label=q_text)) submit = gr.Button("📥 Save My Profile") output = gr.Textbox(label="Status") submit.click(fn=save_profile, inputs=[file, *quiz_components, about_me, blog_opt_in, blog_text, *category_inputs], outputs=[output]) if __name__ == '__main__': demo.launch()