aimodelcollab / app.py
abedhyieaw's picture
Update app.py
3189113 verified
Raw History Blame Contribute Delete
3.34 kB
import os
os.system('pip install scikit-learn')
os.system('pip install gradio')
import pandas as pd
import numpy as np
import gradio as gr
from pandas.core.frame import DataFrame
df = pd.read_csv("books.csv")
print(df)
unnecessary_columns = [ "isbn13", "isbn10", "subtitle", "ratings_count"]
df = df.drop(columns=unnecessary_columns)
print(df.head())
df.dropna(how="all")
# Define a function to recommend books based on user input
def recommend_books(user_input):
# Simple recommendation logic: filter by book title containing the user's input
recommendations = df[df['title'].str.contains(user_input, case=False, na=False)].head()
if not recommendations.empty:
return recommendations[['title', 'author']].to_dict(orient='records')
else:
return "Sorry, no books found."
# Create the Gradio chatbot interface
def chatbot(user_input):
return recommend_books(user_input)
# Set up the Gradio interface
interface = gr.Interface(
fn=chatbot, # The function to call
inputs="text", # The input type (text for a chatbot)
outputs="json", # The output type (JSON for structured data)
title="Book Recommender Chatbot"
)
# Define a function to try launching Gradio on different ports
def try_ports(start_port=7860, end_port=8000):
for port in range(start_port, end_port):
try:
# Attempt to launch Gradio on the current port
interface.launch(server_port=port)
break # If successful, break out of the loop
except OSError:
print(f"Port {port} is unavailable, trying next port...")
# Call the function to try launching on ports 5000 to 5100
try_ports(7860, 8000)
interface = gr.Interface.load("aimdoel")
interface.launch(share=True)
from sklearn import datasets
X, y = datasets.load_iris(return_X_y=True)
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import KFold, cross_val_score
clf = DecisionTreeClassifier(random_state=42)
k_folds = KFold(n_splits = 5)
scores = cross_val_score(clf, X, y, cv = k_folds)
print("Cross Validation Scores: ", scores)
print("Average CV Score: ", scores.mean())
print("Number of CV Scores used in Average: ", len(scores))
from sklearn.linear_model import Ridge
from sklearn.model_selection import KFold
os.system('pip install yellowbrick')
from yellowbrick.datasets import load_energy
from yellowbrick.model_selection import CVScores
cv = KFold(n_splits=12, shuffle=True, random_state=42)
model = Ridge()
visualizer = CVScores(model, cv=cv, scoring='r2')
visualizer.fit(X, y) # Fit the data to the visualizer
visualizer.show() # Finalize and render the figure
from sklearn.model_selection import StratifiedKFold
from sklearn.naive_bayes import MultinomialNB
from yellowbrick.datasets import load_occupancy
from yellowbrick.model_selection import CVScores
# Load the classification dataset
X, y = load_occupancy()
# Create a cross-validation strategy
cv = StratifiedKFold(n_splits=12, shuffle=True, random_state=42)
# Instantiate the classification model and visualizer
model = MultinomialNB()
visualizer = CVScores(model, cv=cv, scoring='f1_weighted')
visualizer.fit(X, y) # Fit the data to the visualizer
visualizer.show() # Finalize and render the figure