Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| from sentence_transformers import SentenceTransformer | |
| import numpy as np | |
| # Permite sobreescribir desde variables de entorno del Space | |
| MODEL_ID = os.getenv("MODEL_ID", "antonypamo/RRFSAVANTMADE") | |
| def load_model(): | |
| """ | |
| Carga perezosa / única del modelo. | |
| En Docker se ejecuta una sola vez al iniciar el contenedor. | |
| """ | |
| return SentenceTransformer(MODEL_ID) | |
| model = load_model() | |
| def embed_text(text: str): | |
| if not text.strip(): | |
| return "Please enter some text.", "" | |
| emb = model.encode(text) | |
| # Magnitud como métrica simple | |
| norm = float(np.linalg.norm(emb)) | |
| info = f"Embedding dim: {emb.shape[0]} | L2 norm: {norm:.4f}" | |
| # Muestra sólo los primeros 16 valores para que no sea gigante | |
| preview = ", ".join(f"{v:.4f}" for v in emb[:16]) | |
| return info, preview | |
| with gr.Blocks(title="SavantEngine – RRF Embeddings") as demo: | |
| gr.Markdown( | |
| """ | |
| # SavantEngine · RRF Embeddings | |
| Ingresa un texto y obtén su vector de embedding usando el modelo RRF. | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| text_in = gr.Textbox( | |
| label="Texto de entrada", | |
| placeholder="Describe una idea, ecuación RRF, o cualquier texto...", | |
| lines=5, | |
| ) | |
| run_btn = gr.Button("Calcular embedding") | |
| with gr.Column(): | |
| info_out = gr.Textbox(label="Info del embedding") | |
| vec_out = gr.Textbox( | |
| label="Primeros 16 componentes", | |
| lines=6, | |
| ) | |
| run_btn.click(embed_text, inputs=text_in, outputs=[info_out, vec_out]) | |
| if __name__ == "__main__": | |
| # En Docker/HF Spaces hay que escuchar en 0.0.0.0:7860 | |
| port = int(os.getenv("PORT", "7860")) | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=port, | |
| ) | |