Spaces:
Runtime error
Runtime error
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 | |
def learning_style_quiz(q1, q2, q3): | |
scores = {'visual': 0, 'auditory': 0, 'reading/writing': 0, 'kinesthetic': 0} | |
mapping = [q1, q2, q3] | |
for answer in mapping: | |
scores[answer] += 1 | |
best = max(scores, key=scores.get) | |
return best.capitalize() | |
# Save all answers into profile | |
def save_profile(file, q1, q2, q3, about_me, blog_text, blog_opt_in): | |
df = parse_transcript(file) | |
transcript_info = extract_transcript_info(df) | |
learning_type = learning_style_quiz(q1, q2, q3) | |
if not blog_opt_in and blog_text.strip() == "": | |
blog_text = "[User chose to skip this section]" | |
profile = { | |
"transcript": df.to_dict(orient='records'), | |
"transcript_info": transcript_info, | |
"learning_style": learning_type, | |
"about_me": about_me, | |
"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") | |
q1 = gr.Radio(["visual", "auditory", "reading/writing", "kinesthetic"], label="1. How do you prefer to learn new topics?") | |
q2 = gr.Radio(["visual", "auditory", "reading/writing", "kinesthetic"], label="2. How do you remember lists best?") | |
q3 = gr.Radio(["visual", "auditory", "reading/writing", "kinesthetic"], label="3. Favorite way to study?") | |
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) | |
submit = gr.Button("π₯ Save My Profile") | |
output = gr.Textbox(label="Status") | |
submit.click(fn=save_profile, | |
inputs=[file, q1, q2, q3, about_me, blog_text, blog_opt_in], | |
outputs=[output]) | |
if __name__ == '__main__': | |
demo.launch() |