Spaces:
Sleeping
Sleeping
File size: 3,335 Bytes
e05f745 3189113 e05f745 3189113 e05f745 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | 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 |