Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,125 +1,171 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
|
|
|
|
3 |
import torch
|
4 |
-
import
|
5 |
-
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
|
|
|
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
"Gênesis 1:27 - Criou Deus o homem à sua imagem...",
|
22 |
-
"1 Coríntios 6:9-11 - Não sabeis que os injustos...",
|
23 |
-
"Gálatas 3:28 - Não há judeu nem grego..."
|
24 |
-
]
|
25 |
-
},
|
26 |
-
"Aborto e Vida": {
|
27 |
-
"description": "Visão bíblica sobre a santidade da vida",
|
28 |
-
"verses": [
|
29 |
-
"Salmos 139:13-16 - Tu formaste o meu interior...",
|
30 |
-
"Jeremias 1:5 - Antes que te formasses...",
|
31 |
-
"Êxodo 20:13 - Não matarás"
|
32 |
-
]
|
33 |
-
}
|
34 |
-
}
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
""
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
49 |
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
response += "Pesquisando nas escrituras...\n"
|
60 |
-
|
61 |
-
for topic, info in CONTROVERSIAL_TOPICS.items():
|
62 |
-
if any(word.lower() in message.lower() for word in topic.split()):
|
63 |
-
response += f"\nVersículos relacionados a {topic}:\n"
|
64 |
-
for verse in info['verses'][:2]:
|
65 |
-
response += f"• {verse}\n"
|
66 |
-
|
67 |
-
history.append((message, response))
|
68 |
-
return history
|
69 |
-
|
70 |
-
except Exception as e:
|
71 |
-
history.append((message, f"Ocorreu um erro: {str(e)}"))
|
72 |
-
return history
|
73 |
-
|
74 |
-
# Interface Gradio
|
75 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
76 |
-
gr.HTML("""
|
77 |
-
<div style="text-align: center; padding: 20px;">
|
78 |
-
<h1>📖 Análise Bíblica de Temas Contemporâneos</h1>
|
79 |
-
<p>Explore diferentes perspectivas bíblicas sobre questões atuais</p>
|
80 |
-
</div>
|
81 |
-
""")
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
with gr.
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
)
|
91 |
-
with gr.Row():
|
92 |
-
msg = gr.Textbox(
|
93 |
-
show_label=False,
|
94 |
-
placeholder="Digite sua pergunta ou clique em um dos temas...",
|
95 |
-
scale=4
|
96 |
-
)
|
97 |
-
clear = gr.Button("🗑️ Limpar", scale=1)
|
98 |
-
|
99 |
-
# Coluna de tópicos
|
100 |
-
with gr.Column(scale=1):
|
101 |
-
gr.Markdown("### 🔍 Temas para Explorar")
|
102 |
-
|
103 |
-
# Criar botões para cada tema
|
104 |
-
for topic in CONTROVERSIAL_TOPICS.keys():
|
105 |
-
topic_btn = gr.Button(f"📚 {topic}")
|
106 |
-
# Criar uma closure para capturar o valor do tópico
|
107 |
-
topic_btn.click(
|
108 |
-
fn=lambda t=topic, h=None: process_topic(t, h if h else []),
|
109 |
-
inputs=[chatbot],
|
110 |
-
outputs=[chatbot]
|
111 |
-
)
|
112 |
|
113 |
-
#
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
-
|
125 |
-
demo.launch(
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
import torch
|
6 |
+
from PIL import Image
|
7 |
+
import easyocr
|
8 |
+
import librosa
|
9 |
+
import soundfile as sf
|
10 |
+
from googletrans import Translator
|
11 |
+
import spacy
|
12 |
|
13 |
+
# 1. Reconhecimento de Texto em Imagens (OCR)
|
14 |
+
def ocr_text(image):
|
15 |
+
reader = easyocr.Reader(['pt', 'en'])
|
16 |
+
result = reader.readtext(image)
|
17 |
+
return " ".join([text[1] for text in result])
|
18 |
|
19 |
+
# 2. Detector de Objetos
|
20 |
+
def detect_objects(image):
|
21 |
+
detector = pipeline('object-detection', model='facebook/detr-resnet-50')
|
22 |
+
results = detector(image)
|
23 |
+
annotated_image = Image.fromarray(np.array(image))
|
24 |
+
for result in results:
|
25 |
+
box = result['box']
|
26 |
+
label = f"{result['label']}: {result['score']:.2f}"
|
27 |
+
cv2.rectangle(
|
28 |
+
np.array(annotated_image),
|
29 |
+
(int(box['xmin']), int(box['ymin'])),
|
30 |
+
(int(box['xmax']), int(box['ymax'])),
|
31 |
+
(255, 0, 0),
|
32 |
+
2
|
33 |
+
)
|
34 |
+
return annotated_image
|
35 |
|
36 |
+
# 3. Análise de Sentimentos
|
37 |
+
def analyze_sentiment(text):
|
38 |
+
classifier = pipeline("sentiment-analysis", model="neuralmind/bert-base-portuguese-cased")
|
39 |
+
result = classifier(text)
|
40 |
+
return f"Sentimento: {result[0]['label']}, Confiança: {result[0]['score']:.2f}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
# 4. Reconhecimento de Fala
|
43 |
+
def speech_to_text(audio):
|
44 |
+
asr = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
|
45 |
+
return asr(audio)["text"]
|
46 |
+
|
47 |
+
# 5. Resumo de Texto
|
48 |
+
def summarize_text(text):
|
49 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
50 |
+
summary = summarizer(text, max_length=130, min_length=30)
|
51 |
+
return summary[0]['summary_text']
|
52 |
+
|
53 |
+
# 6. Geração de Legendas para Imagens
|
54 |
+
def generate_caption(image):
|
55 |
+
captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
56 |
+
caption = captioner(image)[0]['generated_text']
|
57 |
+
return caption
|
58 |
+
|
59 |
+
# 7. Tradução de Texto
|
60 |
+
def translate_text(text, target_lang):
|
61 |
+
translator = Translator()
|
62 |
+
translation = translator.translate(text, dest=target_lang)
|
63 |
+
return translation.text
|
64 |
+
|
65 |
+
# 8. Extração de Entidades Nomeadas (NER)
|
66 |
+
def extract_entities(text):
|
67 |
+
nlp = spacy.load("pt_core_news_sm")
|
68 |
+
doc = nlp(text)
|
69 |
+
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
70 |
+
return str(entities)
|
71 |
+
|
72 |
+
# 9. Classificação de Imagens
|
73 |
+
def classify_image(image):
|
74 |
+
classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
|
75 |
+
results = classifier(image)
|
76 |
+
return f"{results[0]['label']}: {results[0]['score']:.2f}"
|
77 |
+
|
78 |
+
# 10. Resposta a Perguntas
|
79 |
+
def answer_question(context, question):
|
80 |
+
qa_pipeline = pipeline("question-answering", model="pierreguillou/bert-base-cased-squad-v1.1-portuguese")
|
81 |
+
result = qa_pipeline(question=question, context=context)
|
82 |
+
return result['answer']
|
83 |
+
|
84 |
+
# Interface Gradio
|
85 |
+
with gr.Blocks(title="Hub de Serviços IA Open Source") as demo:
|
86 |
+
gr.Markdown("# 🤖 Hub de Serviços de IA Open Source")
|
87 |
|
88 |
+
# 1. OCR
|
89 |
+
with gr.Tab("OCR"):
|
90 |
+
with gr.Row():
|
91 |
+
ocr_input = gr.Image(type="numpy", label="Imagem com Texto")
|
92 |
+
ocr_output = gr.Textbox(label="Texto Extraído")
|
93 |
+
ocr_button = gr.Button("Extrair Texto")
|
94 |
+
ocr_button.click(ocr_text, inputs=ocr_input, outputs=ocr_output)
|
95 |
|
96 |
+
# 2. Detecção de Objetos
|
97 |
+
with gr.Tab("Detector de Objetos"):
|
98 |
+
with gr.Row():
|
99 |
+
obj_input = gr.Image(type="numpy", label="Imagem")
|
100 |
+
obj_output = gr.Image(label="Objetos Detectados")
|
101 |
+
obj_button = gr.Button("Detectar Objetos")
|
102 |
+
obj_button.click(detect_objects, inputs=obj_input, outputs=obj_output)
|
103 |
|
104 |
+
# 3. Análise de Sentimentos
|
105 |
+
with gr.Tab("Análise de Sentimentos"):
|
106 |
+
with gr.Row():
|
107 |
+
sent_input = gr.Textbox(label="Texto para Análise")
|
108 |
+
sent_output = gr.Textbox(label="Sentimento")
|
109 |
+
sent_button = gr.Button("Analisar Sentimento")
|
110 |
+
sent_button.click(analyze_sentiment, inputs=sent_input, outputs=sent_output)
|
111 |
|
112 |
+
# 4. Reconhecimento de Fala
|
113 |
+
with gr.Tab("Reconhecimento de Fala"):
|
114 |
+
with gr.Row():
|
115 |
+
speech_input = gr.Audio(type="numpy", label="Áudio")
|
116 |
+
speech_output = gr.Textbox(label="Texto Transcrito")
|
117 |
+
speech_button = gr.Button("Transcrever Áudio")
|
118 |
+
speech_button.click(speech_to_text, inputs=speech_input, outputs=speech_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
+
# 5. Resumo de Texto
|
121 |
+
with gr.Tab("Resumo de Texto"):
|
122 |
+
with gr.Row():
|
123 |
+
sum_input = gr.Textbox(label="Texto para Resumir")
|
124 |
+
sum_output = gr.Textbox(label="Resumo")
|
125 |
+
sum_button = gr.Button("Gerar Resumo")
|
126 |
+
sum_button.click(summarize_text, inputs=sum_input, outputs=sum_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
+
# 6. Geração de Legendas
|
129 |
+
with gr.Tab("Legendas para Imagens"):
|
130 |
+
with gr.Row():
|
131 |
+
cap_input = gr.Image(type="numpy", label="Imagem")
|
132 |
+
cap_output = gr.Textbox(label="Legenda")
|
133 |
+
cap_button = gr.Button("Gerar Legenda")
|
134 |
+
cap_button.click(generate_caption, inputs=cap_input, outputs=cap_output)
|
135 |
+
|
136 |
+
# 7. Tradução
|
137 |
+
with gr.Tab("Tradução"):
|
138 |
+
with gr.Row():
|
139 |
+
trans_input = gr.Textbox(label="Texto para Traduzir")
|
140 |
+
trans_lang = gr.Dropdown(choices=["en", "es", "fr", "de", "it"], label="Idioma Alvo")
|
141 |
+
trans_output = gr.Textbox(label="Tradução")
|
142 |
+
trans_button = gr.Button("Traduzir")
|
143 |
+
trans_button.click(translate_text, inputs=[trans_input, trans_lang], outputs=trans_output)
|
144 |
+
|
145 |
+
# 8. NER
|
146 |
+
with gr.Tab("Extração de Entidades"):
|
147 |
+
with gr.Row():
|
148 |
+
ner_input = gr.Textbox(label="Texto para Análise")
|
149 |
+
ner_output = gr.Textbox(label="Entidades Encontradas")
|
150 |
+
ner_button = gr.Button("Extrair Entidades")
|
151 |
+
ner_button.click(extract_entities, inputs=ner_input, outputs=ner_output)
|
152 |
+
|
153 |
+
# 9. Classificação de Imagens
|
154 |
+
with gr.Tab("Classificação de Imagens"):
|
155 |
+
with gr.Row():
|
156 |
+
class_input = gr.Image(type="numpy", label="Imagem")
|
157 |
+
class_output = gr.Textbox(label="Classificação")
|
158 |
+
class_button = gr.Button("Classificar Imagem")
|
159 |
+
class_button.click(classify_image, inputs=class_input, outputs=class_output)
|
160 |
+
|
161 |
+
# 10. Resposta a Perguntas
|
162 |
+
with gr.Tab("Resposta a Perguntas"):
|
163 |
+
with gr.Row():
|
164 |
+
qa_context = gr.Textbox(label="Contexto")
|
165 |
+
qa_question = gr.Textbox(label="Pergunta")
|
166 |
+
qa_output = gr.Textbox(label="Resposta")
|
167 |
+
qa_button = gr.Button("Responder")
|
168 |
+
qa_button.click(answer_question, inputs=[qa_context, qa_question], outputs=qa_output)
|
169 |
|
170 |
+
if __name__ == "__main__":
|
171 |
+
demo.launch()
|