Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,170 +1,92 @@
|
|
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
|
9 |
-
|
10 |
-
from
|
11 |
-
from langchain.
|
12 |
-
from langchain.
|
13 |
from langchain_openai import ChatOpenAI
|
14 |
-
from
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
def
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
with gr.Blocks() as demo:
|
116 |
with gr.Tab("Translator"):
|
117 |
-
gr.
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
gr.Button("Translate").click(text_translator, inputs=[input_text, language], outputs=output)
|
122 |
|
123 |
-
with gr.Tab("Sentiment"):
|
124 |
-
gr.
|
125 |
-
gr.
|
|
|
|
|
126 |
|
127 |
with gr.Tab("Financial Analyst"):
|
128 |
-
gr.
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
gr.
|
136 |
-
gr.
|
137 |
-
gr.
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
gr.
|
143 |
-
|
144 |
-
|
145 |
-
gr.
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
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()
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import joblib
|
3 |
+
import re
|
4 |
+
import pandas as pd
|
5 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
6 |
+
from langchain.chains import LLMChain
|
7 |
+
from langchain.prompts import PromptTemplate
|
8 |
from langchain_openai import ChatOpenAI
|
9 |
+
from langchain_core.output_parsers import StrOutputParser
|
10 |
+
|
11 |
+
# 1. Translator
|
12 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-fr")
|
13 |
+
|
14 |
+
def translate_text(text):
|
15 |
+
return translator(text)[0]['translation_text']
|
16 |
+
|
17 |
+
# 2. Sentiment Analysis
|
18 |
+
sentiment = pipeline("sentiment-analysis")
|
19 |
+
|
20 |
+
def analyze_sentiment(text):
|
21 |
+
return sentiment(text)[0]
|
22 |
+
|
23 |
+
# 3. Financial Analyst (LangChain with OpenAI, requires API key)
|
24 |
+
def financial_analysis(text, api_key):
|
25 |
+
chat = ChatOpenAI(api_key=api_key)
|
26 |
+
template = "Analyze the financial context of this text:\n\n{text}"
|
27 |
+
prompt = PromptTemplate.from_template(template)
|
28 |
+
chain = LLMChain(llm=chat, prompt=prompt, output_parser=StrOutputParser())
|
29 |
+
return chain.run({"text": text})
|
30 |
+
|
31 |
+
# 4. Personal Info Detection
|
32 |
+
def detect_pii(text):
|
33 |
+
pii_patterns = {
|
34 |
+
"email": r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+",
|
35 |
+
"phone": r"\+?\d[\d\-\s]{8,}\d",
|
36 |
+
"credit_card": r"\b(?:\d[ -]*?){13,16}\b"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
}
|
38 |
+
found = {}
|
39 |
+
for label, pattern in pii_patterns.items():
|
40 |
+
matches = re.findall(pattern, text)
|
41 |
+
if matches:
|
42 |
+
found[label] = matches
|
43 |
+
return found or "No personal information found."
|
44 |
+
|
45 |
+
# 5. Telco Customer Churn Prediction
|
46 |
+
model = joblib.load("model.joblib")
|
47 |
+
def churn_prediction(gender, SeniorCitizen, Partner, tenure, MonthlyCharges):
|
48 |
+
input_df = pd.DataFrame([[gender, SeniorCitizen, Partner, tenure, MonthlyCharges]],
|
49 |
+
columns=["gender", "SeniorCitizen", "Partner", "tenure", "MonthlyCharges"])
|
50 |
+
prediction = model.predict(input_df)[0]
|
51 |
+
return "Churn" if prediction == 1 else "Not Churn"
|
52 |
+
|
53 |
+
# Gradio UI setup
|
54 |
with gr.Blocks() as demo:
|
55 |
with gr.Tab("Translator"):
|
56 |
+
input_text = gr.Textbox(label="Input Text")
|
57 |
+
output_text = gr.Textbox(label="Translated Text")
|
58 |
+
translate_button = gr.Button("Translate")
|
59 |
+
translate_button.click(fn=translate_text, inputs=input_text, outputs=output_text)
|
|
|
60 |
|
61 |
+
with gr.Tab("Sentiment Analysis"):
|
62 |
+
sentiment_input = gr.Textbox(label="Text")
|
63 |
+
sentiment_output = gr.Textbox(label="Sentiment")
|
64 |
+
sentiment_button = gr.Button("Analyze")
|
65 |
+
sentiment_button.click(fn=analyze_sentiment, inputs=sentiment_input, outputs=sentiment_output)
|
66 |
|
67 |
with gr.Tab("Financial Analyst"):
|
68 |
+
finance_input = gr.Textbox(label="Financial Text")
|
69 |
+
api_key_input = gr.Textbox(label="OpenAI API Key", type="password")
|
70 |
+
finance_output = gr.Textbox(label="Analysis")
|
71 |
+
finance_button = gr.Button("Analyze")
|
72 |
+
finance_button.click(fn=financial_analysis, inputs=[finance_input, api_key_input], outputs=finance_output)
|
73 |
+
|
74 |
+
with gr.Tab("PII Detector"):
|
75 |
+
pii_input = gr.Textbox(label="Text")
|
76 |
+
pii_output = gr.JSON(label="Detected PII")
|
77 |
+
pii_button = gr.Button("Detect")
|
78 |
+
pii_button.click(fn=detect_pii, inputs=pii_input, outputs=pii_output)
|
79 |
+
|
80 |
+
with gr.Tab("Telco Churn Predictor"):
|
81 |
+
gender = gr.Dropdown(choices=["Male", "Female"], label="Gender")
|
82 |
+
senior = gr.Dropdown(choices=[0, 1], label="Senior Citizen")
|
83 |
+
partner = gr.Dropdown(choices=["Yes", "No"], label="Partner")
|
84 |
+
tenure = gr.Number(label="Tenure (months)")
|
85 |
+
charges = gr.Number(label="Monthly Charges")
|
86 |
+
churn_output = gr.Textbox(label="Prediction")
|
87 |
+
churn_button = gr.Button("Predict")
|
88 |
+
churn_button.click(fn=churn_prediction,
|
89 |
+
inputs=[gender, senior, partner, tenure, charges],
|
90 |
+
outputs=churn_output)
|
91 |
+
|
92 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|