Spaces:
Runtime error
Runtime error
import gradio as gr | |
import pandas as pd | |
import PyPDF2 | |
import json | |
import re | |
# ------------------------- | |
# 1. Parse uploaded transcript | |
# ------------------------- | |
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 | |
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 | |
# ------------------------- | |
# 2. Learning Style Quiz | |
# ------------------------- | |
learning_style_questions = [ | |
"You are about to give directions to a person. You would:", | |
"You are not sure whether a word should be spelled 'dependent' or 'dependant'. You would:", | |
"You’re planning a vacation for a group. You want people to appreciate it. You would:", | |
"You are going to cook something as a special treat. You would:", | |
"A group of tourists wants to learn about the parks or wildlife. You would:", | |
"You are not sure how a word is spelled. You would:", | |
"You prefer a teacher who likes to use:", | |
"You are trying to remember a phone number. You would:", | |
"You want to learn a new program, game, or app on your phone. You would:" | |
] | |
learning_style_answers = [ | |
["draw a map", "tell them directions", "write down the directions"], | |
["look it up in a dictionary", "sound it out", "write both forms down"], | |
["show them the brochures/pictures", "tell them what's planned", "list the itinerary"], | |
["cook from a recipe", "call a friend", "improvise based on what you know"], | |
["show maps", "talk about nature", "hand out leaflets with info"], | |
["look it up visually", "say it aloud", "write it a few times"], | |
["diagrams/charts", "discussions", "reading material"], | |
["see it in your head", "say it to yourself", "write it down"], | |
["watch tutorials", "talk to someone who knows", "read instructions"] | |
] | |
style_key_map = { | |
0: "visual", | |
1: "auditory", | |
2: "reading/writing" | |
} | |
def learning_style_quiz(*answers): | |
counts = {'visual': 0, 'auditory': 0, 'reading/writing': 0} | |
for ans in answers: | |
counts[ans] += 1 | |
best = max(counts, key=counts.get) | |
return best.capitalize() | |
# ------------------------- | |
# 3. PanoramaEd "Get to Know You" Categories | |
# ------------------------- | |
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?" | |
] | |
} | |
# ------------------------- | |
# 4. Save Profile Logic | |
# ------------------------- | |
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: | |
confirm = gr.Textbox.show(label="Skipping the blog might reduce personalization. Do you really want to skip?") | |
if blog_text.strip() == "": | |
blog_text = "[User chose to skip this section]" | |
responses = dict(zip([q for cat in get_to_know_categories.values() for q in cat], 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}" | |
# ------------------------- | |
# 5. Gradio UI | |
# ------------------------- | |
with gr.Blocks() as demo: | |
gr.Markdown("## 🎓 Build Your Personalized Learning Profile") | |
file = gr.File(label="📤 Upload Your Transcript (.pdf, .csv, .xlsx)") | |
gr.Markdown("### 🧠 Learning Style Questions") | |
quiz_inputs = [] | |
for i, question in enumerate(learning_style_questions): | |
quiz_inputs.append( | |
gr.Radio( | |
choices=["visual", "auditory", "reading/writing"], | |
label=f"{i+1}. {question}" | |
) | |
) | |
gr.Markdown("### ✨ Tell Me About You") | |
about_me = gr.Textbox(lines=3, label="Tell me a fun fact, favorite music, or dream job!") | |
blog_opt_in = gr.Checkbox(label="📝 I want to write a short blog") | |
blog_text = gr.Textbox(lines=5, label="Write a short blog about your life") | |
category_inputs = [] | |
for category, questions in get_to_know_categories.items(): | |
gr.Markdown(f"### 📘 {category}") | |
for question in questions: | |
category_inputs.append(gr.Textbox(label=question)) | |
submit = gr.Button("📥 Save My Profile") | |
output = gr.Textbox(label="✅ Status") | |
submit.click(fn=save_profile, | |
inputs=[file, *quiz_inputs, about_me, blog_opt_in, blog_text, *category_inputs], | |
outputs=[output]) | |
# Run the app | |
if __name__ == '__main__': | |
demo.launch() | |