Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,187 +1,32 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
from langchain.output_parsers import PydanticOutputParser
|
16 |
-
from langchain_openai import ChatOpenAI
|
17 |
|
18 |
-
# Set up models for each app
|
19 |
-
chat = ChatOpenAI()
|
20 |
-
classifier = pipeline("sentiment-analysis", model="cardiffnlp/twitter-xlm-roberta-base-sentiment")
|
21 |
-
asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
|
22 |
-
summarizer = pipeline("summarization", model="knkarthick/MEETING_SUMMARY")
|
23 |
-
fin_model = pipeline("sentiment-analysis", model='yiyanghkust/finbert-tone', tokenizer='yiyanghkust/finbert-tone')
|
24 |
-
fls_model = pipeline("text-classification", model="demo-org/finbert_fls", tokenizer="demo-org/finbert_fls")
|
25 |
-
|
26 |
-
# --- Translator App ---
|
27 |
-
class TextTranslator(BaseModel):
|
28 |
-
output: str = Field(description="Python string containing the output text translated in the desired language")
|
29 |
-
|
30 |
-
output_parser = PydanticOutputParser(pydantic_object=TextTranslator)
|
31 |
-
format_instructions = output_parser.get_format_instructions()
|
32 |
-
|
33 |
-
def text_translator(input_text : str, language : str) -> str:
|
34 |
-
human_template = """Enter the text that you want to translate:
|
35 |
-
{input_text}, and enter the language that you want it to translate to {language}. {format_instructions}"""
|
36 |
-
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
37 |
-
chat_prompt = ChatPromptTemplate.from_messages([human_message_prompt])
|
38 |
-
prompt = chat_prompt.format_prompt(input_text = input_text, language = language, format_instructions = format_instructions)
|
39 |
-
messages = prompt.to_messages()
|
40 |
-
response = chat(messages = messages)
|
41 |
-
output = output_parser.parse(response.content)
|
42 |
-
output_text = output.output
|
43 |
-
return output_text
|
44 |
-
|
45 |
-
# --- Sentiment Analysis App ---
|
46 |
-
def sentiment_analysis(message, history):
|
47 |
-
result = classifier(message)
|
48 |
-
return f"Sentimiento : {result[0]['label']} (Probabilidad: {result[0]['score']:.2f})"
|
49 |
-
|
50 |
-
# --- Financial Analyst App ---
|
51 |
-
nlp = spacy.load('en_core_web_sm')
|
52 |
-
nlp.add_pipe('sentencizer')
|
53 |
-
|
54 |
-
def split_in_sentences(text):
|
55 |
-
doc = nlp(text)
|
56 |
-
return [str(sent).strip() for sent in doc.sents]
|
57 |
-
|
58 |
-
def make_spans(text, results):
|
59 |
-
results_list = [results[i]['label'] for i in range(len(results))]
|
60 |
-
return list(zip(split_in_sentences(text), results_list))
|
61 |
-
|
62 |
-
def summarize_text(text):
|
63 |
-
resp = summarizer(text)
|
64 |
-
return resp[0]['summary_text']
|
65 |
-
|
66 |
-
def text_to_sentiment(text):
|
67 |
-
sentiment = fin_model(text)[0]["label"]
|
68 |
-
return sentiment
|
69 |
-
|
70 |
-
def fin_ext(text):
|
71 |
-
results = fin_model(split_in_sentences(text))
|
72 |
-
return make_spans(text, results)
|
73 |
-
|
74 |
-
def fls(text):
|
75 |
-
results = fls_model(split_in_sentences(text))
|
76 |
-
return make_spans(text, results)
|
77 |
-
|
78 |
-
# --- Customer Churn App ---
|
79 |
-
script_dir = os.path.dirname(os.path.abspath(__file__))
|
80 |
-
pipeline_path = os.path.join(script_dir, 'toolkit', 'pipeline.joblib')
|
81 |
-
model_path = os.path.join(script_dir, 'toolkit', 'Random Forest Classifier.joblib')
|
82 |
-
|
83 |
-
pipeline = 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, InternetService, OnlineSecurity, OnlineBackup, DeviceProtection,
|
90 |
-
TechSupport, StreamingTV, StreamingMovies, Contract, PaperlessBilling, PaymentMethod, MonthlyCharges):
|
91 |
-
TotalCharges = calculate_total_charges(tenure, MonthlyCharges)
|
92 |
-
input_df = pd.DataFrame({
|
93 |
-
'SeniorCitizen': [SeniorCitizen],
|
94 |
-
'Partner': [Partner],
|
95 |
-
'Dependents': [Dependents],
|
96 |
-
'tenure': [tenure],
|
97 |
-
'InternetService': [InternetService],
|
98 |
-
'OnlineSecurity': [OnlineSecurity],
|
99 |
-
'OnlineBackup': [OnlineBackup],
|
100 |
-
'DeviceProtection': [DeviceProtection],
|
101 |
-
'TechSupport': [TechSupport],
|
102 |
-
'StreamingTV': [StreamingTV],
|
103 |
-
'StreamingMovies': [StreamingMovies],
|
104 |
-
'Contract': [Contract],
|
105 |
-
'PaperlessBilling': [PaperlessBilling],
|
106 |
-
'PaymentMethod': [PaymentMethod],
|
107 |
-
'MonthlyCharges': [MonthlyCharges],
|
108 |
-
'TotalCharges': [TotalCharges]
|
109 |
-
})
|
110 |
-
|
111 |
-
X_processed = pipeline.transform(input_df)
|
112 |
-
cat_cols = [col for col in input_df.columns if input_df[col].dtype == 'object']
|
113 |
-
num_cols = [col for col in input_df.columns if input_df[col].dtype != 'object']
|
114 |
-
|
115 |
-
cat_encoder = pipeline.named_steps['preprocessor'].named_transformers_['cat'].named_steps['onehot']
|
116 |
-
cat_feature_names = cat_encoder.get_feature_names_out(cat_cols)
|
117 |
-
|
118 |
-
feature_names = num_cols + list(cat_feature_names)
|
119 |
-
final_df = pd.DataFrame(X_processed, columns=feature_names)
|
120 |
-
final_df = pd.concat([final_df.iloc[:, 3:], final_df.iloc[:, :3]], axis=1)
|
121 |
-
|
122 |
-
prediction_probs = model.predict_proba(final_df)[0]
|
123 |
-
prediction_label = {
|
124 |
-
"Prediction: CHURN 🔴": prediction_probs[1],
|
125 |
-
"Prediction: STAY ✅": prediction_probs[0]
|
126 |
-
}
|
127 |
-
return prediction_label
|
128 |
-
|
129 |
-
# --- Personal Information Detection App ---
|
130 |
-
import gradio as gr
|
131 |
-
gr.load("models/iiiorg/piiranha-v1-detect-personal-information").launch()
|
132 |
-
|
133 |
-
# --- Gradio Interface ---
|
134 |
with gr.Blocks() as demo:
|
135 |
-
gr.Markdown("#
|
136 |
-
|
137 |
-
gr.HTML("<h1 align='center'>Text Translator</h1>")
|
138 |
-
text_input = gr.Textbox(label="Enter Text")
|
139 |
-
language_input = gr.Textbox(label="Enter Language")
|
140 |
-
translate_btn = gr.Button("Translate")
|
141 |
-
translated_text = gr.Textbox(label="Translated Text")
|
142 |
-
translate_btn.click(fn=text_translator, inputs=[text_input, language_input], outputs=translated_text)
|
143 |
|
144 |
-
|
145 |
-
gr.Markdown("# Sentiment Analysis")
|
146 |
-
sentiment_input = gr.Textbox(label="Enter Message")
|
147 |
-
sentiment_output = gr.Textbox(label="Sentiment")
|
148 |
-
sentiment_btn = gr.Button("Analyze Sentiment")
|
149 |
-
sentiment_btn.click(fn=sentiment_analysis, inputs=sentiment_input, outputs=sentiment_output)
|
150 |
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
financial_output = gr.Textbox(label="Analysis Results")
|
157 |
-
summarize_btn.click(fn=summarize_text, inputs=financial_input, outputs=financial_output)
|
158 |
-
sentiment_btn.click(fn=text_to_sentiment, inputs=financial_input, outputs=financial_output)
|
159 |
|
160 |
-
|
161 |
-
gr.Markdown("# Customer Churn Prediction")
|
162 |
-
churn_inputs = [
|
163 |
-
gr.Radio(['Yes', 'No'], label="Are you a Seniorcitizen?"),
|
164 |
-
gr.Radio(['Yes', 'No'], label="Do you have a Partner?"),
|
165 |
-
gr.Radio(['No', 'Yes'], label="Do you have Dependents?"),
|
166 |
-
gr.Slider(label="Tenure (Months)", minimum=1, maximum=73),
|
167 |
-
gr.Radio(['DSL', 'Fiber optic', 'No Internet'], label="Internet Service"),
|
168 |
-
gr.Radio(['No', 'Yes'], label="Online Security"),
|
169 |
-
gr.Radio(['No', 'Yes'], label="Online Backup"),
|
170 |
-
gr.Radio(['No', 'Yes'], label="Device Protection"),
|
171 |
-
gr.Radio(['No', 'Yes'], label="Tech Support"),
|
172 |
-
gr.Radio(['No', 'Yes'], label="Streaming TV"),
|
173 |
-
gr.Radio(['No', 'Yes'], label="Streaming Movies"),
|
174 |
-
gr.Radio(['Month-to-month', 'One year', 'Two year'], label="Contract Type"),
|
175 |
-
gr.Radio(['Yes', 'No'], label="Paperless Billing"),
|
176 |
-
gr.Radio(['Electronic check', 'Mailed check', 'Bank transfer (automatic)', 'Credit card (automatic)'], label="Payment Method"),
|
177 |
-
gr.Slider(label="Monthly Charges", minimum=18.4, maximum=118.65)
|
178 |
-
]
|
179 |
-
churn_output = gr.Label(label="Churn Prediction")
|
180 |
-
churn_btn = gr.Button("Predict Churn")
|
181 |
-
churn_btn.click(fn=predict, inputs=churn_inputs, outputs=churn_output)
|
182 |
|
183 |
-
|
184 |
-
gr.HTML("<h1 align='center'>Personal Information Detection</h1>")
|
185 |
-
gr.Interface.load("models/iiiorg/piiranha-v1-detect-personal-information").launch()
|
186 |
|
187 |
-
demo.launch(
|
|
|
1 |
import gradio as gr
|
2 |
+
from modules.sentiment import sentiment_function # โมดูลวิเคราะห์ความรู้สึก
|
3 |
+
from modules.financial_analyst import financial_analysis_function # โมดูลวิเคราะห์การเงิน
|
4 |
+
from modules.translator import text_translator # โมดูลแปลภาษา
|
5 |
+
from modules.personal_info_identifier import identify_personal_info # โมดูลตรวจสอบข้อมูลส่วนบุคคล
|
6 |
+
from modules.churn_analysis import churn_prediction # โมดูลทำนายการเลิกบริการ
|
7 |
+
|
8 |
+
def run_all_functions(input_text):
|
9 |
+
sentiment_result = sentiment_function(input_text)
|
10 |
+
financial_result = financial_analysis_function(input_text)
|
11 |
+
translation_result = text_translator(input_text, "English")
|
12 |
+
personal_info_result = identify_personal_info(input_text)
|
13 |
+
churn_result = churn_prediction(input_text)
|
14 |
+
return sentiment_result, financial_result, translation_result, personal_info_result, churn_result
|
|
|
|
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
with gr.Blocks() as demo:
|
17 |
+
gr.Markdown("# Multi-Function App")
|
18 |
+
gr.Markdown("### Combine various AI tasks into one platform")
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
text_input = gr.Textbox(label="Enter text to analyze")
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
sentiment_output = gr.Textbox(label="Sentiment Analysis")
|
23 |
+
financial_output = gr.Textbox(label="Financial Analysis")
|
24 |
+
translation_output = gr.Textbox(label="Translation Result")
|
25 |
+
personal_info_output = gr.Textbox(label="Personal Info Detection")
|
26 |
+
churn_output = gr.Textbox(label="Customer Churn Prediction")
|
|
|
|
|
|
|
27 |
|
28 |
+
run_button = gr.Button("Run All Functions")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
run_button.click(fn=run_all_functions, inputs=text_input, outputs=[sentiment_output, financial_output, translation_output, personal_info_output, churn_output])
|
|
|
|
|
31 |
|
32 |
+
demo.launch()
|