Spaces:
Runtime error
Runtime error
| # This cell will generate a unified Gradio app.py content based on all 5 apps provided | |
| import os | |
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| import joblib | |
| import spacy | |
| from transformers import pipeline | |
| from langchain_core.pydantic import BaseModel, Field | |
| from langchain.prompts import HumanMessagePromptTemplate, ChatPromptTemplate | |
| from langchain.output_parsers import PydanticOutputParser | |
| from langchain_openai import ChatOpenAI | |
| # ---------------- Text Translator ---------------- # | |
| chat = ChatOpenAI() | |
| class TextTranslator(BaseModel): | |
| output: str = Field(description="Translated output text") | |
| output_parser = PydanticOutputParser(pydantic_object=TextTranslator) | |
| format_instructions = output_parser.get_format_instructions() | |
| def text_translator(input_text: str, language: str) -> str: | |
| human_template = f"Enter the text that you want to translate: {{input_text}}, and enter the language that you want it to translate to {{language}}. {format_instructions}" | |
| human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) | |
| chat_prompt = ChatPromptTemplate.from_messages([human_message_prompt]) | |
| prompt = chat_prompt.format_prompt(input_text=input_text, language=language, format_instructions=format_instructions) | |
| messages = prompt.to_messages() | |
| response = chat(messages=messages) | |
| output = output_parser.parse(response.content) | |
| return output.output | |
| # ---------------- Sentiment Analysis ---------------- # | |
| sentiment_classifier = pipeline("sentiment-analysis", model="cardiffnlp/twitter-xlm-roberta-base-sentiment") | |
| def sentiment_analysis(message, history): | |
| result = sentiment_classifier(message) | |
| return f"Sentiment: {result[0]['label']} (Probability: {result[0]['score']:.2f})" | |
| # ---------------- Financial Analyst ---------------- # | |
| nlp = spacy.load('en_core_web_sm') | |
| nlp.add_pipe('sentencizer') | |
| def split_in_sentences(text): | |
| doc = nlp(text) | |
| return [str(sent).strip() for sent in doc.sents] | |
| def make_spans(text, results): | |
| results_list = [res['label'] for res in results] | |
| return list(zip(split_in_sentences(text), results_list)) | |
| auth_token = os.environ.get("HF_Token") | |
| asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h") | |
| def speech_to_text(speech): | |
| return asr(speech)["text"] | |
| summarizer = pipeline("summarization", model="knkarthick/MEETING_SUMMARY") | |
| def summarize_text(text): | |
| return summarizer(text)[0]['summary_text'] | |
| fin_model = pipeline("sentiment-analysis", model='yiyanghkust/finbert-tone', tokenizer='yiyanghkust/finbert-tone') | |
| def text_to_sentiment(text): | |
| return fin_model(text)[0]["label"] | |
| def fin_ner(text): | |
| api = gr.Interface.load("dslim/bert-base-NER", src='models', use_auth_token=auth_token) | |
| return api(text) | |
| def fin_ext(text): | |
| results = fin_model(split_in_sentences(text)) | |
| return make_spans(text, results) | |
| def fls(text): | |
| fls_model = pipeline("text-classification", model="demo-org/finbert_fls", tokenizer="demo-org/finbert_fls", use_auth_token=auth_token) | |
| results = fls_model(split_in_sentences(text)) | |
| return make_spans(text, results) | |
| # ---------------- Personal Information Identifier ---------------- # | |
| def detect_personal_info(text): | |
| pii_model = gr.Interface.load("models/iiiorg/piiranha-v1-detect-personal-information") | |
| return pii_model(text) | |
| # ---------------- Customer Churn ---------------- # | |
| script_dir = os.path.dirname(os.path.abspath(__file__)) | |
| pipeline_path = os.path.join(script_dir, 'toolkit', 'pipeline.joblib') | |
| model_path = os.path.join(script_dir, 'toolkit', 'Random Forest Classifier.joblib') | |
| pipeline_churn = joblib.load(pipeline_path) | |
| model_churn = joblib.load(model_path) | |
| def calculate_total_charges(tenure, monthly_charges): | |
| return tenure * monthly_charges | |
| def predict_churn(SeniorCitizen, Partner, Dependents, tenure, | |
| InternetService, OnlineSecurity, OnlineBackup, DeviceProtection, TechSupport, | |
| StreamingTV, StreamingMovies, Contract, PaperlessBilling, PaymentMethod, | |
| MonthlyCharges): | |
| TotalCharges = calculate_total_charges(tenure, MonthlyCharges) | |
| input_df = pd.DataFrame({ | |
| 'SeniorCitizen': [SeniorCitizen], | |
| 'Partner': [Partner], | |
| 'Dependents': [Dependents], | |
| 'tenure': [tenure], | |
| 'InternetService': [InternetService], | |
| 'OnlineSecurity': [OnlineSecurity], | |
| 'OnlineBackup': [OnlineBackup], | |
| 'DeviceProtection': [DeviceProtection], | |
| 'TechSupport': [TechSupport], | |
| 'StreamingTV': [StreamingTV], | |
| 'StreamingMovies': [StreamingMovies], | |
| 'Contract': [Contract], | |
| 'PaperlessBilling': [PaperlessBilling], | |
| 'PaymentMethod': [PaymentMethod], | |
| 'MonthlyCharges': [MonthlyCharges], | |
| 'TotalCharges': [TotalCharges] | |
| }) | |
| cat_cols = [col for col in input_df.columns if input_df[col].dtype == 'object'] | |
| num_cols = [col for col in input_df.columns if input_df[col].dtype != 'object'] | |
| X_processed = pipeline_churn.transform(input_df) | |
| cat_encoder = pipeline_churn.named_steps['preprocessor'].named_transformers_['cat'].named_steps['onehot'] | |
| cat_feature_names = cat_encoder.get_feature_names_out(cat_cols) | |
| feature_names = num_cols + list(cat_feature_names) | |
| final_df = pd.DataFrame(X_processed, columns=feature_names) | |
| first_three_columns = final_df.iloc[:, :3] | |
| remaining_columns = final_df.iloc[:, 3:] | |
| final_df = pd.concat([remaining_columns, first_three_columns], axis=1) | |
| prediction_probs = model_churn.predict_proba(final_df)[0] | |
| return { | |
| "Prediction: CHURN 🔴": prediction_probs[1], | |
| "Prediction: STAY ✅": prediction_probs[0] | |
| } | |
| # ---------------- Interface ---------------- # | |
| with gr.Blocks() as app: | |
| with gr.Tab("Text Translator"): | |
| input_text = gr.Textbox(label="Enter text to translate") | |
| lang = gr.Textbox(label="Target language (e.g., Hindi, French)") | |
| output_text = gr.Textbox(label="Translated text") | |
| gr.Button("Translate").click(fn=text_translator, inputs=[input_text, lang], outputs=output_text) | |
| with gr.Tab("Sentiment Analysis"): | |
| gr.ChatInterface(sentiment_analysis) | |
| with gr.Tab("Financial Analyst"): | |
| audio_input = gr.Audio(source="microphone", type="filepath") | |
| text = gr.Textbox(label="Transcribed Text") | |
| gr.Button("Transcribe").click(fn=speech_to_text, inputs=audio_input, outputs=text) | |
| stext = gr.Textbox(label="Summary") | |
| gr.Button("Summarize").click(fn=summarize_text, inputs=text, outputs=stext) | |
| gr.Button("Financial Tone").click(fn=text_to_sentiment, inputs=stext, outputs=gr.Label()) | |
| gr.Button("NER").click(fn=fin_ner, inputs=text, outputs=gr.HighlightedText()) | |
| gr.Button("Tone per sentence").click(fn=fin_ext, inputs=text, outputs=gr.HighlightedText()) | |
| gr.Button("Forward-looking").click(fn=fls, inputs=text, outputs=gr.HighlightedText()) | |
| with gr.Tab("Personal Information Identifier"): | |
| pii_input = gr.Textbox(label="Enter text to analyze") | |
| pii_output = gr.Textbox(label="Detected Personal Info") | |
| gr.Button("Detect").click(fn=detect_personal_info, inputs=pii_input, outputs=pii_output) | |
| with gr.Tab("Customer Churn"): | |
| churn_inputs = [ | |
| gr.Radio(['Yes', 'No'], label="SeniorCitizen"), | |
| gr.Radio(['Yes', 'No'], label="Partner"), | |
| gr.Radio(['No', 'Yes'], label="Dependents"), | |
| gr.Slider(1, 73, step=1, label="Tenure (Months)"), | |
| gr.Radio(['DSL', 'Fiber optic', 'No Internet'], label="InternetService"), | |
| gr.Radio(['No', 'Yes'], label="OnlineSecurity"), | |
| gr.Radio(['No', 'Yes'], label="OnlineBackup"), | |
| gr.Radio(['No', 'Yes'], label="DeviceProtection"), | |
| gr.Radio(['No', 'Yes'], label="TechSupport"), | |
| gr.Radio(['No', 'Yes'], label="StreamingTV"), | |
| gr.Radio(['No', 'Yes'], label="StreamingMovies"), | |
| gr.Radio(['Month-to-month', 'One year', 'Two year'], label="Contract"), | |
| gr.Radio(['Yes', 'No'], label="PaperlessBilling"), | |
| gr.Radio(['Electronic check', 'Mailed check', 'Bank transfer (automatic)', 'Credit card (automatic)'], label="PaymentMethod"), | |
| gr.Slider(18.40, 118.65, label="MonthlyCharges") | |
| ] | |
| churn_output = gr.Label(label="Churn Prediction") | |
| gr.Button("Predict").click(fn=predict_churn, inputs=churn_inputs, outputs=churn_output) | |
| app.launch() | |