Anupam251272's picture
Create app.py
515bd6c verified
raw
history blame
7.34 kB
import gradio as gr
import pandas as pd
import torch
from transformers import BertTokenizer, BertModel
import numpy as np
from sklearn.preprocessing import StandardScaler
import logging
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class EasyLearningPlatform:
def __init__(self):
self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
logger.info(f"Using device: {self.device}")
self.initialize_models()
def initialize_models(self):
"""Initialize BERT model for processing"""
try:
self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
self.model = BertModel.from_pretrained('bert-base-uncased').to(self.device)
except Exception as e:
logger.error(f"Error initializing models: {str(e)}")
raise
def process_learning_request(
self,
name: str,
age: int,
education_level: str,
interests: str,
learning_goal: str,
preferred_learning_style: str,
available_hours_per_week: int
):
"""Process user input and generate learning recommendations"""
try:
# Create user profile
profile = {
'name': name,
'age': age,
'education': education_level,
'interests': interests,
'goal': learning_goal,
'learning_style': preferred_learning_style,
'hours_available': available_hours_per_week
}
# Generate recommendations based on profile
recommendations = self.generate_recommendations(profile)
# Create response
return {
"status": "Success",
"personal_learning_path": recommendations['learning_path'],
"estimated_completion_time": recommendations['completion_time'],
"recommended_resources": recommendations['resources'],
"next_steps": recommendations['next_steps']
}
except Exception as e:
logger.error(f"Error processing request: {str(e)}")
return {
"status": "Error",
"message": "There was an error processing your request. Please try again."
}
def generate_recommendations(self, profile):
"""Generate personalized learning recommendations"""
# Simplified recommendation logic
learning_styles = {
'visual': ['video tutorials', 'infographics', 'diagrams'],
'auditory': ['podcasts', 'audio books', 'lectures'],
'reading/writing': ['textbooks', 'articles', 'written guides'],
'kinesthetic': ['practical exercises', 'hands-on projects', 'interactive tutorials']
}
# Get recommended resources based on learning style
preferred_resources = learning_styles.get(
profile['learning_style'].lower(),
learning_styles['visual'] # default to visual if style not found
)
# Calculate estimated completion time (simplified)
weekly_hours = min(max(profile['hours_available'], 1), 168) # Limit between 1 and 168 hours
estimated_weeks = 12 # Default to 12-week program
return {
'learning_path': [
f"Week 1-2: Introduction to {profile['goal']}",
f"Week 3-4: Fundamental Concepts",
f"Week 5-8: Core Skills Development",
f"Week 9-12: Advanced Topics and Projects"
],
'completion_time': f"{estimated_weeks} weeks at {weekly_hours} hours per week",
'resources': preferred_resources,
'next_steps': [
"1. Review your personalized learning path",
"2. Schedule your study time",
"3. Start with the recommended resources",
"4. Track your progress weekly"
]
}
def create_interface(self):
"""Create the Gradio interface"""
# Define the interface
iface = gr.Interface(
fn=self.process_learning_request,
inputs=[
gr.Textbox(label="Name"),
gr.Number(label="Age", minimum=1, maximum=120),
gr.Dropdown(
choices=[
"High School",
"Bachelor's Degree",
"Master's Degree",
"PhD",
"Other"
],
label="Education Level"
),
gr.Textbox(
label="Interests",
placeholder="e.g., programming, data science, web development"
),
gr.Textbox(
label="Learning Goal",
placeholder="What do you want to learn?"
),
gr.Dropdown(
choices=[
"Visual",
"Auditory",
"Reading/Writing",
"Kinesthetic"
],
label="Preferred Learning Style",
info="How do you learn best?"
),
gr.Slider(
minimum=1,
maximum=40,
value=10,
label="Available Hours per Week",
info="How many hours can you dedicate to learning each week?"
)
],
outputs=gr.JSON(label="Your Personalized Learning Plan"),
title="AI Learning Path Generator",
description="""
Welcome to your personalized learning journey!
Fill in your information below to get a customized learning path:
1. Enter your basic information
2. Specify your learning goals
3. Choose your preferred learning style
4. Set your weekly time commitment
Contact-: AJoshi 91-8847374914 email [email protected]
Click submit to generate your personalized learning plan!
""",
examples=[
[
"John Doe",
25,
"Bachelor's Degree",
"Machine Learning, Python",
"Learn Data Science",
"Visual",
10
],
[
"Jane Smith",
30,
"Master's Degree",
"Web Development, JavaScript",
"Full Stack Development",
"Kinesthetic",
15
]
]
)
return iface
def main():
# Create and launch the platform
platform = EasyLearningPlatform()
interface = platform.create_interface()
interface.launch(share=True)
if __name__ == "__main__":
"""
# Run these commands in Google Colab first:
!pip install gradio transformers torch numpy pandas scikit-learn
"""
main()