Spaces:
Running
Running
| import os | |
| import requests | |
| HUGGINGFACE_API_TOKEN = os.environ.get("HF_TOKEN", "") | |
| API_URL = "/static-proxy?url=https%3A%2F%2Fapi-inference.huggingface.co%2Fmodels%2Fhuggingface%2FCodeBERTa-language-id%26quot%3B%3C%2Fspan%3E%3C!-- HTML_TAG_END --> | |
| headers = { | |
| "Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}" | |
| } | |
| def obtener_sugerencia_nlp(texto_comentario): | |
| payload = { | |
| "inputs": f"// {texto_comentario}\n# sugerido:", | |
| "parameters": { | |
| "max_new_tokens": 20, | |
| "temperature": 0.7, | |
| "return_full_text": False | |
| } | |
| } | |
| response = requests.post(API_URL, headers=headers, json=payload) | |
| if response.status_code == 200: | |
| resultado = response.json() | |
| return resultado[0]["generated_text"].strip() | |
| else: | |
| return f"(sin sugerencia: {response.status_code})" | |
| def procesar_comentarios(codigo: str): | |
| lineas = codigo.splitlines() | |
| sugerencias = [] | |
| for linea in lineas: | |
| if "//" in linea: | |
| comentario = linea.split("//", 1)[1].strip() | |
| sugerencia = obtener_sugerencia_nlp(comentario) | |
| sugerencias.append((comentario, sugerencia)) | |
| return sugerencias |