File size: 8,103 Bytes
328b44a
1d5a1b0
 
f809b13
1d5a1b0
f809b13
 
794a977
1d5a1b0
 
 
f809b13
 
 
1d5a1b0
f809b13
 
1d5a1b0
f809b13
 
1d5a1b0
f809b13
ef2f775
794a977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c9f1a1
ef2f775
f809b13
794a977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c9f1a1
ef2f775
f809b13
aa96e64
f809b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373d965
f809b13
373d965
f809b13
 
 
373d965
f809b13
 
373d965
f809b13
 
 
 
 
373d965
f809b13
 
373d965
f809b13
 
 
 
 
794a977
 
f809b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
794a977
f809b13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
794a977
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import gradio as gr
import pandas as pd
import json
import os
import re
from PyPDF2 import PdfReader

# ========== TRANSCRIPT PARSING FUNCTIONS (UPDATED) ==========

def parse_transcript(file):
    if file.name.endswith('.csv'):
        df = pd.read_csv(file)
    elif file.name.endswith('.xlsx'):
        df = pd.read_excel(file)
    elif file.name.endswith('.pdf'):
        text = ''
        reader = PdfReader(file)
        for page in reader.pages:
            text += page.extract_text() + '\n'
        return extract_info_from_pdf(text)
    else:
        return "Unsupported file format", None

    # Extract GPA (try multiple possible column names)
    gpa = "N/A"
    for col in ['GPA', 'Grade Point Average', 'Cumulative GPA']:
        if col in df.columns:
            gpa = df[col].iloc[0] if isinstance(df[col].iloc[0], (float, int)) else "N/A"
            break

    # Extract grade level (try multiple possible column names)
    grade_level = "N/A"
    for col in ['Grade Level', 'Grade', 'Class', 'Year']:
        if col in df.columns:
            grade_level = df[col].iloc[0]
            break

    # Extract courses (current and past)
    courses = []
    for col in ['Course', 'Subject', 'Course Name', 'Class']:
        if col in df.columns:
            courses = df[col].tolist()
            break

    # Create output display
    output_text = f"Grade Level: {grade_level}\nGPA: {gpa}\n\nCourses:\n"
    output_text += "\n".join(f"- {course}" for course in courses)

    return output_text, {
        "gpa": gpa,
        "grade_level": grade_level,
        "courses": courses
    }

def extract_info_from_pdf(text):
    # Extract GPA
    gpa_match = re.search(r"(GPA|Grade Point Average)[:\s]+(\d\.\d+)", text, re.IGNORECASE)
    gpa = float(gpa_match.group(2)) if gpa_match else "N/A"

    # Extract grade level
    grade_match = re.search(r"(Grade|Year)[:\s]+(\d+|Freshman|Sophomore|Junior|Senior)", text, re.IGNORECASE)
    grade_level = grade_match.group(2) if grade_match else "N/A"

    # Extract courses - improved pattern to catch more course formats
    course_pattern = r"""
        (?:[A-Z]{2,}\s?\d{3})          # Course codes like 'MATH 101' or 'ENG101'
        |[A-Z][a-z]+(?:\s[A-Z][a-z]+)*  # Or full course names
    """
    courses = re.findall(course_pattern, text, re.VERBOSE)
    courses = list(set(courses))  # Remove duplicates

    # Create output display
    output_text = f"Grade Level: {grade_level}\nGPA: {gpa}\n\nCourses:\n"
    output_text += "\n".join(f"- {course}" for course in courses)

    return output_text, {
        "gpa": gpa,
        "grade_level": grade_level,
        "courses": courses
    }

# ========== LEARNING STYLE QUIZ FUNCTION ==========

def learning_style_quiz(*answers):
    visual = answers.count("I remember something better when I see it written down.")
    auditory = answers.count("I remember best by listening to a lecture or a recording.")
    reading = answers.count("I remember best by reading information on my own.")

    styles = {"Visual": visual, "Auditory": auditory, "Reading/Writing": reading}
    top_styles = [k for k, v in styles.items() if v == max(styles.values())]
    result = ", ".join(top_styles)
    return result

# ========== SAVE STUDENT PROFILE FUNCTION ==========

