Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- demostracion_nlp.py +15 -0
- sugerencias_nlp.py +35 -0
demostracion_nlp.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from sugerencias_nlp import procesar_comentarios
|
3 |
+
|
4 |
+
# Leer entrada original
|
5 |
+
with open("/mnt/data/entrada.txt", "r", encoding="utf-8") as f:
|
6 |
+
codigo = f.read()
|
7 |
+
|
8 |
+
# Obtener sugerencias NLP desde comentarios
|
9 |
+
sugerencias = procesar_comentarios(codigo)
|
10 |
+
|
11 |
+
# Anexar al final del archivo de c贸digo intermedio
|
12 |
+
with open("/mnt/data/codigo_intermedio.txt", "a", encoding="utf-8") as f:
|
13 |
+
f.write("\n# ==== SUGERENCIAS NLP DESDE COMENTARIOS ===\n")
|
14 |
+
for comentario, sugerencia in sugerencias:
|
15 |
+
f.write(f"# {comentario}\n{sugerencia}\n")
|
sugerencias_nlp.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
HUGGINGFACE_API_TOKEN = "hf_..." # Reemplazar con tu token real
|
4 |
+
API_URL = "https://api-inference.huggingface.co/models/gpt2"
|
5 |
+
|
6 |
+
headers = {
|
7 |
+
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}"
|
8 |
+
}
|
9 |
+
|
10 |
+
def obtener_sugerencia_nlp(texto_comentario):
|
11 |
+
payload = {
|
12 |
+
"inputs": f"// {texto_comentario}\n# sugerido:",
|
13 |
+
"parameters": {
|
14 |
+
"max_new_tokens": 20,
|
15 |
+
"temperature": 0.7,
|
16 |
+
"return_full_text": False
|
17 |
+
}
|
18 |
+
}
|
19 |
+
|
20 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
21 |
+
if response.status_code == 200:
|
22 |
+
resultado = response.json()
|
23 |
+
return resultado[0]["generated_text"].strip()
|
24 |
+
else:
|
25 |
+
return f"# (No se pudo generar sugerencia: {response.status_code})"
|
26 |
+
|
27 |
+
def procesar_comentarios(codigo: str):
|
28 |
+
lineas = codigo.splitlines()
|
29 |
+
sugerencias = []
|
30 |
+
for linea in lineas:
|
31 |
+
if "//" in linea:
|
32 |
+
comentario = linea.split("//", 1)[1].strip()
|
33 |
+
sugerencia = obtener_sugerencia_nlp(comentario)
|
34 |
+
sugerencias.append((comentario, sugerencia))
|
35 |
+
return sugerencias
|