PedroM2626 commited on
Commit
42d59e5
·
1 Parent(s): 5f1f1e4

refactor: remove openai integration and simplify interaction flow

Browse files
Files changed (2) hide show
  1. app.py +6 -29
  2. requirements.txt +0 -1
app.py CHANGED
@@ -24,21 +24,6 @@ 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
- try:
29
- import openai
30
- client = openai.OpenAI(api_key=api_key)
31
- response = client.chat.completions.create(
32
- model="gpt-3.5-turbo",
33
- messages=[
34
- {"role": "system", "content": "Você é um assistente virtual útil e conciso. Responda em português."},
35
- {"role": "user", "content": text}
36
- ]
37
- )
38
- return response.choices[0].message.content
39
- except Exception as e:
40
- return f"Erro na IA: {str(e)}"
41
-
42
  def try_local_commands(text):
43
  s = (text or "").lower()
44
  if "wikipedia" in s:
@@ -74,7 +59,7 @@ def text_to_speech(text):
74
  print(f"Erro TTS: {e}")
75
  return None
76
 
77
- def process_interaction(audio_path, text_input, history, api_key):
78
  # Inicializar histórico se for None
79
  if history is None:
80
  history = []
@@ -99,13 +84,9 @@ def process_interaction(audio_path, text_input, history, 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
 
@@ -127,7 +108,6 @@ def process_interaction(audio_path, text_input, history, api_key):
127
 
128
  def main():
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")
@@ -140,7 +120,7 @@ def main():
140
 
141
  with gr.Row():
142
  with gr.Column(scale=2):
143
- chatbot = gr.Chatbot(label="Conversa")
144
  audio_output = gr.Audio(label="Resposta em Áudio", autoplay=True)
145
 
146
  with gr.Column(scale=1):
@@ -149,19 +129,16 @@ def main():
149
  btn_send = gr.Button("Enviar", variant="primary")
150
  btn_clear = gr.Button("Limpar Conversa")
151
 
152
- # Estado para a chave API (pega do .env inicialmente)
153
- api_key_state = gr.State(value=api_key)
154
-
155
  # Eventos
156
  btn_send.click(
157
  process_interaction,
158
- inputs=[audio_input, text_input, chatbot, api_key_state],
159
  outputs=[chatbot, text_input, audio_output]
160
  )
161
 
162
  text_input.submit(
163
  process_interaction,
164
- inputs=[audio_input, text_input, chatbot, api_key_state],
165
  outputs=[chatbot, text_input, audio_output]
166
  )
167
 
 
24
  whisper_model = whisper.load_model("base")
25
  return whisper_model
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  def try_local_commands(text):
28
  s = (text or "").lower()
29
  if "wikipedia" in s:
 
59
  print(f"Erro TTS: {e}")
60
  return None
61
 
62
+ def process_interaction(audio_path, text_input, history):
63
  # Inicializar histórico se for None
64
  if history is None:
65
  history = []
 
84
  # Processar comando local primeiro
85
  response_text = try_local_commands(input_text)
86
 
87
+ # Se não for comando local, apenas confirma o que ouviu (já que removemos a IA externa)
88
  if response_text is None:
89
+ response_text = f"Você disse: {input_text}. (Comando local não reconhecido)"
 
 
 
 
90
 
91
  print(f"Resposta: {response_text}")
92
 
 
108
 
109
  def main():
110
  load_dotenv()
 
111
 
112
  with gr.Blocks(title="Assistente Virtual Local") as demo:
113
  gr.Markdown("# 🤖 Assistente Virtual 100% Local")
 
120
 
121
  with gr.Row():
122
  with gr.Column(scale=2):
123
+ chatbot = gr.Chatbot(label="Conversa", type="messages")
124
  audio_output = gr.Audio(label="Resposta em Áudio", autoplay=True)
125
 
126
  with gr.Column(scale=1):
 
129
  btn_send = gr.Button("Enviar", variant="primary")
130
  btn_clear = gr.Button("Limpar Conversa")
131
 
 
 
 
132
  # Eventos
133
  btn_send.click(
134
  process_interaction,
135
+ inputs=[audio_input, text_input, chatbot],
136
  outputs=[chatbot, text_input, audio_output]
137
  )
138
 
139
  text_input.submit(
140
  process_interaction,
141
+ inputs=[audio_input, text_input, chatbot],
142
  outputs=[chatbot, text_input, audio_output]
143
  )
144
 
requirements.txt CHANGED
@@ -2,7 +2,6 @@ pyttsx3==2.90
2
  python-dotenv==1.0.1
3
  pytest==8.3.3
4
  jupytext==1.16.2
5
- openai>=1.0.0
6
  openai-whisper
7
  gTTS
8
  pygame
 
2
  python-dotenv==1.0.1
3
  pytest==8.3.3
4
  jupytext==1.16.2
 
5
  openai-whisper
6
  gTTS
7
  pygame