Download app.py from TeamA/README: direct link, hf CLI and curl.
- Browser
- Download file 3.48 kB
-
https://huggingface.co/spaces/TeamA/README/resolve/main/app.py
- Command line
-
hf download hf://spaces/TeamA/README/app.py
-
curl -L -o app.py https://huggingface.co/spaces/TeamA/README/resolve/main/app.py
3.48 kB
| # app.py | |
| import pandas as pd | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| import plotly.express as px | |
| from PIL import Image | |
| import json | |
| import csv | |
| from io import BytesIO | |
| import requests | |
| from sklearn.preprocessing import LabelEncoder | |
| from sklearn.ensemble import RandomForestClassifier | |
| from geopy.distance import geodesic | |
| import osmnx as ox | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.svm import SVC | |
| from sklearn.tree import DecisionTreeClassifier | |
| from flask import Flask, request, render_template | |
| app = Flask(__name__) | |
| # Read data | |
| pd.set_option('display.max_columns', None) | |
| df = pd.read_json("./Tourist_related.json") | |
| # Convert data to CSV | |
| with open('./data.csv', 'w') as data_file: | |
| csv_writer = csv.writer(data_file) | |
| csv_writer.writerow(df.columns) | |
| csv_writer.writerows(df.values) | |
| # Read CSV data | |
| read_dat = pd.read_csv('./data.csv', on_bad_lines='skip') | |
| # Function for data quality test | |
| def data_quality_test(): | |
| check_na = read_dat.isnull().sum() | |
| for features, count in check_na.items(): | |
| print(f'{features}: {count}') | |
| # Function for correlation analysis | |
| def correlation_analysis(): | |
| plt.figure(figsize=(12, 6)) | |
| sns.heatmap(df.corr(), annot=True) | |
| plt.show() | |
| # Geo Plot Visualization Function | |
| def geo_plot_visualization(): | |
| df['lat'] = df['location'].apply(lambda x: x.get('lat', None)) | |
| df['lng'] = df['location'].apply(lambda x: x.get('lng', None)) | |
| color_scale = [(0, 'orange'), (1, 'red')] | |
| fig = px.scatter_mapbox(df, | |
| lat="lat", | |
| lon="lng", | |
| color_continuous_scale=color_scale, | |
| zoom=8, | |
| height=800, | |
| width=800) | |
| fig.update_layout(mapbox_style="open-street-map") | |
| fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0}) | |
| fig.show() | |
| # Function for image category recognition | |
| def predict_image_category(image_url): | |
| # Replace this part with your image classification logic | |
| # For example, you can use a pre-trained deep learning model | |
| # Here, I'm using a placeholder | |
| categories = ['Category A', 'Category B', 'Category C'] | |
| prediction = np.random.choice(categories) | |
| return prediction | |
| # Random Forest Classifier | |
| def train_random_forest(): | |
| # Assuming 'target' is the target variable | |
| X = df.drop('target', axis=1) | |
| y = df['target'] | |
| # Assuming 'categorical_columns' is a list of categorical columns | |
| le = LabelEncoder() | |
| for col in categorical_columns: | |
| X[col] = le.fit_transform(X[col]) | |
| # Create and train the random forest classifier | |
| rf_classifier = RandomForestClassifier() | |
| rf_classifier.fit(X, y) | |
| return rf_classifier | |
| # Home route | |
| def home(): | |
| return render_template('index.html') | |
| # Image upload route | |
| def upload(): | |
| if request.method == 'POST': | |
| file = request.files['file'] | |
| if file: | |
| # Read the uploaded image | |
| img = Image.open(file.stream) | |
| # Display the image | |
| plt.imshow(img) | |
| plt.axis('off') | |
| plt.show() | |
| # Predict the image category | |
| prediction = predict_image_category(file.stream) | |
| return render_template('result.html', prediction=prediction) | |
| # Start the Flask app | |
| if __name__ == '__main__': | |
| app.run(debug=True) |