Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import (
|
| 3 |
+
BlipProcessor, BlipForConditionalGeneration,
|
| 4 |
+
WhisperProcessor, WhisperForConditionalGeneration,
|
| 5 |
+
AutoTokenizer, AutoModelForCausalLM
|
| 6 |
+
)
|
| 7 |
+
from diffusers import StableDiffusionPipeline
|
| 8 |
+
import torch
|
| 9 |
+
import librosa
|
| 10 |
+
import subprocess
|
| 11 |
+
from gtts import gTTS
|
| 12 |
+
import os
|
| 13 |
+
|
| 14 |
+
# Carregar modelos
|
| 15 |
+
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 16 |
+
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 17 |
+
|
| 18 |
+
sd_pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16).to("cuda")
|
| 19 |
+
|
| 20 |
+
whisper_processor = WhisperProcessor.from_pretrained("openai/whisper-small")
|
| 21 |
+
whisper_model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")
|
| 22 |
+
|
| 23 |
+
# Modelo de chatbot (OpenAssistant em português)
|
| 24 |
+
tokenizer = AutoTokenizer.from_pretrained("OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5")
|
| 25 |
+
chatbot_model = AutoModelForCausalLM.from_pretrained("OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5")
|
| 26 |
+
|
| 27 |
+
# Funções principais
|
| 28 |
+
def gerar_imagem(prompt):
|
| 29 |
+
imagem = sd_pipe(prompt).images[0]
|
| 30 |
+
return imagem
|
| 31 |
+
|
| 32 |
+
def descrever_imagem(imagem):
|
| 33 |
+
inputs = blip_processor(imagem, return_tensors="pt")
|
| 34 |
+
out = blip_model.generate(**inputs)
|
| 35 |
+
return blip_processor.decode(out[0], skip_special_tokens=True)
|
| 36 |
+
|
| 37 |
+
def transcrever_audio(audio):
|
| 38 |
+
speech, _ = librosa.load(audio, sr=16000)
|
| 39 |
+
input_features = whisper_processor(speech, sampling_rate=16000, return_tensors="pt").input_features
|
| 40 |
+
predicted_ids = whisper_model.generate(input_features)
|
| 41 |
+
return whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
| 42 |
+
|
| 43 |
+
def responder_chat(prompt):
|
| 44 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 45 |
+
outputs = chatbot_model.generate(**inputs, max_length=100)
|
| 46 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 47 |
+
|
| 48 |
+
def executar_comando(comando):
|
| 49 |
+
try:
|
| 50 |
+
resultado = subprocess.run(comando, shell=True, capture_output=True, text=True)
|
| 51 |
+
return resultado.stdout or "Comando executado!"
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"Erro: {str(e)}"
|
| 54 |
+
|
| 55 |
+
def texto_para_voz(texto):
|
| 56 |
+
tts = gTTS(texto, lang='pt-br')
|
| 57 |
+
tts.save("resposta.mp3")
|
| 58 |
+
return "resposta.mp3"
|
| 59 |
+
|
| 60 |
+
# Interface Gradio
|
| 61 |
+
with gr.Blocks() as demo:
|
| 62 |
+
gr.Markdown("# 🤖 IA Multimodal Completa")
|
| 63 |
+
|
| 64 |
+
with gr.Tab("Gerar Imagem"):
|
| 65 |
+
texto_imagem = gr.Textbox(label="Descreva a imagem que deseja gerar")
|
| 66 |
+
saida_imagem = gr.Image(label="Imagem Gerada")
|
| 67 |
+
btn_imagem = gr.Button("Gerar")
|
| 68 |
+
|
| 69 |
+
with gr.Tab("Descrever Imagem"):
|
| 70 |
+
entrada_imagem = gr.Image(label="Envie uma imagem", type="pil")
|
| 71 |
+
texto_descricao = gr.Textbox(label="Descrição Gerada")
|
| 72 |
+
btn_descricao = gr.Button("Descrever")
|
| 73 |
+
|
| 74 |
+
with gr.Tab("Transcrever Áudio"):
|
| 75 |
+
entrada_audio = gr.Audio(label="Grave ou envie um áudio", type="filepath")
|
| 76 |
+
texto_transcricao = gr.Textbox(label="Transcrição")
|
| 77 |
+
btn_transcricao = gr.Button("Transcrever")
|
| 78 |
+
|
| 79 |
+
with gr.Tab("Chatbot"):
|
| 80 |
+
chatbot_input = gr.Textbox(label="Pergunte algo")
|
| 81 |
+
chatbot_output = gr.Textbox(label="Resposta")
|
| 82 |
+
chatbot_audio = gr.Audio(label="Resposta em Áudio", visible=True)
|
| 83 |
+
chatbot_btn = gr.Button("Enviar")
|
| 84 |
+
|
| 85 |
+
with gr.Tab("Executar Comandos"):
|
| 86 |
+
comando_input = gr.Textbox(label="Comando (ex: pip install numpy)")
|
| 87 |
+
comando_output = gr.Textbox(label="Resultado")
|
| 88 |
+
comando_btn = gr.Button("Executar")
|
| 89 |
+
|
| 90 |
+
# Conexões
|
| 91 |
+
btn_imagem.click(gerar_imagem, inputs=texto_imagem, outputs=saida_imagem)
|
| 92 |
+
btn_descricao.click(descrever_imagem, inputs=entrada_imagem, outputs=texto_descricao)
|
| 93 |
+
btn_transcricao.click(transcrever_audio, inputs=entrada_audio, outputs=texto_transcricao)
|
| 94 |
+
chatbot_btn.click(responder_chat, inputs=chatbot_input, outputs=chatbot_output)
|
| 95 |
+
chatbot_btn.click(texto_para_voz, inputs=chatbot_output, outputs=chatbot_audio)
|
| 96 |
+
comando_btn.click(executar_comando, inputs=comando_input, outputs=comando_output)
|
| 97 |
+
|
| 98 |
+
demo.launch()
|