| import os |
| import subprocess |
| import time |
| import gradio as gr |
| import requests |
| from huggingface_hub import hf_hub_download |
|
|
| |
| REPO_ID = "google/gemma-4-12B-it-qat-q4_0-gguf" |
| FILENAME = "gemma-4-12b-it-qat-q4_0.gguf" |
| BIN_PATH = "./serverLlama" |
|
|
| |
| MODEL_PATH = "/app/gemma-4-12b.gguf" if os.path.exists("/app") else "./gemma-4-12b.gguf" |
|
|
| if not os.path.exists(MODEL_PATH): |
| print(f"Descargando {FILENAME} desde el repositorio oficial de Google...") |
| descarga = hf_hub_download( |
| repo_id=REPO_ID, |
| filename=FILENAME, |
| local_dir="/app" if os.path.exists("/app") else "." |
| ) |
| |
| os.rename(descarga, MODEL_PATH) |
| print("¡Descarga oficial completada con éxito!") |
|
|
| |
| print("Configurando permisos para serverLlama...") |
| os.chmod(BIN_PATH, 0o755) |
|
|
| |
| print("Encendiendo serverLlama con 32k de contexto en segundo plano...") |
| subprocess.Popen([ |
| BIN_PATH, |
| "--model", MODEL_PATH, |
| "--host", "127.0.0.1", |
| "--port", "8080", |
| "--threads", "2", |
| "--ctx-size", "32768", |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| "--n-predict", "4096", |
| |
| "--cache-type-k", "q4_0", |
| "--cache-type-v", "q4_0", |
| |
| |
| |
| |
| "--batch-size", "1536", |
| "--ubatch-size", "768", |
| |
| ]) |
|
|
| |
| time.sleep(10) |
| print("¡Servidor backend en C++ listo!") |
|
|
| |
| def predict_api(prompt, system_prompt="Eres un asistente experto y conciso."): |
| if not prompt.strip(): |
| return "Por favor, escribe un prompt válido." |
|
|
| headers = {"Content-Type": "application/json"} |
| data = { |
| "messages": [ |
| {"role": "system", "content": system_prompt}, |
| {"role": "user", "content": prompt} |
| ], |
| "temperature": 0.7 |
| } |
| try: |
| response = requests.post("http://127.0.0.1:8080/v1/chat/completions", json=data, headers=headers) |
| if response.status_code == 200: |
| return response.json()["choices"][0]["message"]["content"].strip() |
| return f"Error del backend (Código {response.status_code}): {response.text}" |
| except Exception as e: |
| return f"Error al conectar con serverLlama: {e}" |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# Backend de IA - Sara (serverLlama + Gradio 32k)") |
| |
| with gr.Row(): |
| with gr.Column(): |
| txt_prompt = gr.Textbox(label="Prompt / Pregunta", lines=4, placeholder="Escribe aquí...") |
| txt_system = gr.Textbox(label="System Prompt", value="Eres un asistente experto.", lines=2) |
| btn_enviar = gr.Button("Enviar", variant="primary") |
| with gr.Column(): |
| txt_salida = gr.Textbox(label="Respuesta de Sara", lines=8, interactive=False) |
| |
| btn_enviar.click(fn=predict_api, inputs=[txt_prompt, txt_system], outputs=txt_salida) |
| |
| |
| api_endpoint = gr.Interface( |
| fn=predict_api, |
| inputs=[gr.Textbox(elem_id="prompt"), gr.Textbox(elem_id="system_prompt")], |
| outputs=gr.Textbox(), |
| api_name="predict_api" |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch(server_name="0.0.0.0", server_port=7860) |