Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pandas as pd | |
import joblib | |
from nltk.stem import WordNetLemmatizer | |
from nltk.corpus import stopwords | |
from nltk.tokenize import word_tokenize | |
from sklearn.preprocessing import StandardScaler, OneHotEncoder | |
from sklearn.metrics import classification_report | |
from sklearn.compose import ColumnTransformer | |
from sklearn.impute import SimpleImputer | |
from textblob import TextBlob | |
import nltk | |
nltk.download('punkt') | |
nltk.download('wordnet') | |
import numpy as np | |
# Load pre-trained models and preprocessor objects | |
model = joblib.load('pet_adoption.pkl') | |
preprocessor = joblib.load('preprocessor.pkl') | |
# Lemmatizer for text preprocessing | |
lemmatizer = WordNetLemmatizer() | |
# Function to preprocess text input | |
def preprocess_text(text): | |
text = text.lower() | |
text = text.replace('[^\w\s]','',regex=True) | |
text = text.replace('\s+',' ',regex=True) | |
text = text.replace('\n',' ',regex=True) | |
text = text.replace('\d+',' ',regex=True) | |
stop_words = set(stopwords.words('english')) | |
words = nltk.word_tokenize(text) | |
words = [word for word in words if word not in stop_words] | |
# Lemmatization | |
words = [lemmatizer.lemmatize(word) for word in words] | |
# Join processed words | |
return ' '.join(words) | |
# Duygu analizi fonksiyonu | |
def analyze_sentiment(description): | |
analysis = TextBlob(description) | |
# Sentiment.polarity değeri -1 ile 1 arasında değişir: -1 negatif, 0 nötr, 1 pozitif | |
return analysis.sentiment.polarity | |
def sentiment_to_adoption_speed(sentiment): | |
if sentiment > 0.2: | |
return 2 # Yüksek olumlu duygu | |
elif sentiment < -0.2: | |
return 0 # Yüksek olumsuz duygu | |
else: | |
return 1 # Nötr veya orta duygu | |
# Gradio arayüzü için girdi alanları ve işlemler | |
def predict_adoption_speed(age, breed1, breed2, gender, color1, color2, color3, | |
maturity_size, fur_length, vaccinated, dewormed, | |
sterilized, health, quantity, fee, state, video_amt, | |
photo_amt, description): | |
# Convert input into DataFrame format | |
data = pd.DataFrame({ | |
'Age': [age], | |
'Breed1': [breed1], | |
'Breed2': [breed2], | |
'Gender': [gender], | |
'Color1': [color1], | |
'Color2': [color2], | |
'Color3': [color3], | |
'MaturitySize': [maturity_size], | |
'FurLength': [fur_length], | |
'Vaccinated': [vaccinated], | |
'Dewormed': [dewormed], | |
'Sterilized': [sterilized], | |
'Health': [health], | |
'Quantity': [quantity], | |
'Fee': [fee], | |
'State': [state], | |
'VideoAmt': [video_amt], | |
'PhotoAmt': [photo_amt], | |
'sentiments': [sentiments] | |
}) | |
# Separate numerical and categorical columns | |
num_columns = ['Age', 'Quantity', 'Fee', 'VideoAmt', 'PhotoAmt'] | |
cat_columns = ['Breed1', 'Breed2', 'Gender', 'Color1', 'Color2', 'Color3', | |
'MaturitySize', 'FurLength', 'Vaccinated', 'Dewormed', | |
'Sterilized', 'Health', 'State'] | |
# Preprocess numerical and categorical data | |
processed_data = preprocessor.transform(data) | |
# Convert description text to sentiments | |
cleaned_description = preprocess_text(description) | |
sentiment = analyze_sentiment(cleaned_description) | |
sentiments = sentiment_to_adoption_speed(sentiment) # Placeholder for sentiment analysis | |
# Make prediction | |
prediction = model.predict(processed_data) | |
# Return prediction result | |
return int(prediction[0]) | |
# Gradio arayüzünü oluşturma | |
# Define Gradio Interface | |
iface = gr.Interface( | |
fn=predict_adoption_speed, | |
inputs=[ | |
gr.Slider(minimum=0, maximum=20, label="Age"), | |
gr.Number(label="Breed1"), | |
gr.Number(label="Breed2"), | |
gr.Dropdown(choices=[0, 1, 2], label="Gender"), | |
gr.Dropdown(choices=[1, 2, 3, 4, 5, 6, 7], label="Color1"), | |
gr.Dropdown(choices=[0, 1, 2, 3, 4, 5, 6, 7], label="Color2"), | |
gr.Dropdown(choices=[0, 1, 2, 3, 4, 5, 6, 7], label="Color3"), | |
gr.Dropdown(choices=[0, 1, 2, 3], label="MaturitySize"), | |
gr.Dropdown(choices=[1, 2, 3], label="FurLength"), | |
gr.Dropdown(choices=[0, 1], label="Vaccinated"), | |
gr.Dropdown(choices=[0, 1], label="Dewormed"), | |
gr.Dropdown(choices=[0, 1], label="Sterilized"), | |
gr.Dropdown(choices=[0, 1], label="Health"), | |
gr.Number(label="Quantity"), | |
gr.Number(label="Fee"), | |
gr.Dropdown(choices=[41324, 41325, 41326, 41327, 41330, 41332, 41335, 41336, 41342, 41345, 41361, 41367, 41401, 41415, 41452], label="State"), | |
gr.Slider(minimum=0, maximum=10, label="VideoAmt"), | |
gr.Slider(minimum=0, maximum=10, label="PhotoAmt"), | |
gr.Textbox(label="Description", placeholder="Enter pet description...") | |
], | |
outputs=gr.Textbox(label="Predicted Adoption Speed") | |
) | |
# Launch Gradio Interface | |
iface.launch(share=True) | |