Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +137 -0
- disaster_model.pkl +3 -0
- pet_adoption.pkl +3 -0
- preprocessor.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import joblib
|
4 |
+
from nltk.stem import WordNetLemmatizer
|
5 |
+
from nltk.corpus import stopwords
|
6 |
+
from nltk.tokenize import word_tokenize
|
7 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
8 |
+
from sklearn.metrics import classification_report
|
9 |
+
from sklearn.compose import ColumnTransformer
|
10 |
+
from sklearn.impute import SimpleImputer
|
11 |
+
from textblob import TextBlob
|
12 |
+
import nltk
|
13 |
+
nltk.download('punkt')
|
14 |
+
nltk.download('wordnet')
|
15 |
+
import numpy as np
|
16 |
+
|
17 |
+
# Load pre-trained models and preprocessor objects
|
18 |
+
model = joblib.load('pet_adoption.pkl')
|
19 |
+
preprocessor = joblib.load('preprocessor.pkl')
|
20 |
+
|
21 |
+
# Lemmatizer for text preprocessing
|
22 |
+
lemmatizer = WordNetLemmatizer()
|
23 |
+
|
24 |
+
# Function to preprocess text input
|
25 |
+
def preprocess_text(text):
|
26 |
+
text = text.lower()
|
27 |
+
text = text.replace('[^\w\s]','',regex=True)
|
28 |
+
text = text.replace('\s+',' ',regex=True)
|
29 |
+
text = text.replace('\n',' ',regex=True)
|
30 |
+
text = text.replace('\d+',' ',regex=True)
|
31 |
+
stop_words = set(stopwords.words('english'))
|
32 |
+
words = nltk.word_tokenize(text)
|
33 |
+
words = [word for word in words if word not in stop_words]
|
34 |
+
|
35 |
+
# Lemmatization
|
36 |
+
words = [lemmatizer.lemmatize(word) for word in words]
|
37 |
+
# Join processed words
|
38 |
+
return ' '.join(words)
|
39 |
+
|
40 |
+
|
41 |
+
# Duygu analizi fonksiyonu
|
42 |
+
def analyze_sentiment(description):
|
43 |
+
analysis = TextBlob(description)
|
44 |
+
# Sentiment.polarity değeri -1 ile 1 arasında değişir: -1 negatif, 0 nötr, 1 pozitif
|
45 |
+
return analysis.sentiment.polarity
|
46 |
+
|
47 |
+
def sentiment_to_adoption_speed(sentiment):
|
48 |
+
if sentiment > 0.2:
|
49 |
+
return 2 # Yüksek olumlu duygu
|
50 |
+
elif sentiment < -0.2:
|
51 |
+
return 0 # Yüksek olumsuz duygu
|
52 |
+
else:
|
53 |
+
return 1 # Nötr veya orta duygu
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
# Gradio arayüzü için girdi alanları ve işlemler
|
58 |
+
def predict_adoption_speed(age, breed1, breed2, gender, color1, color2, color3,
|
59 |
+
maturity_size, fur_length, vaccinated, dewormed,
|
60 |
+
sterilized, health, quantity, fee, state, video_amt,
|
61 |
+
photo_amt, description):
|
62 |
+
|
63 |
+
# Convert input into DataFrame format
|
64 |
+
data = pd.DataFrame({
|
65 |
+
'Age': [age],
|
66 |
+
'Breed1': [breed1],
|
67 |
+
'Breed2': [breed2],
|
68 |
+
'Gender': [gender],
|
69 |
+
'Color1': [color1],
|
70 |
+
'Color2': [color2],
|
71 |
+
'Color3': [color3],
|
72 |
+
'MaturitySize': [maturity_size],
|
73 |
+
'FurLength': [fur_length],
|
74 |
+
'Vaccinated': [vaccinated],
|
75 |
+
'Dewormed': [dewormed],
|
76 |
+
'Sterilized': [sterilized],
|
77 |
+
'Health': [health],
|
78 |
+
'Quantity': [quantity],
|
79 |
+
'Fee': [fee],
|
80 |
+
'State': [state],
|
81 |
+
'VideoAmt': [video_amt],
|
82 |
+
'PhotoAmt': [photo_amt],
|
83 |
+
'sentiments': [sentiments]
|
84 |
+
})
|
85 |
+
|
86 |
+
# Separate numerical and categorical columns
|
87 |
+
num_columns = ['Age', 'Quantity', 'Fee', 'VideoAmt', 'PhotoAmt']
|
88 |
+
cat_columns = ['Breed1', 'Breed2', 'Gender', 'Color1', 'Color2', 'Color3',
|
89 |
+
'MaturitySize', 'FurLength', 'Vaccinated', 'Dewormed',
|
90 |
+
'Sterilized', 'Health', 'State']
|
91 |
+
|
92 |
+
# Preprocess numerical and categorical data
|
93 |
+
processed_data = preprocessor.transform(data)
|
94 |
+
|
95 |
+
# Convert description text to sentiments
|
96 |
+
cleaned_description = preprocess_text(description)
|
97 |
+
sentiment = analyze_sentiment(cleaned_description)
|
98 |
+
sentiments = sentiment_to_adoption_speed(sentiment) # Placeholder for sentiment analysis
|
99 |
+
|
100 |
+
# Make prediction
|
101 |
+
prediction = model.predict(processed_data)
|
102 |
+
|
103 |
+
# Return prediction result
|
104 |
+
return int(prediction[0])
|
105 |
+
|
106 |
+
# Gradio arayüzünü oluşturma
|
107 |
+
|
108 |
+
# Define Gradio Interface
|
109 |
+
iface = gr.Interface(
|
110 |
+
fn=predict_adoption_speed,
|
111 |
+
inputs=[
|
112 |
+
gr.Slider(minimum=0, maximum=20, default=1, label="Age"),
|
113 |
+
gr.Number(label="Breed1"),
|
114 |
+
gr.Number(label="Breed2"),
|
115 |
+
gr.Dropdown(choices=[0, 1, 2], label="Gender"),
|
116 |
+
gr.Dropdown(choices=[1, 2, 3, 4, 5, 6, 7], label="Color1"),
|
117 |
+
gr.Dropdown(choices=[0, 1, 2, 3, 4, 5, 6, 7], label="Color2"),
|
118 |
+
gr.Dropdown(choices=[0, 1, 2, 3, 4, 5, 6, 7], label="Color3"),
|
119 |
+
gr.Dropdown(choices=[0, 1, 2, 3], label="MaturitySize"),
|
120 |
+
gr.Dropdown(choices=[1, 2, 3], label="FurLength"),
|
121 |
+
gr.Dropdown(choices=[0, 1], label="Vaccinated"),
|
122 |
+
gr.Dropdown(choices=[0, 1], label="Dewormed"),
|
123 |
+
gr.Dropdown(choices=[0, 1], label="Sterilized"),
|
124 |
+
gr.Dropdown(choices=[0, 1], label="Health"),
|
125 |
+
gr.Number(label="Quantity"),
|
126 |
+
gr.Number(label="Fee"),
|
127 |
+
gr.Dropdown(choices=[41324, 41325, 41326, 41327, 41330, 41332, 41335, 41336, 41342, 41345, 41361, 41367, 41401, 41415, 41452], label="State"),
|
128 |
+
gr.Slider(minimum=0, maximum=10, default=0, label="VideoAmt"),
|
129 |
+
gr.Slider(minimum=0, maximum=10, default=0, label="PhotoAmt"),
|
130 |
+
gr.Textbox(label="Description", placeholder="Enter pet description...")
|
131 |
+
],
|
132 |
+
outputs=gr.outputs.Textbox(label="Predicted Adoption Speed")
|
133 |
+
)
|
134 |
+
|
135 |
+
# Launch Gradio Interface
|
136 |
+
iface.launch(share=True)
|
137 |
+
|
disaster_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e24f49b1850c7d28a654c0b21f70213ed405d47520e53601b8fa6e8de258df55
|
3 |
+
size 602806
|
pet_adoption.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1fc0db89c54fa4101b3cab630632994ca09b0fef9cd23281188f427fe0979255
|
3 |
+
size 590845
|
preprocessor.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:cc23657bb8f71c44eb4d8598bcdcaa98b9941b8a6a62277d72acd7f4947e35be
|
3 |
+
size 6451
|