Spaces:
Sleeping
Sleeping
Commit ·
5f1f1e4
1
Parent(s): b650615
refactor(app): improve local-first approach and error handling
Browse files
app.py
CHANGED
|
@@ -24,10 +24,7 @@ def get_whisper_model():
|
|
| 24 |
whisper_model = whisper.load_model("base")
|
| 25 |
return whisper_model
|
| 26 |
|
| 27 |
-
def get_chatgpt_response(text, api_key):
|
| 28 |
-
if not api_key or api_key == "sua_chave_api_aqui":
|
| 29 |
-
return "Erro: Chave API da OpenAI não configurada no arquivo .env."
|
| 30 |
-
|
| 31 |
try:
|
| 32 |
import openai
|
| 33 |
client = openai.OpenAI(api_key=api_key)
|
|
@@ -102,10 +99,13 @@ def process_interaction(audio_path, text_input, history, api_key):
|
|
| 102 |
# Processar comando local primeiro
|
| 103 |
response_text = try_local_commands(input_text)
|
| 104 |
|
| 105 |
-
# Se não for comando local,
|
| 106 |
if response_text is None:
|
| 107 |
-
print("Usando IA para responder...")
|
| 108 |
response_text = get_chatgpt_response(input_text, api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
print(f"Resposta: {response_text}")
|
| 111 |
|
|
@@ -129,9 +129,14 @@ def main():
|
|
| 129 |
load_dotenv()
|
| 130 |
api_key = os.getenv("OPENAI_API_KEY", "")
|
| 131 |
|
| 132 |
-
with gr.Blocks(title="Assistente Virtual
|
| 133 |
-
gr.Markdown("# 🤖 Assistente Virtual
|
| 134 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
with gr.Row():
|
| 137 |
with gr.Column(scale=2):
|
|
|
|
| 24 |
whisper_model = whisper.load_model("base")
|
| 25 |
return whisper_model
|
| 26 |
|
| 27 |
+
def get_chatgpt_response(text, api_key):
|
|
|
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
import openai
|
| 30 |
client = openai.OpenAI(api_key=api_key)
|
|
|
|
| 99 |
# Processar comando local primeiro
|
| 100 |
response_text = try_local_commands(input_text)
|
| 101 |
|
| 102 |
+
# Se não for comando local, tentar IA
|
| 103 |
if response_text is None:
|
|
|
|
| 104 |
response_text = get_chatgpt_response(input_text, api_key)
|
| 105 |
+
|
| 106 |
+
# Se a IA não estiver disponível (sem chave), apenas confirma o que ouviu
|
| 107 |
+
if response_text is None:
|
| 108 |
+
response_text = f"Você disse: {input_text}"
|
| 109 |
|
| 110 |
print(f"Resposta: {response_text}")
|
| 111 |
|
|
|
|
| 129 |
load_dotenv()
|
| 130 |
api_key = os.getenv("OPENAI_API_KEY", "")
|
| 131 |
|
| 132 |
+
with gr.Blocks(title="Assistente Virtual Local") as demo:
|
| 133 |
+
gr.Markdown("# 🤖 Assistente Virtual 100% Local")
|
| 134 |
+
gr.Markdown("Este assistente usa **OpenAI Whisper** rodando localmente no seu computador para ouvir e processar comandos sem depender de APIs externas.")
|
| 135 |
+
|
| 136 |
+
gr.Markdown("### 🎤 Comandos Disponíveis:")
|
| 137 |
+
gr.Markdown("- 'Pesquisar Wikipedia sobre [assunto]'")
|
| 138 |
+
gr.Markdown("- 'Abrir YouTube' ou 'Vídeo de [assunto]'")
|
| 139 |
+
gr.Markdown("- 'Farmácia próxima'")
|
| 140 |
|
| 141 |
with gr.Row():
|
| 142 |
with gr.Column(scale=2):
|