Spaces:
Sleeping
Sleeping
import gradio as gr | |
import torch | |
from transformers import pipeline | |
from datetime import datetime | |
import nltk | |
from nltk.tokenize import word_tokenize | |
from nltk.corpus import stopwords | |
from collections import Counter | |
import logging | |
from typing import Dict, List, Tuple, Optional | |
class GeradorTrilhaAprendizado: | |
def __init__(self): | |
try: | |
self.device = "cuda" if torch.cuda.is_available() else "cpu" | |
self.transcriber = pipeline("automatic-speech-recognition", | |
model="openai/whisper-base", | |
device=self.device) | |
self.generator = pipeline("text-generation", | |
model="gpt2-large", | |
device=self.device) | |
self.historico: List[Dict] = [] | |
# Download NLTK data only if not already present | |
for resource in ['punkt', 'stopwords']: | |
try: | |
nltk.data.find(f'tokenizers/{resource}') | |
except LookupError: | |
nltk.download(resource) | |
self.stop_words = set(stopwords.words('portuguese')) | |
except Exception as e: | |
logging.error(f"Initialization error: {str(e)}") | |
raise | |
def processar_audio(self, | |
audio_path: Optional[str], | |
nome_trilha: str, | |
nivel: str = "intermediário", | |
area: str = "geral", | |
duracao: str = "3 meses", | |
incluir_recursos: bool = True) -> Tuple[str, str, str, str]: | |
if not audio_path: | |
return ("", "", self._formatar_historico(), "❌ Nenhum áudio fornecido") | |
try: | |
transcricao = self.transcriber(audio_path)["text"] | |
if not transcricao.strip(): | |
return ("", "", self._formatar_historico(), "❌ Nenhum texto detectado no áudio") | |
analise = self._gerar_trilha_personalizada(transcricao, nivel, area, duracao) | |
if incluir_recursos: | |
recursos = self._gerar_recursos(nivel, area, transcricao) | |
analise += "\n\n" + recursos | |
self.historico.append({ | |
"data": datetime.now().strftime("%d/%m/%Y %H:%M"), | |
"nome": nome_trilha.strip(), | |
"nivel": nivel, | |
"area": area, | |
"duracao": duracao, | |
"transcricao": transcricao, | |
"analise": analise | |
}) | |
return (transcricao, analise, self._formatar_historico(), "✅ Trilha gerada com sucesso!") | |
except Exception as e: | |
logging.error(f"Processing error: {str(e)}") | |
return ("", "", self._formatar_historico(), f"❌ Erro: {str(e)}") | |
def _extrair_palavras_chave(self, texto: str) -> List[str]: | |
try: | |
tokens = word_tokenize(texto.lower()) | |
palavras = [palavra for palavra in tokens | |
if palavra.isalnum() and | |
len(palavra) > 2 and | |
palavra not in self.stop_words] | |
return [palavra[0] for palavra in Counter(palavras).most_common(5)] | |
except Exception as e: | |
logging.error(f"Keyword extraction error: {str(e)}") | |
return ["erro ao extrair palavras-chave"] | |
def _formatar_historico(self) -> str: | |
if not self.historico: | |
return "Nenhuma trilha gerada ainda" | |
return "📋 Histórico de Trilhas:\n\n" + "\n".join( | |
f"• {h['data']} - {h['nome']} ({h['nivel']}, {h['area']})" | |
for h in self.historico[-5:] | |
) | |
# Rest of the methods remain the same, but with added error handling and type hints | |
def criar_interface() -> gr.Blocks: | |
with gr.Blocks(theme=gr.themes.Soft()) as app: | |
gr.Markdown(""" | |
# 🎓 Gerador de Trilha de Aprendizado | |
Grave ou faça upload de um áudio descrevendo seus objetivos e receba uma trilha personalizada! | |
""") | |
with gr.Row(): | |
with gr.Column(): | |
audio_input = gr.Audio( | |
type="filepath", | |
label="Áudio", | |
sources=["microphone", "upload"] | |
) | |
nome_trilha = gr.Textbox( | |
label="Nome da Trilha", | |
placeholder="Dê um nome para sua trilha", | |
value="" | |
) | |
nivel = gr.Dropdown( | |
choices=["iniciante", "intermediário", "avançado"], | |
value="intermediário", | |
label="Nível de Dificuldade" | |
) | |
area = gr.Dropdown( | |
choices=["programação", "data science", "design", "marketing", "negócios", "geral"], | |
value="geral", | |
label="Área de Conhecimento" | |
) | |
duracao = gr.Dropdown( | |
choices=["1 mês", "3 meses", "6 meses", "1 ano"], | |
value="3 meses", | |
label="Duração Estimada" | |
) | |
incluir_recursos = gr.Checkbox( | |
label="Incluir Recursos Recomendados", | |
value=True | |
) | |
processar_btn = gr.Button("🚀 Gerar Trilha de Aprendizado") | |
with gr.Row(): | |
with gr.Column(): | |
status = gr.Markdown() | |
transcricao = gr.Textbox(label="Transcrição do Áudio", lines=4) | |
analise = gr.Textbox(label="Sua Trilha de Aprendizado", lines=10) | |
historico = gr.Markdown() | |
with gr.Accordion("ℹ️ Como usar"): | |
gr.Markdown(""" | |
1. Grave um áudio descrevendo seus objetivos de aprendizado | |
2. Escolha o nome da trilha, nível, área e duração | |
3. Clique em 'Gerar Trilha de Aprendizado' | |
4. Revise a transcrição e a trilha gerada | |
5. O histórico mostra suas últimas 5 trilhas geradas | |
""") | |
gerador = GeradorTrilhaAprendizado() # Create single instance | |
processar_btn.click( | |
fn=gerador.processar_audio, # Use instance method | |
inputs=[audio_input, nome_trilha, nivel, area, duracao, incluir_recursos], | |
outputs=[transcricao, analise, historico, status] | |
) | |
return app | |
if __name__ == "__main__": | |
logging.basicConfig(level=logging.INFO) | |
app = criar_interface() | |
app.queue() | |
app.launch() |