Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
from llama_cpp import Llama
|
4 |
+
import spacy
|
5 |
+
|
6 |
+
class ProcesadorAutodiagnostico:
|
7 |
+
def __init__(self):
|
8 |
+
# Initialize Llama model
|
9 |
+
self.llm = Llama(
|
10 |
+
model_path="./models/llama-2-7b-chat.gguf", # We'll need to specify the correct path
|
11 |
+
n_ctx=2048, # Context window
|
12 |
+
n_threads=4 # Adjust based on available resources
|
13 |
+
)
|
14 |
+
self.nlp = spacy.load("es_core_news_sm")
|
15 |
+
|
16 |
+
def generar_prompt(self, texto: str) -> str:
|
17 |
+
return f"""Analiza el siguiente texto y extrae informaci贸n estructurada para un autodiagn贸stico.
|
18 |
+
El texto describe un reto o proyecto relacionado con tecnolog铆as cognitivas.
|
19 |
+
|
20 |
+
Texto: {texto}
|
21 |
+
|
22 |
+
Genera un JSON con la siguiente estructura, manteniendo solo la informaci贸n relevante encontrada en el texto:
|
23 |
+
{{
|
24 |
+
"datos_generales": {{
|
25 |
+
"sector": "",
|
26 |
+
"ubicacion": ""
|
27 |
+
}},
|
28 |
+
"reto": {{
|
29 |
+
"descripcion": "",
|
30 |
+
"contexto": "",
|
31 |
+
"alcance": ""
|
32 |
+
}},
|
33 |
+
"conocimientos_cogtech": {{
|
34 |
+
"terminos_desconocidos": [],
|
35 |
+
"conceptos_dudosos": [],
|
36 |
+
"areas_desconfianza": []
|
37 |
+
}},
|
38 |
+
"dominio_actual": {{
|
39 |
+
"aspectos_dominados": [],
|
40 |
+
"experiencia_previa": [],
|
41 |
+
"recursos_disponibles": []
|
42 |
+
}},
|
43 |
+
"entrampes": {{
|
44 |
+
"tecnicos": [],
|
45 |
+
"implementacion": [],
|
46 |
+
"organizacionales": []
|
47 |
+
}},
|
48 |
+
"objetivos": {{
|
49 |
+
"corto_plazo": [],
|
50 |
+
"esperados_microtaller": []
|
51 |
+
}}
|
52 |
+
}}
|
53 |
+
|
54 |
+
Aseg煤rate de:
|
55 |
+
1. Identificar t茅rminos t茅cnicos que generan dudas
|
56 |
+
2. Detectar preocupaciones y 谩reas de desconfianza
|
57 |
+
3. Identificar objetivos expl铆citos e impl铆citos
|
58 |
+
4. Clasificar correctamente los entrampes
|
59 |
+
5. Mantener el contexto del sector y ubicaci贸n
|
60 |
+
|
61 |
+
JSON:"""
|
62 |
+
|
63 |
+
def procesar_texto(self, texto: str) -> dict:
|
64 |
+
# Generate prompt
|
65 |
+
prompt = self.generar_prompt(texto)
|
66 |
+
|
67 |
+
# Get completion from LLaMa
|
68 |
+
response = self.llm(
|
69 |
+
prompt,
|
70 |
+
max_tokens=2048,
|
71 |
+
temperature=0.1, # Low temperature for more consistent results
|
72 |
+
top_p=0.9,
|
73 |
+
stop=["```"] # Stop at code blocks if any
|
74 |
+
)
|
75 |
+
|
76 |
+
try:
|
77 |
+
# Parse the response as JSON
|
78 |
+
resultado = json.loads(response['choices'][0]['text'])
|
79 |
+
|
80 |
+
# Post-process with spaCy if needed
|
81 |
+
doc = self.nlp(texto)
|
82 |
+
# Add any additional processing here
|
83 |
+
|
84 |
+
return resultado
|
85 |
+
except json.JSONDecodeError:
|
86 |
+
# Fallback in case of invalid JSON
|
87 |
+
return {
|
88 |
+
"error": "No se pudo procesar el texto correctamente",
|
89 |
+
"texto_original": texto
|
90 |
+
}
|
91 |
+
|
92 |
+
def procesar_con_gradio(texto_input: str) -> str:
|
93 |
+
"""Wrapper function for Gradio interface"""
|
94 |
+
procesador = ProcesadorAutodiagnostico()
|
95 |
+
try:
|
96 |
+
resultado = procesador.procesar_texto(texto_input)
|
97 |
+
return json.dumps(resultado, indent=2, ensure_ascii=False)
|
98 |
+
except Exception as e:
|
99 |
+
return json.dumps({
|
100 |
+
"error": f"Error en el procesamiento: {str(e)}",
|
101 |
+
"texto_original": texto_input
|
102 |
+
}, indent=2, ensure_ascii=False)
|
103 |
+
|
104 |
+
# Create the Gradio interface
|
105 |
+
with gr.Blocks() as app:
|
106 |
+
gr.Markdown("# Autodiagn贸stico CogTech")
|
107 |
+
gr.Markdown("""
|
108 |
+
## Instrucciones
|
109 |
+
Describe tu situaci贸n considerando:
|
110 |
+
- Tu reto o necesidad principal
|
111 |
+
- Qu茅 conoces y qu茅 dudas tienes sobre tecnolog铆as cognitivas
|
112 |
+
- Qu茅 obst谩culos o dificultades enfrentas
|
113 |
+
- Qu茅 esperas lograr
|
114 |
+
""")
|
115 |
+
|
116 |
+
with gr.Row():
|
117 |
+
texto_input = gr.Textbox(
|
118 |
+
label="Tu descripci贸n",
|
119 |
+
placeholder="Describe tu situaci贸n...",
|
120 |
+
lines=10
|
121 |
+
)
|
122 |
+
json_output = gr.JSON(label="Autodiagn贸stico Estructurado")
|
123 |
+
|
124 |
+
analizar_btn = gr.Button("Generar Autodiagn贸stico")
|
125 |
+
analizar_btn.click(
|
126 |
+
fn=procesar_con_gradio,
|
127 |
+
inputs=texto_input,
|
128 |
+
outputs=json_output
|
129 |
+
)
|
130 |
+
|
131 |
+
if __name__ == "__main__":
|
132 |
+
app.launch()
|