Spaces:
Build error
Build error
FIrst commit
Browse files- .gitignore +44 -0
- Dockerfile +12 -0
- README.md +82 -1
- app.py +85 -0
- requirements.txt +10 -0
.gitignore
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Archivos de entorno
|
| 2 |
+
.env
|
| 3 |
+
.env.*
|
| 4 |
+
|
| 5 |
+
# Archivos de Python
|
| 6 |
+
__pycache__/
|
| 7 |
+
*.py[cod]
|
| 8 |
+
*$py.class
|
| 9 |
+
*.so
|
| 10 |
+
.Python
|
| 11 |
+
env/
|
| 12 |
+
build/
|
| 13 |
+
develop-eggs/
|
| 14 |
+
dist/
|
| 15 |
+
downloads/
|
| 16 |
+
eggs/
|
| 17 |
+
.eggs/
|
| 18 |
+
lib/
|
| 19 |
+
lib64/
|
| 20 |
+
parts/
|
| 21 |
+
sdist/
|
| 22 |
+
var/
|
| 23 |
+
*.egg-info/
|
| 24 |
+
.installed.cfg
|
| 25 |
+
*.egg
|
| 26 |
+
|
| 27 |
+
# Directorios virtuales
|
| 28 |
+
venv/
|
| 29 |
+
ENV/
|
| 30 |
+
env/
|
| 31 |
+
|
| 32 |
+
# Archivos de IDE
|
| 33 |
+
.idea/
|
| 34 |
+
.vscode/
|
| 35 |
+
*.swp
|
| 36 |
+
*.swo
|
| 37 |
+
|
| 38 |
+
# Logs
|
| 39 |
+
*.log
|
| 40 |
+
logs/
|
| 41 |
+
|
| 42 |
+
# Archivos temporales
|
| 43 |
+
.DS_Store
|
| 44 |
+
Thumbs.db
|
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
EXPOSE 8000
|
| 11 |
+
|
| 12 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
README.md
CHANGED
|
@@ -9,4 +9,85 @@ license: apache-2.0
|
|
| 9 |
short_description: Backend of SmolLM2 chat
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
short_description: Backend of SmolLM2 chat
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# SmolLM2 Backend
|
| 13 |
+
|
| 14 |
+
Este proyecto implementa una API con FastAPI que utiliza LangChain y LangGraph para generar texto con el modelo Qwen2.5-72B-Instruct de HuggingFace.
|
| 15 |
+
|
| 16 |
+
## Configuraci贸n
|
| 17 |
+
|
| 18 |
+
### En HuggingFace Spaces
|
| 19 |
+
|
| 20 |
+
Este proyecto est谩 dise帽ado para ejecutarse en HuggingFace Spaces. Para configurarlo:
|
| 21 |
+
|
| 22 |
+
1. Crea un nuevo Space en HuggingFace con SDK Docker
|
| 23 |
+
2. Configura la variable de entorno `HUGGINGFACE_TOKEN` o `HF_TOKEN` en la configuraci贸n del Space:
|
| 24 |
+
- Ve a la pesta帽a "Settings" de tu Space
|
| 25 |
+
- Despl谩zate hasta la secci贸n "Repository secrets"
|
| 26 |
+
- Agrega una nueva variable con el nombre `HUGGINGFACE_TOKEN` y tu token como valor
|
| 27 |
+
- Guarda los cambios
|
| 28 |
+
|
| 29 |
+
### Desarrollo local
|
| 30 |
+
|
| 31 |
+
Para desarrollo local:
|
| 32 |
+
|
| 33 |
+
1. Clona este repositorio
|
| 34 |
+
2. Crea un archivo `.env` en la ra铆z del proyecto con tu token de HuggingFace:
|
| 35 |
+
```
|
| 36 |
+
HUGGINGFACE_TOKEN=tu_token_aqui
|
| 37 |
+
```
|
| 38 |
+
3. Instala las dependencias:
|
| 39 |
+
```
|
| 40 |
+
pip install -r requirements.txt
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
## Ejecuci贸n local
|
| 44 |
+
|
| 45 |
+
```bash
|
| 46 |
+
uvicorn app:app --reload
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
La API estar谩 disponible en `http://localhost:8000`.
|
| 50 |
+
|
| 51 |
+
## Endpoints
|
| 52 |
+
|
| 53 |
+
### GET /
|
| 54 |
+
|
| 55 |
+
Endpoint de bienvenida que devuelve un mensaje de saludo.
|
| 56 |
+
|
| 57 |
+
### POST /generate
|
| 58 |
+
|
| 59 |
+
Endpoint para generar texto usando el modelo de lenguaje.
|
| 60 |
+
|
| 61 |
+
**Par谩metros de la solicitud:**
|
| 62 |
+
```json
|
| 63 |
+
{
|
| 64 |
+
"query": "Tu pregunta aqu铆",
|
| 65 |
+
"thread_id": "identificador_opcional_del_hilo"
|
| 66 |
+
}
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
**Respuesta:**
|
| 70 |
+
```json
|
| 71 |
+
{
|
| 72 |
+
"generated_text": "Texto generado por el modelo",
|
| 73 |
+
"thread_id": "identificador_del_hilo"
|
| 74 |
+
}
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
## Docker
|
| 78 |
+
|
| 79 |
+
Para ejecutar la aplicaci贸n en un contenedor Docker:
|
| 80 |
+
|
| 81 |
+
```bash
|
| 82 |
+
# Construir la imagen
|
| 83 |
+
docker build -t smollm2-backend .
|
| 84 |
+
|
| 85 |
+
# Ejecutar el contenedor
|
| 86 |
+
docker run -p 8000:8000 --env-file .env smollm2-backend
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
## Documentaci贸n de la API
|
| 90 |
+
|
| 91 |
+
La documentaci贸n interactiva de la API est谩 disponible en:
|
| 92 |
+
- Swagger UI: `http://localhost:8000/docs`
|
| 93 |
+
- ReDoc: `http://localhost:8000/redoc`
|
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
+
|
| 4 |
+
from fastapi import FastAPI, HTTPException
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 9 |
+
from langchain_core.messages import HumanMessage
|
| 10 |
+
from langgraph.checkpoint.memory import MemorySaver
|
| 11 |
+
from langgraph.graph import START, MessagesState, StateGraph
|
| 12 |
+
|
| 13 |
+
# Cargar variables de entorno (煤til para desarrollo local)
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
# Obtener token de HuggingFace
|
| 17 |
+
# En HuggingFace Spaces, el token estar谩 disponible como variable de entorno
|
| 18 |
+
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN") or os.getenv("HF_TOKEN")
|
| 19 |
+
if not HUGGINGFACE_TOKEN:
|
| 20 |
+
raise ValueError("No se encontr贸 la variable de entorno HUGGINGFACE_TOKEN o HF_TOKEN")
|
| 21 |
+
|
| 22 |
+
# Inicializar el modelo
|
| 23 |
+
model = HuggingFaceEndpoint(
|
| 24 |
+
model="Qwen/Qwen2.5-72B-Instruct",
|
| 25 |
+
huggingfacehub_api_token=HUGGINGFACE_TOKEN,
|
| 26 |
+
max_new_tokens=64,
|
| 27 |
+
temperature=0.5,
|
| 28 |
+
top_p=0.7,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Definir el grafo
|
| 32 |
+
workflow = StateGraph(state_schema=MessagesState)
|
| 33 |
+
|
| 34 |
+
# Definir la funci贸n que llama al modelo
|
| 35 |
+
def call_model(state: MessagesState):
|
| 36 |
+
response = model.invoke(state["messages"])
|
| 37 |
+
return {"messages": response}
|
| 38 |
+
|
| 39 |
+
# Definir el nodo en el grafo
|
| 40 |
+
workflow.add_edge(START, "model")
|
| 41 |
+
workflow.add_node("model", call_model)
|
| 42 |
+
|
| 43 |
+
# Agregar memoria
|
| 44 |
+
memory = MemorySaver()
|
| 45 |
+
graph_app = workflow.compile(checkpointer=memory)
|
| 46 |
+
|
| 47 |
+
# Definir el modelo de datos para la solicitud
|
| 48 |
+
class QueryRequest(BaseModel):
|
| 49 |
+
query: str
|
| 50 |
+
thread_id: str = "default"
|
| 51 |
+
|
| 52 |
+
# Crear la aplicaci贸n FastAPI
|
| 53 |
+
app = FastAPI(title="LangChain FastAPI", description="API para generar texto usando LangChain y LangGraph")
|
| 54 |
+
|
| 55 |
+
@app.get("/")
|
| 56 |
+
async def root():
|
| 57 |
+
"""Endpoint de bienvenida"""
|
| 58 |
+
return {"detail": "Welcome to FastAPI, Langchain, Docker tutorial"}
|
| 59 |
+
|
| 60 |
+
@app.post("/generate")
|
| 61 |
+
async def generate(request: QueryRequest):
|
| 62 |
+
"""Endpoint para generar texto usando el modelo de lenguaje"""
|
| 63 |
+
try:
|
| 64 |
+
# Configurar el ID del hilo
|
| 65 |
+
config = {"configurable": {"thread_id": request.thread_id}}
|
| 66 |
+
|
| 67 |
+
# Crear el mensaje de entrada
|
| 68 |
+
input_messages = [HumanMessage(content=request.query)]
|
| 69 |
+
|
| 70 |
+
# Invocar el grafo
|
| 71 |
+
output = graph_app.invoke({"messages": input_messages}, config)
|
| 72 |
+
|
| 73 |
+
# Obtener la respuesta del modelo
|
| 74 |
+
response = output["messages"][-1].content
|
| 75 |
+
|
| 76 |
+
return {
|
| 77 |
+
"generated_text": response,
|
| 78 |
+
"thread_id": request.thread_id
|
| 79 |
+
}
|
| 80 |
+
except Exception as e:
|
| 81 |
+
raise HTTPException(status_code=500, detail=f"Error al generar texto: {str(e)}")
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
import uvicorn
|
| 85 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.99.1
|
| 2 |
+
uvicorn
|
| 3 |
+
requests
|
| 4 |
+
pydantic==1.10.12
|
| 5 |
+
langchain
|
| 6 |
+
langchain-huggingface
|
| 7 |
+
langgraph
|
| 8 |
+
python-dotenv
|
| 9 |
+
clarifai
|
| 10 |
+
Pillow
|