Final_project / app.py
Dannyar608's picture
Update app.py
794a977 verified
raw
history blame
8.1 kB
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()