Spaces:
Sleeping
Sleeping
File size: 4,944 Bytes
928f866 |
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
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)
|