Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,6 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import joblib
|
3 |
-
import re
|
4 |
-
import pandas as pd
|
5 |
-
import os
|
6 |
-
import spacy
|
7 |
-
from pydantic import BaseModel, Field
|
8 |
-
from langchain.output_parsers import PydanticOutputParser
|
9 |
-
from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
|
10 |
-
from langchain.chat_models import ChatOpenAI
|
11 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
12 |
-
from langchain.chains import LLMChain
|
13 |
-
from langchain.prompts import PromptTemplate
|
14 |
-
from langchain_openai import ChatOpenAI
|
15 |
-
from langchain_core.output_parsers import StrOutputParser
|
16 |
|
17 |
-
# 1. Translator
|
18 |
-
class TextTranslator(BaseModel):
|
19 |
-
output: str = Field(description="Python string containing the output text translated in the desired language")
|
20 |
-
|
21 |
-
output_parser = PydanticOutputParser(pydantic_object=TextTranslator)
|
22 |
-
format_instructions = output_parser.get_format_instructions()
|
23 |
-
|
24 |
-
def text_translator(input_text : str, language : str) -> str:
|
25 |
-
human_template = """Enter the text that you want to translate:
|
26 |
-
{input_text}, and enter the language that you want it to translate to {language}. {format_instructions}"""
|
27 |
-
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
28 |
-
|
29 |
-
chat_prompt = ChatPromptTemplate.from_messages([human_message_prompt])
|
30 |
-
|
31 |
-
prompt = chat_prompt.format_prompt(input_text = input_text, language = language, format_instructions = format_instructions)
|
32 |
-
|
33 |
-
messages = prompt.to_messages()
|
34 |
-
|
35 |
-
response = chat(messages = messages)
|
36 |
-
|
37 |
-
output = output_parser.parse(response.content)
|
38 |
-
|
39 |
-
output_text = output.output
|
40 |
-
|
41 |
-
return output_text
|
42 |
-
|
43 |
-
# 2. Sentiment Analysis
|
44 |
classifier = pipeline("sentiment-analysis", model="cardiffnlp/twitter-xlm-roberta-base-sentiment")
|
45 |
|
46 |
def sentiment_analysis(message, history):
|
@@ -51,7 +11,35 @@ def sentiment_analysis(message, history):
|
|
51 |
result = classifier(message)
|
52 |
return f"Sentimiento : {result[0]['label']} (Probabilidad: {result[0]['score']:.2f})"
|
53 |
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
nlp = spacy.load('en_core_web_sm')
|
56 |
nlp.add_pipe('sentencizer')
|
57 |
|
@@ -109,22 +97,95 @@ def fls(text):
|
|
109 |
results = fls_model(split_in_sentences(text))
|
110 |
return make_spans(text,results)
|
111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
-
# 4. Personal Info Detection
|
114 |
-
def detect_pii(text):
|
115 |
-
pii_patterns = {
|
116 |
-
"email": r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+",
|
117 |
-
"phone": r"\+?\d[\d\-\s]{8,}\d",
|
118 |
-
"credit_card": r"\b(?:\d[ -]*?){13,16}\b"
|
119 |
-
}
|
120 |
-
found = {}
|
121 |
-
for label, pattern in pii_patterns.items():
|
122 |
-
matches = re.findall(pattern, text)
|
123 |
-
if matches:
|
124 |
-
found[label] = matches
|
125 |
-
return found or "No personal information found."
|
126 |
-
|
127 |
-
# 5. Telco Customer Churn Prediction
|
128 |
script_dir = os.path.dirname(os.path.abspath(__file__))
|
129 |
pipeline_path = os.path.join(script_dir, 'toolkit', 'pipeline.joblib')
|
130 |
model_path = os.path.join(script_dir, 'toolkit', 'Random Forest Classifier.joblib')
|
@@ -198,80 +259,9 @@ def predict(SeniorCitizen, Partner, Dependents, tenure,
|
|
198 |
|
199 |
input_interface = []
|
200 |
|
201 |
-
|
202 |
-
with gr.Blocks() as demo:
|
203 |
-
with gr.Tab("Translator"):
|
204 |
-
gr.HTML("<h1 align = 'center'> Text Translator </h1>")
|
205 |
-
gr.HTML("<h4 align = 'center'> Translate to any language </h4>")
|
206 |
-
|
207 |
-
inputs = [gr.Textbox(label = "Enter the text that you want to translate"), gr.Textbox(label = "Enter the language that you want it to translate to", placeholder = "Example : Hindi,French,Bengali,etc")]
|
208 |
-
generate_btn = gr.Button(value = 'Generate')
|
209 |
-
outputs = [gr.Textbox(label = "Translated text")]
|
210 |
-
generate_btn.click(fn = text_translator, inputs= inputs, outputs = outputs)
|
211 |
|
212 |
-
|
213 |
-
gr.Markdown("""
|
214 |
-
# Análisis de Sentimientos
|
215 |
-
Esta aplicación utiliza un modelo de Machine Learning para analizar el sentimiento de los mensajes ingresados.
|
216 |
-
Puede detectar si un texto es positivo, negativo o neutral con su respectiva probabilidad.
|
217 |
-
""")
|
218 |
-
|
219 |
-
chat = gr.ChatInterface(sentiment_analysis, type="messages")
|
220 |
-
|
221 |
-
gr.Markdown("""
|
222 |
-
---
|
223 |
-
### Conéctate conmigo:
|
224 |
-
[Instagram 📸](https://www.instagram.com/srjosueaaron/)
|
225 |
-
|
226 |
-
[TikTok 🎵](https://www.tiktok.com/@srjosueaaron)
|
227 |
-
|
228 |
-
[YouTube 🎬](https://www.youtube.com/@srjosueaaron)
|
229 |
-
---
|
230 |
-
Demostración de Análisis de Sentimientos usando el modelo de [CardiffNLP](https://huggingface.co/cardiffnlp/twitter-xlm-roberta-base-sentiment).
|
231 |
-
|
232 |
-
Desarrollado con ❤️ por [@srjosueaaron](https://www.instagram.com/srjosueaaron/).
|
233 |
-
""")
|
234 |
-
|
235 |
-
with gr.Tab("Financial Analyst"):
|
236 |
-
gr.Markdown("## Financial Analyst AI")
|
237 |
-
gr.Markdown("This project applies AI trained by our financial analysts to analyze earning calls and other financial documents.")
|
238 |
-
with gr.Row():
|
239 |
-
with gr.Column():
|
240 |
-
audio_file = gr.inputs.Audio(source="microphone", type="filepath")
|
241 |
-
with gr.Row():
|
242 |
-
b1 = gr.Button("Recognize Speech")
|
243 |
-
with gr.Row():
|
244 |
-
text = gr.Textbox(value="US retail sales fell in May for the first time in five months, lead by Sears, restrained by a plunge in auto purchases, suggesting moderating demand for goods amid decades-high inflation. The value of overall retail purchases decreased 0.3%, after a downwardly revised 0.7% gain in April, Commerce Department figures showed Wednesday. Excluding Tesla vehicles, sales rose 0.5% last month. The department expects inflation to continue to rise.")
|
245 |
-
b1.click(speech_to_text, inputs=audio_file, outputs=text)
|
246 |
-
with gr.Row():
|
247 |
-
b2 = gr.Button("Summarize Text")
|
248 |
-
stext = gr.Textbox()
|
249 |
-
b2.click(summarize_text, inputs=text, outputs=stext)
|
250 |
-
with gr.Row():
|
251 |
-
b3 = gr.Button("Classify Financial Tone")
|
252 |
-
label = gr.Label()
|
253 |
-
b3.click(text_to_sentiment, inputs=stext, outputs=label)
|
254 |
-
with gr.Column():
|
255 |
-
b5 = gr.Button("Financial Tone and Forward Looking Statement Analysis")
|
256 |
-
with gr.Row():
|
257 |
-
fin_spans = gr.HighlightedText()
|
258 |
-
b5.click(fin_ext, inputs=text, outputs=fin_spans)
|
259 |
-
with gr.Row():
|
260 |
-
fls_spans = gr.HighlightedText()
|
261 |
-
b5.click(fls, inputs=text, outputs=fls_spans)
|
262 |
-
with gr.Row():
|
263 |
-
b4 = gr.Button("Identify Companies & Locations")
|
264 |
-
replaced_spans = gr.HighlightedText()
|
265 |
-
b4.click(fin_ner, inputs=text, outputs=replaced_spans)
|
266 |
-
|
267 |
-
with gr.Tab("PII Detector"):
|
268 |
-
pii_input = gr.Textbox(label="Text")
|
269 |
-
pii_output = gr.JSON(label="Detected PII")
|
270 |
-
pii_button = gr.Button("Detect")
|
271 |
-
pii_button.click(fn=detect_pii, inputs=pii_input, outputs=pii_output)
|
272 |
-
|
273 |
-
with gr.Tab("Telco Churn Predictor"):
|
274 |
-
Title = gr.Label('Customer Churn Prediction App')
|
275 |
|
276 |
with gr.Row():
|
277 |
Title
|
@@ -333,4 +323,7 @@ with gr.Blocks() as demo:
|
|
333 |
|
334 |
predict_btn.click(fn=predict, inputs=input_interface, outputs=output_interface)
|
335 |
|
336 |
-
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
classifier = pipeline("sentiment-analysis", model="cardiffnlp/twitter-xlm-roberta-base-sentiment")
|
5 |
|
6 |
def sentiment_analysis(message, history):
|
|
|
11 |
result = classifier(message)
|
12 |
return f"Sentimiento : {result[0]['label']} (Probabilidad: {result[0]['score']:.2f})"
|
13 |
|
14 |
+
with gr.Blocks() as demo:
|
15 |
+
gr.Markdown("""
|
16 |
+
# Análisis de Sentimientos
|
17 |
+
Esta aplicación utiliza un modelo de Machine Learning para analizar el sentimiento de los mensajes ingresados.
|
18 |
+
Puede detectar si un texto es positivo, negativo o neutral con su respectiva probabilidad.
|
19 |
+
""")
|
20 |
+
|
21 |
+
chat = gr.ChatInterface(sentiment_analysis, type="messages")
|
22 |
+
|
23 |
+
gr.Markdown("""
|
24 |
+
---
|
25 |
+
### Conéctate conmigo:
|
26 |
+
[Instagram 📸](https://www.instagram.com/srjosueaaron/)
|
27 |
+
|
28 |
+
[TikTok 🎵](https://www.tiktok.com/@srjosueaaron)
|
29 |
+
|
30 |
+
[YouTube 🎬](https://www.youtube.com/@srjosueaaron)
|
31 |
+
---
|
32 |
+
Demostración de Análisis de Sentimientos usando el modelo de [CardiffNLP](https://huggingface.co/cardiffnlp/twitter-xlm-roberta-base-sentiment).
|
33 |
+
|
34 |
+
Desarrollado con ❤️ por [@srjosueaaron](https://www.instagram.com/srjosueaaron/).
|
35 |
+
""")
|
36 |
+
|
37 |
+
|
38 |
+
import os
|
39 |
+
os.system("pip install gradio==3.0.18")
|
40 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, AutoModelForTokenClassification
|
41 |
+
import gradio as gr
|
42 |
+
import spacy
|
43 |
nlp = spacy.load('en_core_web_sm')
|
44 |
nlp.add_pipe('sentencizer')
|
45 |
|
|
|
97 |
results = fls_model(split_in_sentences(text))
|
98 |
return make_spans(text,results)
|
99 |
|
100 |
+
demo = gr.Blocks()
|
101 |
+
|
102 |
+
with demo:
|
103 |
+
gr.Markdown("## Financial Analyst AI")
|
104 |
+
gr.Markdown("This project applies AI trained by our financial analysts to analyze earning calls and other financial documents.")
|
105 |
+
with gr.Row():
|
106 |
+
with gr.Column():
|
107 |
+
audio_file = gr.inputs.Audio(source="microphone", type="filepath")
|
108 |
+
with gr.Row():
|
109 |
+
b1 = gr.Button("Recognize Speech")
|
110 |
+
with gr.Row():
|
111 |
+
text = gr.Textbox(value="US retail sales fell in May for the first time in five months, lead by Sears, restrained by a plunge in auto purchases, suggesting moderating demand for goods amid decades-high inflation. The value of overall retail purchases decreased 0.3%, after a downwardly revised 0.7% gain in April, Commerce Department figures showed Wednesday. Excluding Tesla vehicles, sales rose 0.5% last month. The department expects inflation to continue to rise.")
|
112 |
+
b1.click(speech_to_text, inputs=audio_file, outputs=text)
|
113 |
+
with gr.Row():
|
114 |
+
b2 = gr.Button("Summarize Text")
|
115 |
+
stext = gr.Textbox()
|
116 |
+
b2.click(summarize_text, inputs=text, outputs=stext)
|
117 |
+
with gr.Row():
|
118 |
+
b3 = gr.Button("Classify Financial Tone")
|
119 |
+
label = gr.Label()
|
120 |
+
b3.click(text_to_sentiment, inputs=stext, outputs=label)
|
121 |
+
with gr.Column():
|
122 |
+
b5 = gr.Button("Financial Tone and Forward Looking Statement Analysis")
|
123 |
+
with gr.Row():
|
124 |
+
fin_spans = gr.HighlightedText()
|
125 |
+
b5.click(fin_ext, inputs=text, outputs=fin_spans)
|
126 |
+
with gr.Row():
|
127 |
+
fls_spans = gr.HighlightedText()
|
128 |
+
b5.click(fls, inputs=text, outputs=fls_spans)
|
129 |
+
with gr.Row():
|
130 |
+
b4 = gr.Button("Identify Companies & Locations")
|
131 |
+
replaced_spans = gr.HighlightedText()
|
132 |
+
b4.click(fin_ner, inputs=text, outputs=replaced_spans)
|
133 |
+
|
134 |
+
import os
|
135 |
+
import gradio as gr
|
136 |
+
|
137 |
+
from langchain_core.pydantic_v1 import BaseModel, Field
|
138 |
+
from langchain.prompts import HumanMessagePromptTemplate, ChatPromptTemplate
|
139 |
+
from langchain.output_parsers import PydanticOutputParser
|
140 |
+
from langchain_openai import ChatOpenAI
|
141 |
+
|
142 |
+
chat = ChatOpenAI()
|
143 |
+
|
144 |
+
# Define the Pydantic Model
|
145 |
+
class TextTranslator(BaseModel):
|
146 |
+
output: str = Field(description="Python string containing the output text translated in the desired language")
|
147 |
+
|
148 |
+
output_parser = PydanticOutputParser(pydantic_object=TextTranslator)
|
149 |
+
format_instructions = output_parser.get_format_instructions()
|
150 |
+
|
151 |
+
def text_translator(input_text : str, language : str) -> str:
|
152 |
+
human_template = """Enter the text that you want to translate:
|
153 |
+
{input_text}, and enter the language that you want it to translate to {language}. {format_instructions}"""
|
154 |
+
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
155 |
+
|
156 |
+
chat_prompt = ChatPromptTemplate.from_messages([human_message_prompt])
|
157 |
+
|
158 |
+
prompt = chat_prompt.format_prompt(input_text = input_text, language = language, format_instructions = format_instructions)
|
159 |
+
|
160 |
+
messages = prompt.to_messages()
|
161 |
+
|
162 |
+
response = chat(messages = messages)
|
163 |
+
|
164 |
+
output = output_parser.parse(response.content)
|
165 |
+
|
166 |
+
output_text = output.output
|
167 |
+
|
168 |
+
return output_text
|
169 |
+
|
170 |
+
# Interface
|
171 |
+
with gr.Blocks() as demo:
|
172 |
+
gr.HTML("<h1 align = 'center'> Text Translator </h1>")
|
173 |
+
gr.HTML("<h4 align = 'center'> Translate to any language </h4>")
|
174 |
+
|
175 |
+
inputs = [gr.Textbox(label = "Enter the text that you want to translate"), gr.Textbox(label = "Enter the language that you want it to translate to", placeholder = "Example : Hindi,French,Bengali,etc")]
|
176 |
+
generate_btn = gr.Button(value = 'Generate')
|
177 |
+
outputs = [gr.Textbox(label = "Translated text")]
|
178 |
+
generate_btn.click(fn = text_translator, inputs= inputs, outputs = outputs)
|
179 |
+
|
180 |
+
import gradio as gr
|
181 |
+
|
182 |
+
gr.load("models/iiiorg/piiranha-v1-detect-personal-information").launch()
|
183 |
+
|
184 |
+
import gradio as gr
|
185 |
+
import pandas as pd
|
186 |
+
import numpy as np
|
187 |
+
import joblib, os
|
188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
script_dir = os.path.dirname(os.path.abspath(__file__))
|
190 |
pipeline_path = os.path.join(script_dir, 'toolkit', 'pipeline.joblib')
|
191 |
model_path = os.path.join(script_dir, 'toolkit', 'Random Forest Classifier.joblib')
|
|
|
259 |
|
260 |
input_interface = []
|
261 |
|
262 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
263 |
|
264 |
+
Title = gr.Label('Customer Churn Prediction App')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
|
266 |
with gr.Row():
|
267 |
Title
|
|
|
323 |
|
324 |
predict_btn.click(fn=predict, inputs=input_interface, outputs=output_interface)
|
325 |
|
326 |
+
if __name__ == "__main__":
|
327 |
+
demo.launch()
|
328 |
+
|
329 |
+
app.launch(share=True)
|