Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,144 +1,170 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
-
import joblib
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
script_dir = os.path.dirname(os.path.abspath(__file__))
|
8 |
pipeline_path = os.path.join(script_dir, 'toolkit', 'pipeline.joblib')
|
9 |
model_path = os.path.join(script_dir, 'toolkit', 'Random Forest Classifier.joblib')
|
10 |
-
|
11 |
-
# Load transformation pipeline and model
|
12 |
-
pipeline = joblib.load(pipeline_path)
|
13 |
model = joblib.load(model_path)
|
14 |
|
15 |
-
# Create a function to calculate TotalCharges
|
16 |
def calculate_total_charges(tenure, monthly_charges):
|
17 |
return tenure * monthly_charges
|
18 |
|
19 |
-
# Create a function that applies the ML pipeline and makes predictions
|
20 |
def predict(SeniorCitizen, Partner, Dependents, tenure,
|
21 |
InternetService, OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport,
|
22 |
StreamingTV, StreamingMovies, Contract, PaperlessBilling, PaymentMethod,
|
23 |
MonthlyCharges):
|
24 |
-
|
25 |
-
# Calculate TotalCharges
|
26 |
-
TotalCharges = calculate_total_charges(tenure, MonthlyCharges)
|
27 |
|
28 |
-
|
29 |
input_df = pd.DataFrame({
|
30 |
-
'SeniorCitizen': [SeniorCitizen],
|
31 |
-
'
|
32 |
-
'
|
33 |
-
'
|
34 |
-
'
|
35 |
-
'
|
36 |
-
'OnlineBackup': [OnlineBackup],
|
37 |
-
'DeviceProtection': [DeviceProtection],
|
38 |
-
'TechSupport': [TechSupport],
|
39 |
-
'StreamingTV': [StreamingTV],
|
40 |
-
'StreamingMovies': [StreamingMovies],
|
41 |
-
'Contract': [Contract],
|
42 |
-
'PaperlessBilling': [PaperlessBilling],
|
43 |
-
'PaymentMethod': [PaymentMethod],
|
44 |
-
'MonthlyCharges': [MonthlyCharges],
|
45 |
-
'TotalCharges': [TotalCharges]
|
46 |
})
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
X_processed = pipeline.transform(input_df)
|
53 |
-
|
54 |
-
# Extracting feature names for categorical columns after one-hot encoding
|
55 |
-
cat_encoder = pipeline.named_steps['preprocessor'].named_transformers_['cat'].named_steps['onehot']
|
56 |
-
cat_feature_names = cat_encoder.get_feature_names_out(cat_cols)
|
57 |
-
|
58 |
-
# Concatenating numerical and categorical feature names
|
59 |
-
feature_names = num_cols + list(cat_feature_names)
|
60 |
-
|
61 |
-
# Convert X_processed to DataFrame
|
62 |
final_df = pd.DataFrame(X_processed, columns=feature_names)
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
final_df = pd.concat([remaining_columns, first_three_columns], axis=1)
|
68 |
-
|
69 |
-
# Make predictions using the model
|
70 |
-
prediction_probs = model.predict_proba(final_df)[0]
|
71 |
-
prediction_label = {
|
72 |
-
"Prediction: CHURN 🔴": prediction_probs[1],
|
73 |
-
"Prediction: STAY ✅": prediction_probs[0]
|
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 |
-
- TechSupport: Whether the customer has tech support or not (Yes, No, No internet)
|
134 |
-
- StreamingTV: Whether the customer has streaming TV or not (Yes, No, No internet service)
|
135 |
-
- StreamingMovies: Whether the customer has streaming movies or not (Yes, No, No Internet service)
|
136 |
-
- Contract: The contract term of the customer (Month-to-Month, One year, Two year)
|
137 |
-
- PaperlessBilling: Whether the customer has paperless billing or not (Yes, No)
|
138 |
-
- Payment Method: The customer's payment method (Electronic check, mailed check, Bank transfer(automatic), Credit card(automatic))
|
139 |
-
- MonthlyCharges: The amount charged to the customer monthly
|
140 |
-
""")
|
141 |
-
|
142 |
-
predict_btn.click(fn=predict, inputs=input_interface, outputs=output_interface)
|
143 |
-
|
144 |
-
app.launch(share=True)
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
+
import os
|
4 |
import gradio as gr
|
5 |
import pandas as pd
|
6 |
import numpy as np
|
7 |
+
import joblib
|
8 |
+
import spacy
|
9 |
+
|
10 |
+
from langchain_core.pydantic_v1 import BaseModel, Field
|
11 |
+
from langchain.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
|
12 |
+
from langchain.output_parsers import PydanticOutputParser
|
13 |
+
from langchain_openai import ChatOpenAI
|
14 |
+
from transformers import pipeline
|
15 |
+
|
16 |
+
### 1. Translator ###
|
17 |
+
chat = ChatOpenAI()
|
18 |
+
|
19 |
+
class TextTranslator(BaseModel):
|
20 |
+
output: str = Field(description="Translated output")
|
21 |
+
|
22 |
+
output_parser = PydanticOutputParser(pydantic_object=TextTranslator)
|
23 |
+
format_instructions = output_parser.get_format_instructions()
|
24 |
+
|
25 |
+
def text_translator(input_text: str, language: str) -> str:
|
26 |
+
template = """Enter the text that you want to translate:
|
27 |
+
{input_text}, and enter the language that you want it to translate to {language}. {format_instructions}"""
|
28 |
+
human_prompt = HumanMessagePromptTemplate.from_template(template)
|
29 |
+
prompt = ChatPromptTemplate.from_messages([human_prompt]).format_prompt(
|
30 |
+
input_text=input_text, language=language, format_instructions=format_instructions)
|
31 |
+
response = chat(messages=prompt.to_messages())
|
32 |
+
return output_parser.parse(response.content).output
|
33 |
+
|
34 |
+
### 2. Sentiment ###
|
35 |
+
sentiment_model = pipeline("sentiment-analysis", model="cardiffnlp/twitter-xlm-roberta-base-sentiment")
|
36 |
+
def sentiment_analysis(message, history):
|
37 |
+
result = sentiment_model(message)
|
38 |
+
return f"Sentimiento : {result[0]['label']} (Probabilidad: {result[0]['score']:.2f})"
|
39 |
+
|
40 |
+
### 3. Financial Analyst ###
|
41 |
+
nlp = spacy.load('en_core_web_sm')
|
42 |
+
nlp.add_pipe('sentencizer')
|
43 |
+
def split_in_sentences(text):
|
44 |
+
return [str(sent).strip() for sent in nlp(text).sents]
|
45 |
+
|
46 |
+
def make_spans(text, results):
|
47 |
+
labels = [r['label'] for r in results]
|
48 |
+
return list(zip(split_in_sentences(text), labels))
|
49 |
+
|
50 |
+
auth_token = os.environ.get("HF_Token")
|
51 |
+
|
52 |
+
asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
|
53 |
+
def speech_to_text(audio):
|
54 |
+
return asr(audio)["text"]
|
55 |
+
|
56 |
+
summarizer = pipeline("summarization", model="knkarthick/MEETING_SUMMARY")
|
57 |
+
def summarize_text(text):
|
58 |
+
return summarizer(text)[0]['summary_text']
|
59 |
+
|
60 |
+
fin_model = pipeline("sentiment-analysis", model='yiyanghkust/finbert-tone')
|
61 |
+
def text_to_sentiment(text):
|
62 |
+
return fin_model(text)[0]["label"]
|
63 |
+
|
64 |
+
def fin_ner(text):
|
65 |
+
return gr.Interface.load("dslim/bert-base-NER", src='models', use_auth_token=auth_token)(text)
|
66 |
+
|
67 |
+
def fin_ext(text):
|
68 |
+
return make_spans(text, fin_model(split_in_sentences(text)))
|
69 |
+
|
70 |
+
def fls(text):
|
71 |
+
model = pipeline("text-classification", model="demo-org/finbert_fls", tokenizer="demo-org/finbert_fls", use_auth_token=auth_token)
|
72 |
+
return make_spans(text, model(split_in_sentences(text)))
|
73 |
+
|
74 |
+
### 4. Personal Info Detection ###
|
75 |
+
def detect_personal_info(text):
|
76 |
+
model = gr.Interface.load("iiiorg/piiranha-v1-detect-personal-information")
|
77 |
+
return model(text)
|
78 |
+
|
79 |
+
### 5. Customer Churn ###
|
80 |
script_dir = os.path.dirname(os.path.abspath(__file__))
|
81 |
pipeline_path = os.path.join(script_dir, 'toolkit', 'pipeline.joblib')
|
82 |
model_path = os.path.join(script_dir, 'toolkit', 'Random Forest Classifier.joblib')
|
83 |
+
pipeline_model = joblib.load(pipeline_path)
|
|
|
|
|
84 |
model = joblib.load(model_path)
|
85 |
|
|
|
86 |
def calculate_total_charges(tenure, monthly_charges):
|
87 |
return tenure * monthly_charges
|
88 |
|
|
|
89 |
def predict(SeniorCitizen, Partner, Dependents, tenure,
|
90 |
InternetService, OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport,
|
91 |
StreamingTV, StreamingMovies, Contract, PaperlessBilling, PaymentMethod,
|
92 |
MonthlyCharges):
|
|
|
|
|
|
|
93 |
|
94 |
+
TotalCharges = calculate_total_charges(tenure, MonthlyCharges)
|
95 |
input_df = pd.DataFrame({
|
96 |
+
'SeniorCitizen': [SeniorCitizen], 'Partner': [Partner], 'Dependents': [Dependents],
|
97 |
+
'tenure': [tenure], 'InternetService': [InternetService], 'OnlineSecurity': [OnlineSecurity],
|
98 |
+
'OnlineBackup': [OnlineBackup], 'DeviceProtection': [DeviceProtection], 'TechSupport': [TechSupport],
|
99 |
+
'StreamingTV': [StreamingTV], 'StreamingMovies': [StreamingMovies], 'Contract': [Contract],
|
100 |
+
'PaperlessBilling': [PaperlessBilling], 'PaymentMethod': [PaymentMethod],
|
101 |
+
'MonthlyCharges': [MonthlyCharges], 'TotalCharges': [TotalCharges]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
})
|
103 |
|
104 |
+
X_processed = pipeline_model.transform(input_df)
|
105 |
+
cat_encoder = pipeline_model.named_steps['preprocessor'].named_transformers_['cat'].named_steps['onehot']
|
106 |
+
feature_names = [*input_df.select_dtypes(exclude='object').columns, *cat_encoder.get_feature_names_out()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
final_df = pd.DataFrame(X_processed, columns=feature_names)
|
108 |
+
pred_probs = model.predict_proba(final_df)[0]
|
109 |
+
return {
|
110 |
+
"Prediction: CHURN 🔴": pred_probs[1],
|
111 |
+
"Prediction: STAY ✅": pred_probs[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
}
|
113 |
|
114 |
+
### COMBINED UI ###
|
115 |
+
with gr.Blocks() as demo:
|
116 |
+
with gr.Tab("Translator"):
|
117 |
+
gr.Markdown("## Translator")
|
118 |
+
input_text = gr.Textbox(label="Text to Translate")
|
119 |
+
language = gr.Textbox(label="Target Language")
|
120 |
+
output = gr.Textbox(label="Translated Text")
|
121 |
+
gr.Button("Translate").click(text_translator, inputs=[input_text, language], outputs=output)
|
122 |
+
|
123 |
+
with gr.Tab("Sentiment"):
|
124 |
+
gr.Markdown("## Sentiment Analysis")
|
125 |
+
gr.ChatInterface(sentiment_analysis, type="messages")
|
126 |
+
|
127 |
+
with gr.Tab("Financial Analyst"):
|
128 |
+
gr.Markdown("## Financial Analyst")
|
129 |
+
audio = gr.Audio(source="microphone", type="filepath")
|
130 |
+
text_input = gr.Textbox()
|
131 |
+
summary = gr.Textbox()
|
132 |
+
tone_label = gr.Label()
|
133 |
+
gr.Button("Speech to Text").click(speech_to_text, inputs=audio, outputs=text_input)
|
134 |
+
gr.Button("Summarize").click(summarize_text, inputs=text_input, outputs=summary)
|
135 |
+
gr.Button("Classify Tone").click(text_to_sentiment, inputs=summary, outputs=tone_label)
|
136 |
+
gr.HighlightedText(label="Tone").render()
|
137 |
+
gr.HighlightedText(label="Forward-Looking").render()
|
138 |
+
gr.Button("Analyze All").click(fn=fin_ext, inputs=text_input, outputs=None).click(fls, inputs=text_input, outputs=None)
|
139 |
+
gr.Button("Entities").click(fin_ner, inputs=text_input, outputs=None)
|
140 |
+
|
141 |
+
with gr.Tab("Personal Info Detector"):
|
142 |
+
gr.Markdown("## Detect Personal Info")
|
143 |
+
pi_input = gr.Textbox()
|
144 |
+
pi_output = gr.HighlightedText()
|
145 |
+
gr.Button("Detect").click(detect_personal_info, inputs=pi_input, outputs=pi_output)
|
146 |
+
|
147 |
+
with gr.Tab("Customer Churn"):
|
148 |
+
gr.Markdown("## Customer Churn Prediction")
|
149 |
+
inputs = [
|
150 |
+
gr.Radio(["Yes", "No"], label="SeniorCitizen"),
|
151 |
+
gr.Radio(["Yes", "No"], label="Partner"),
|
152 |
+
gr.Radio(["No", "Yes"], label="Dependents"),
|
153 |
+
gr.Slider(1, 73, step=1, label="Tenure"),
|
154 |
+
gr.Radio(["DSL", "Fiber optic", "No Internet"], label="InternetService"),
|
155 |
+
gr.Radio(["No", "Yes"], label="OnlineSecurity"),
|
156 |
+
gr.Radio(["No", "Yes"], label="OnlineBackup"),
|
157 |
+
gr.Radio(["No", "Yes"], label="DeviceProtection"),
|
158 |
+
gr.Radio(["No", "Yes"], label="TechSupport"),
|
159 |
+
gr.Radio(["No", "Yes"], label="StreamingTV"),
|
160 |
+
gr.Radio(["No", "Yes"], label="StreamingMovies"),
|
161 |
+
gr.Radio(["Month-to-month", "One year", "Two year"], label="Contract"),
|
162 |
+
gr.Radio(["Yes", "No"], label="PaperlessBilling"),
|
163 |
+
gr.Radio(["Electronic check", "Mailed check", "Bank transfer (automatic)", "Credit card (automatic)"], label="PaymentMethod"),
|
164 |
+
gr.Slider(18.40, 118.65, label="MonthlyCharges")
|
165 |
+
]
|
166 |
+
churn_output = gr.Label()
|
167 |
+
gr.Button("Predict").click(predict, inputs=inputs, outputs=churn_output)
|
168 |
+
|
169 |
+
if __name__ == "__main__":
|
170 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|