Zaheer Khan commited on
Commit
c766e3c
·
unverified ·
2 Parent(s): cd6c356 b12e4cb

Merge pull request #1 from Za-heer/master

Browse files

Working on AI & Data Science Assignment

.pylintrc ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [MASTER]
2
+ # Disable specific warnings/errors to make Pylint less strict
3
+ disable=
4
+ C0114, # Missing module docstring
5
+ C0115, # Missing class docstring
6
+ C0116, # Missing function docstring
7
+ R0903, # Too few public methods
8
+ W0311, # Bad indentation
9
+ W0703, # Broad-except
10
+ C0303, # Trailing whitespace
11
+ C0301, # Line too long
12
+
13
+ # Set maximum line length to a reasonable value
14
+ max-line-length=120
15
+
16
+ # Allow fewer checks for small scripts
17
+ min-similarity-lines=6
18
+
19
+ # Ignore minor style issues
20
+ ignore-patterns=.*\.ipynb
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, render_template
2
+ import os
3
+ from transformers import AutoTokenizer, AutoModel
4
+ import torch
5
+ from pylint.lint import Run
6
+ from pylint.reporters.text import TextReporter
7
+ from io import StringIO
8
+
9
+ app = Flask(__name__)
10
+ UPLOAD_FOLDER = 'uploads'
11
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
12
+ app.static_folder = 'static'
13
+
14
+ # Load CodeBERT with error handling
15
+ try:
16
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/codebert-base")
17
+ model = AutoModel.from_pretrained("microsoft/codebert-base")
18
+ except Exception as e:
19
+ print(f"Error loading CodeBERT: {e}")
20
+ tokenizer = None
21
+ model = None
22
+
23
+ def pylint_check(file_path):
24
+ try:
25
+ output = StringIO()
26
+ reporter = TextReporter(output)
27
+ Run([file_path, '--disable=C0114,C0115,W0311,W0703,C0116,R0903,C0303,C0301', '--max-line-length=120'], reporter=reporter, exit=False)
28
+ pylint_output = output.getvalue()
29
+ return pylint_output if pylint_output.strip() else "No critical issues found. Code looks good!"
30
+ except Exception as e:
31
+ return f"Pylint error: {str(e)}"
32
+
33
+ def analyze_code(file_path):
34
+ try:
35
+ # Read the code file
36
+ with open(file_path, 'r', encoding='utf-8') as file:
37
+ code = file.read()
38
+
39
+ # CodeBERT analysis
40
+ codebert_feedback = "CodeBERT not loaded."
41
+ if tokenizer and model:
42
+ inputs = tokenizer(code, return_tensors="pt", truncation=True, max_length=512)
43
+ with torch.no_grad():
44
+ outputs = model(**inputs)
45
+ codebert_feedback = f"Code analyzed with CodeBERT. Length: {len(code)} characters."
46
+
47
+ # Pylint analysis
48
+ pylint_feedback = pylint_check(file_path)
49
+
50
+ # Combine and format feedback
51
+ feedback = f"<h3>Analysis Report</h3><p><strong>CodeBERT Feedback:</strong> {codebert_feedback}</p><p><strong>Pylint Feedback:</strong><br><pre>{pylint_feedback}</pre></p>"
52
+ return feedback
53
+ except Exception as e:
54
+ return f"Error analyzing file: {str(e)}"
55
+
56
+ @app.route('/')
57
+ def index():
58
+ return render_template('upload.html')
59
+
60
+ @app.route('/upload', methods=['POST'])
61
+ def upload_file():
62
+ try:
63
+ if 'file' not in request.files:
64
+ return 'No file uploaded', 400
65
+ file = request.files['file']
66
+ if file.filename == '':
67
+ return 'No file selected', 400
68
+ if file and (file.filename.endswith('.py') or file.filename.endswith('.ipynb')):
69
+ file_path = os.path.abspath(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))
70
+ file.save(file_path)
71
+ feedback = analyze_code(file_path)
72
+ return f'<h2>File {file.filename} uploaded successfully!</h2>{feedback}'
73
+ return 'Invalid file type', 400
74
+ except Exception as e:
75
+ return f'Error during upload: {str(e)}', 500
76
+
77
+ if __name__ == '__main__':
78
+ if not os.path.exists(UPLOAD_FOLDER):
79
+ os.makedirs(UPLOAD_FOLDER)
80
+ app.run(debug=True, port=5000)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ flask==3.0.3
2
+ transformers==4.44.2
3
+ torch==2.4.1
4
+ pandas==2.2.2
5
+ notebook==7.2.2
6
+ pylint==3.2.7
static/style.css ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* General Styling */
2
+ body {
3
+ background-color: #f8f9fa;
4
+ font-family: 'Arial', sans-serif;
5
+ }
6
+
7
+ /* Card Styling for Form and Feedback */
8
+ .card {
9
+ border-radius: 10px;
10
+ background-color: #ffffff;
11
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
12
+ }
13
+
14
+ /* Form Styling */
15
+ .form-label {
16
+ font-weight: bold;
17
+ color: #333;
18
+ }
19
+
20
+ .form-control {
21
+ border: 2px solid #ced4da;
22
+ border-radius: 5px;
23
+ transition: border-color 0.3s;
24
+ }
25
+
26
+ .form-control:focus {
27
+ border-color: #007bff;
28
+ box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
29
+ }
30
+
31
+ .btn-primary {
32
+ background-color: #007bff;
33
+ border: none;
34
+ padding: 10px 20px;
35
+ font-size: 1.1rem;
36
+ border-radius: 5px;
37
+ transition: background-color 0.3s;
38
+ }
39
+
40
+ .btn-primary:hover {
41
+ background-color: #0056b3;
42
+ }
43
+
44
+ /* Feedback Section Styling */
45
+ h2 {
46
+ color: #007bff;
47
+ margin-bottom: 20px;
48
+ text-align: center;
49
+ }
50
+
51
+ h3 {
52
+ color: #333;
53
+ font-size: 1.5rem;
54
+ margin-top: 20px;
55
+ border-bottom: 2px solid #007bff;
56
+ padding-bottom: 5px;
57
+ }
58
+
59
+ pre {
60
+ background-color: #f1f3f5;
61
+ padding: 15px;
62
+ border-radius: 5px;
63
+ font-size: 0.9rem;
64
+ white-space: pre-wrap;
65
+ word-wrap: break-word;
66
+ max-height: 400px;
67
+ overflow-y: auto;
68
+ border: 1px solid #ced4da;
69
+ }
70
+
71
+ /* Responsive Design */
72
+ @media (max-width: 576px) {
73
+ .card {
74
+ padding: 15px;
75
+ }
76
+
77
+ h1 {
78
+ font-size: 1.8rem;
79
+ }
80
+
81
+ .btn-primary {
82
+ width: 100%;
83
+ }
84
+ }
templates/upload.html ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Assignment Checker</title>
8
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
9
+ <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
10
+ </head>
11
+
12
+ <body class="container mt-5">
13
+ <h1 class="text-center mb-4">Assignment Checker</h1>
14
+ <div class="card p-4 shadow">
15
+ <h3>Upload Your Assignment</h3>
16
+ <form method="post" action="/upload" enctype="multipart/form-data">
17
+ <div class="mb-3">
18
+ <label for="file" class="form-label">Select a .py or .ipynb file:</label>
19
+ <input type="file" name="file" accept=".py,.ipynb" class="form-control">
20
+ </div>
21
+ <button type="submit" class="btn btn-primary">Upload</button>
22
+ </form>
23
+ </div>
24
+ </body>
25
+
26
+ </html>
testpylint.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pylint.lint import Run
2
+ from pylint.reporters.text import TextReporter
3
+ from io import StringIO
4
+
5
+ def pylint_check(file_path):
6
+ try:
7
+ output = StringIO()
8
+ reporter = TextReporter(output)
9
+ Run([file_path, '--disable=C0114,C0115,C0116,R0903,W0311,W0703,C0303,C0301', '--max-line-length=120'], reporter=reporter, exit=False)
10
+ return output.getvalue()
11
+ except Exception as e:
12
+ return f"Pylint error: {str(e)}"
13
+
14
+ print(pylint_check("uploads/test.py"))
uploads/assignment_01.ipynb ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "c37acdaf",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Assignment 1: Calculate the Factorial\n",
9
+ " \"Write a function to compute the factorial of a given number n.\n",
10
+ " The factorial of a number n is the product of all positive integers less than or equal to n.\n",
11
+ " For example, factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120."
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "execution_count": 1,
17
+ "id": "ac5b116f",
18
+ "metadata": {},
19
+ "outputs": [
20
+ {
21
+ "name": "stdout",
22
+ "output_type": "stream",
23
+ "text": [
24
+ "Factorial of 5 is 120\n"
25
+ ]
26
+ }
27
+ ],
28
+ "source": [
29
+ "def factorial(n):\n",
30
+ " if n == 0 or n == 1:\n",
31
+ " return 1\n",
32
+ " else:\n",
33
+ " return n * factorial(n - 1)\n",
34
+ " \n",
35
+ " # Test the function\\,\n",
36
+ "number = 5\n",
37
+ "result = factorial(number)\n",
38
+ "print(f'Factorial of {number} is {result}')"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "execution_count": null,
44
+ "id": "80df5cfc",
45
+ "metadata": {},
46
+ "outputs": [],
47
+ "source": []
48
+ }
49
+ ],
50
+ "metadata": {
51
+ "kernelspec": {
52
+ "display_name": "ml_env",
53
+ "language": "python",
54
+ "name": "python3"
55
+ },
56
+ "language_info": {
57
+ "codemirror_mode": {
58
+ "name": "ipython",
59
+ "version": 3
60
+ },
61
+ "file_extension": ".py",
62
+ "mimetype": "text/x-python",
63
+ "name": "python",
64
+ "nbconvert_exporter": "python",
65
+ "pygments_lexer": "ipython3",
66
+ "version": "3.12.2"
67
+ }
68
+ },
69
+ "nbformat": 4,
70
+ "nbformat_minor": 5
71
+ }
uploads/assignment_01.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Assignment 1: Calculate the factorial of a number
2
+ # Write a function to compute the factorial of a given number n
3
+ def factorial(n):
4
+ if n == 0 or n == 1:
5
+ return 1
6
+ else:
7
+ return n * factorial(n - 1)
8
+
9
+ # Test the function
10
+ number = 5
11
+ result = factorial(number)
12
+ print(f"Factorial of {number} is {result}")
uploads/assignment_02.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Assignment 2: Check if a number is prime
2
+ # Write a function to check if a number is prime
3
+
4
+
5
+ def is_prime(n):
6
+ if n <= 1:
7
+ return False
8
+ for i in range(2, n):
9
+ if n % i == 0:
10
+ return False
11
+ return True
12
+
13
+ # Test the function
14
+ num = 17
15
+ print(f"Is {num} prime? {is_prime(num)}")
16
+ print(f"Is 4 prime? {is_prime(4)}")
17
+ print(f"Is {undefined_var} prime?")
uploads/assignment_03.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Assignment 3: EDA on Synthetic Dataset
2
+ # Perform EDA on a dataset of student scores
3
+ import pandas as pd
4
+ import numpy as np
5
+
6
+ # Synthetic dataset
7
+ data = {
8
+ 'name': ['Alice', 'Bob', 'Charlie', 'David'],
9
+ 'math_score': [85, 90, 78, 92],
10
+ 'science_score': [88, 95, 80, 90]
11
+ }
12
+ df = pd.DataFrame(data)
13
+
14
+ # Calculate average scores
15
+ avg_math = df['math_score'].mean()
16
+ avg_science = df['science_score'].mean()
17
+ print(f"Average Math Score: {avg_math}")
18
+ print(f"Average Science Score: {avg_science}")
19
+
20
+ # Plot histogram of math scores
21
+ import matplotlib.pyplot as plt
22
+ plt.hist(df['math_score'], bins=5)
23
+ plt.title('Math Score Distribution')
24
+ plt.xlabel('Score')
25
+ plt.ylabel('Frequency')
26
+ plt.show() # Error: May not display in non-interactive environments
27
+ print(df['english_score']) # Error: 'english_score' column doesn't exist
uploads/assignment_04.ipynb ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "86625251",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Assignment 4: EDA - Correlation Analysis\n",
9
+ "Perform correlation analysis on a dataset of car features and prices.\n",
10
+ "Calculate correlation matrix and plot a heatmap."
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": null,
16
+ "id": "d3ae5847",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "import pandas as pd\n",
21
+ "import seaborn as sns\n",
22
+ "import matplotlib.pyplot as plt\n",
23
+ "\n",
24
+ "# Synthetic dataset\n",
25
+ "data = {\n",
26
+ " 'horsepower': [120, 150, 100, 180],\n",
27
+ " 'weight': [3000, 3200, 2800, 3500],\n",
28
+ " 'price': [20000, 25000, 18000, 30000]\n",
29
+ "}\n",
30
+ "df = pd.DataFrame(data)\n",
31
+ "\n",
32
+ "# Correlation matrix\n",
33
+ "corr_matrix = df.corr()\n",
34
+ "\n",
35
+ "# Plot heatmap\n",
36
+ "sns.heatmap(corr_matrix, annot=True)\n",
37
+ "plt.title('Correlation Heatmap')\n",
38
+ "plt.show()\n",
39
+ "\n",
40
+ "# Error: Incorrect column name\n",
41
+ "print(df['Price'])"
42
+ ]
43
+ }
44
+ ],
45
+ "metadata": {
46
+ "language_info": {
47
+ "name": "python"
48
+ }
49
+ },
50
+ "nbformat": 4,
51
+ "nbformat_minor": 5
52
+ }
uploads/assignment_04.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Assignment 4: Linear Regression on Synthetic Data
2
+ # Build a linear regression model to predict house prices
3
+ import numpy as np
4
+ from sklearn.linear_model import LinearRegression
5
+
6
+ # Synthetic dataset
7
+ X = np.array([[1, 2], [2, 4], [3, 6], [4, 8]]) # Features: size, rooms
8
+ y = np.array([100, 200, 300, 400]) # Prices
9
+
10
+ # Train model
11
+ model = LinearRegression()
12
+ model.fit(X, y)
13
+
14
+ # Predict on new data
15
+ new_data = np.array([5, 10]) # Error: Shape mismatch, should be [[5, 10]]
16
+ print(f"Predicted price: {model.predict(new_data)}")
uploads/assignment_05.ipynb ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "10d2cf4b",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Assignment 5: Decision Tree Classifier\n",
9
+ "Train a decision tree classifier on a synthetic dataset of customer purchases.\n",
10
+ "Predict whether a customer will buy based on age and income."
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": null,
16
+ "id": "10abdab6",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "import pandas as pd\n",
21
+ "from sklearn.tree import DecisionTreeClassifier\n",
22
+ "\n",
23
+ "# Synthetic dataset\n",
24
+ "data = {\n",
25
+ " 'age': [25, 30, 35, 40],\n",
26
+ " 'income': [50000, 60000, 55000, 70000],\n",
27
+ " 'buy': [0, 1, 0, 1]\n",
28
+ "}\n",
29
+ "df = pd.DataFrame(data)\n",
30
+ "\n",
31
+ "# Train model\n",
32
+ "X = df[['age', 'income']]\n",
33
+ "y = df['buy']\n",
34
+ "model = DecisionTreeClassifier()\n",
35
+ "model.fit(X, y) # Error: Missing arguments, should be model.fit(X, y)\\n\",\n",
36
+ "\n",
37
+ "# Predict\\n\",\n",
38
+ "print(model.predict([[30, 65000]]))"
39
+ ]
40
+ }
41
+ ],
42
+ "metadata": {
43
+ "language_info": {
44
+ "name": "python"
45
+ }
46
+ },
47
+ "nbformat": 4,
48
+ "nbformat_minor": 5
49
+ }
uploads/assignment_06.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Assignment 6: Missing Value Imputation
2
+ # Impute missing values in a dataset using mean strategy
3
+ import pandas as pd
4
+ import numpy as np
5
+ from sklearn.impute import SimpleImputer
6
+
7
+ # Synthetic dataset
8
+ data = {
9
+ 'feature1': [1, 2, np.nan, 4],
10
+ 'feature2': [10, np.nan, 30, 40]
11
+ }
12
+ df = pd.DataFrame(data)
13
+
14
+ # Impute missing values
15
+ imputer = SimpleImputer(strategy='mean')
16
+ df_imputed = imputer.fit_transform(df)
17
+
18
+ # Error: Incorrectly printing DataFrame as array
19
+ print(df_imputed['feature1']) # Should use pd.DataFrame(df_imputed)
uploads/assignment_07.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Assignment 7: Feature Engineering - Polynomial Features
2
+ # Create polynomial features for a regression model
3
+ from sklearn.preprocessing import PolynomialFeatures
4
+ import numpy as np
5
+
6
+ # Synthetic dataset
7
+ X = np.array([[1], [2], [3], [4]])
8
+
9
+ # Create polynomial features
10
+ poly = PolynomialFeatures(degree=2)
11
+ X_poly = poly.fit_transform(X)
12
+
13
+ # Error: Incorrectly accessing feature names
14
+ print(poly.feature_names) # Error: Should use get_feature_names_out()
15
+ print(X_poly)
uploads/assignment_09.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Assignment 9: PCA for Dimensionality Reduction
2
+ # Apply PCA to reduce dimensions of a dataset
3
+ from sklearn.decomposition import PCA
4
+ import numpy as np
5
+
6
+ # Synthetic dataset
7
+ X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
8
+
9
+ # Apply PCA
10
+ pca = PCA(n_components=2)
11
+ X_reduced = pca.fit_transform(X)
12
+
13
+ # Error: Accessing undefined attribute
14
+ print(pca.explained_variance) # Should be explained_variance_ratio_
15
+ print(X_reduced)
uploads/test.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # test.py
2
+ def factorial(n):
3
+ if n == 0:
4
+ return 1
5
+ else:
6
+ return n * factorial(n-1)