def save_profile(name, age, interests, transcript, learning_style, favorites, blog):
    data = {
        "name": name,
        "age": age,
        "interests": interests,
        "transcript": transcript,
        "learning_style": learning_style,
        "favorites": favorites,
        "blog": blog
    }
    os.makedirs("student_profiles", exist_ok=True)
    json_path = os.path.join("student_profiles", f"{name.replace(' ', '_')}_profile.json")
    with open(json_path, "w") as f:
        json.dump(data, f, indent=2)

    markdown_summary = f"""### Student Profile: {name}

**Age:** {age}  
**Interests:** {interests}  
**Learning Style:** {learning_style}  

#### Transcript:
{transcript_display(transcript)}

#### Favorites:
- Movie: {favorites['movie']} ({favorites['movie_reason']})
- Show: {favorites['show']} ({favorites['show_reason']})
- Book: {favorites['book']} ({favorites['book_reason']})
- Character: {favorites['character']} ({favorites['character_reason']})

#### Blog:
{blog if blog else "_No blog provided_"}
"""
    return markdown_summary

def transcript_display(transcript_dict):
    if not transcript_dict:
        return "No transcript uploaded."
    return "\n".join([f"- {course}" for course in transcript_dict["courses"]] + 
                     [f"Grade Level: {transcript_dict['grade_level']}", f"GPA: {transcript_dict['gpa']}"])

# ========== GRADIO INTERFACE ==========

with gr.Blocks() as app:
    with gr.Tab("Step 1: Upload Transcript"):
        transcript_file = gr.File(label="Upload your transcript (CSV, Excel, or PDF)")
        transcript_output = gr.Textbox(label="Transcript Output")
        transcript_data = gr.State()
        transcript_file.change(fn=parse_transcript, inputs=transcript_file, outputs=[transcript_output, transcript_data])

    with gr.Tab("Step 2: Learning Style Quiz"):
        q1 = gr.Radio(choices=[
            "I remember something better when I see it written down.",
            "I remember best by listening to a lecture or a recording.",
            "I remember best by reading information on my own."
        ], label="1. How do you best remember information?")
        q2 = gr.Radio(choices=q1.choices, label="2. What’s your preferred study method?")
        q3 = gr.Radio(choices=q1.choices, label="3. What helps you understand new topics?")
        q4 = gr.Radio(choices=q1.choices, label="4. How do you prefer to take notes?")
        q5 = gr.Radio(choices=q1.choices, label="5. When you visualize concepts, what helps most?")
        learning_output = gr.Textbox(label="Learning Style Result")
        gr.Button("Submit Quiz").click(learning_style_quiz, inputs=[q1, q2, q3, q4, q5], outputs=learning_output)

    with gr.Tab("Step 3: Personal Questions"):
        name = gr.Textbox(label="What's your name?")
        age = gr.Number(label="How old are you?")
        interests = gr.Textbox(label="What are your interests?")
        movie = gr.Textbox(label="Favorite movie?")
        movie_reason = gr.Textbox(label="Why do you like that movie?")
        show = gr.Textbox(label="Favorite TV show?")
        show_reason = gr.Textbox(label="Why do you like that show?")
        book = gr.Textbox(label="Favorite book?")
        book_reason = gr.Textbox(label="Why do you like that book?")
        character = gr.Textbox(label="Favorite character?")
        character_reason = gr.Textbox(label="Why do you like that character?")
        blog_checkbox = gr.Checkbox(label="Do you want to write a blog?", value=False)
        blog_text = gr.Textbox(label="Write your blog here", visible=False, lines=5)
        blog_checkbox.change(fn=lambda x: gr.update(visible=x), inputs=blog_checkbox, outputs=blog_text)

    with gr.Tab("Step 4: Save & Review"):
        output_summary = gr.Markdown()
        save_btn = gr.Button("Save Profile")

        def gather_and_save(name, age, interests, movie, movie_reason, show, show_reason,
                            book, book_reason, character, character_reason, blog, transcript, learning_style):
            favorites = {
                "movie": movie,
                "movie_reason": movie_reason,
                "show": show,
                "show_reason": show_reason,
                "book": book,
                "book_reason": book_reason,
                "character": character,
                "character_reason": character_reason,
            }
            return save_profile(name, age, interests, transcript, learning_style, favorites, blog)

        save_btn.click(fn=gather_and_save, 
                       inputs=[name, age, interests, movie, movie_reason, show, show_reason,
                               book, book_reason, character, character_reason, blog_text,
                               transcript_data, learning_output],
                       outputs=output_summary)

app.launch()