Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"""
|
| 6 |
-
|
| 7 |
"""
|
| 8 |
try:
|
| 9 |
datos = json.loads(datos_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
"
|
| 15 |
-
|
| 16 |
-
"ram_disponible": "16GB en uso"
|
| 17 |
-
}
|
| 18 |
|
| 19 |
-
return json.dumps(resultado, indent=2)
|
| 20 |
-
|
| 21 |
except Exception as e:
|
| 22 |
-
return json.dumps({"error": str(e)})
|
| 23 |
|
| 24 |
-
# Interfaz de Gradio
|
| 25 |
demo = gr.Interface(
|
| 26 |
-
fn=
|
| 27 |
-
inputs=gr.Textbox(label="
|
| 28 |
-
outputs=gr.Textbox(label="
|
| 29 |
-
title="
|
| 30 |
-
description="
|
| 31 |
)
|
| 32 |
|
| 33 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# Cargar modelo (esto usa la RAM de Hugging Face)
|
| 7 |
+
print("Cargando DeepSeek-Coder 1.3B...")
|
| 8 |
+
modelo_nombre = "deepseek-ai/deepseek-coder-1.3b-base"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(modelo_nombre, trust_remote_code=True)
|
| 10 |
+
modelo = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
modelo_nombre,
|
| 12 |
+
trust_remote_code=True,
|
| 13 |
+
torch_dtype=torch.float32, # Para CPU
|
| 14 |
+
low_cpu_mem_usage=True
|
| 15 |
+
)
|
| 16 |
+
print("✅ Modelo cargado en memoria de HF!")
|
| 17 |
+
|
| 18 |
+
def generar_codigo(datos_json):
|
| 19 |
"""
|
| 20 |
+
Genera código usando DeepSeek desde tu PC
|
| 21 |
"""
|
| 22 |
try:
|
| 23 |
datos = json.loads(datos_json)
|
| 24 |
+
prompt = datos.get("prompt", "")
|
| 25 |
+
max_tokens = datos.get("max_tokens", 256)
|
| 26 |
+
|
| 27 |
+
# Generar código
|
| 28 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 29 |
+
outputs = modelo.generate(
|
| 30 |
+
**inputs,
|
| 31 |
+
max_new_tokens=max_tokens,
|
| 32 |
+
temperature=0.7,
|
| 33 |
+
do_sample=True,
|
| 34 |
+
pad_token_id=tokenizer.eos_token_id
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
codigo_generado = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 38 |
|
| 39 |
+
return json.dumps({
|
| 40 |
+
"status": "ok",
|
| 41 |
+
"codigo": codigo_generado,
|
| 42 |
+
"modelo": modelo_nombre
|
| 43 |
+
}, indent=2)
|
|
|
|
|
|
|
| 44 |
|
|
|
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
+
return json.dumps({"status": "error", "mensaje": str(e)})
|
| 47 |
|
|
|
|
| 48 |
demo = gr.Interface(
|
| 49 |
+
fn=generar_codigo,
|
| 50 |
+
inputs=gr.Textbox(label="Datos JSON", lines=5),
|
| 51 |
+
outputs=gr.Textbox(label="Código generado", lines=15),
|
| 52 |
+
title="DeepSeek-Coder API",
|
| 53 |
+
description="Genera código con IA desde tu PC"
|
| 54 |
)
|
| 55 |
|
| 56 |
demo.launch()
|