Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,28 +8,34 @@ import sys
|
|
8 |
from tools.asesoramiento_tool import asesorar_consulta
|
9 |
from tools.triaje_tool import triaje_primer_tool
|
10 |
|
11 |
-
#
|
12 |
model_dir = os.path.join(os.path.dirname(__file__), "models")
|
13 |
os.makedirs(model_dir, exist_ok=True)
|
14 |
model_path = os.path.join(model_dir, "en_core_sci_md")
|
15 |
|
16 |
-
# Descargar y cargar el modelo si no existe
|
17 |
if not os.path.exists(model_path):
|
18 |
os.system(f"{sys.executable} -m spacy download en_core_sci_md --target {model_dir}")
|
19 |
nlp = spacy.load(model_path)
|
20 |
|
|
|
21 |
whisper_model = WhisperModel("tiny", device="cpu", compute_type="int8")
|
22 |
health_nlp = pipeline("text-generation", model="mistralai/Mixtral-8x7B-Instruct-v0.1")
|
23 |
|
24 |
def speech_to_text(audio_path):
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
27 |
|
28 |
def text_to_speech(text):
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
33 |
|
34 |
def process_input(audio, mode="asesoramiento"):
|
35 |
if audio is None:
|
@@ -42,6 +48,7 @@ def process_input(audio, mode="asesoramiento"):
|
|
42 |
respuesta = resultado["respuesta"]
|
43 |
elif mode == "triaje":
|
44 |
doc = nlp(query.lower())
|
|
|
45 |
symptoms = [ent.text for ent in doc.ents if ent.label_ in ["SYMPTOM", "DISEASE"]]
|
46 |
data = {
|
47 |
"sintomas": " ".join(symptoms) if symptoms else query,
|
@@ -73,4 +80,4 @@ with gr.Blocks(title="Agente Médico Speech-to-Speech") as demo:
|
|
73 |
|
74 |
btn.click(fn=process_input, inputs=[audio_input, mode_select], outputs=[audio_output, text_output])
|
75 |
|
76 |
-
demo.launch()
|
|
|
8 |
from tools.asesoramiento_tool import asesorar_consulta
|
9 |
from tools.triaje_tool import triaje_primer_tool
|
10 |
|
11 |
+
# Configuración y carga del modelo de lenguaje especializado
|
12 |
model_dir = os.path.join(os.path.dirname(__file__), "models")
|
13 |
os.makedirs(model_dir, exist_ok=True)
|
14 |
model_path = os.path.join(model_dir, "en_core_sci_md")
|
15 |
|
|
|
16 |
if not os.path.exists(model_path):
|
17 |
os.system(f"{sys.executable} -m spacy download en_core_sci_md --target {model_dir}")
|
18 |
nlp = spacy.load(model_path)
|
19 |
|
20 |
+
# Cargar modelos de speech-to-text y text-generation
|
21 |
whisper_model = WhisperModel("tiny", device="cpu", compute_type="int8")
|
22 |
health_nlp = pipeline("text-generation", model="mistralai/Mixtral-8x7B-Instruct-v0.1")
|
23 |
|
24 |
def speech_to_text(audio_path):
|
25 |
+
try:
|
26 |
+
segments, _ = whisper_model.transcribe(audio_path, language="es")
|
27 |
+
return " ".join([segment.text for segment in segments])
|
28 |
+
except Exception as e:
|
29 |
+
return f"Error en la transcripción: {str(e)}"
|
30 |
|
31 |
def text_to_speech(text):
|
32 |
+
try:
|
33 |
+
tts = gTTS(text, lang="es")
|
34 |
+
audio_file = "response.mp3"
|
35 |
+
tts.save(audio_file)
|
36 |
+
return audio_file
|
37 |
+
except Exception as e:
|
38 |
+
return None
|
39 |
|
40 |
def process_input(audio, mode="asesoramiento"):
|
41 |
if audio is None:
|
|
|
48 |
respuesta = resultado["respuesta"]
|
49 |
elif mode == "triaje":
|
50 |
doc = nlp(query.lower())
|
51 |
+
# Se mejora la extracción utilizando una lista ampliada de entidades y posibles sinónimos
|
52 |
symptoms = [ent.text for ent in doc.ents if ent.label_ in ["SYMPTOM", "DISEASE"]]
|
53 |
data = {
|
54 |
"sintomas": " ".join(symptoms) if symptoms else query,
|
|
|
80 |
|
81 |
btn.click(fn=process_input, inputs=[audio_input, mode_select], outputs=[audio_output, text_output])
|
82 |
|
83 |
+
demo.launch()
|