Spaces:
Runtime error
Runtime error
Download app.py from aliakr3m/embed-REST: direct link, hf CLI and curl.
- Browser
- Download file 1.4 kB
-
https://huggingface.co/spaces/aliakr3m/embed-REST/resolve/main/app.py
- Command line
-
hf download hf://spaces/aliakr3m/embed-REST/app.py
-
curl -L -o app.py https://huggingface.co/spaces/aliakr3m/embed-REST/resolve/main/app.py
1.4 kB
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| import requests | |
| import time | |
| app = FastAPI() | |
| GRADIO_CALL_URL = "https://aliakr3m-embed-gen.hf.space/gradio_api/call/generate_embedding" | |
| class EmbedRequest(BaseModel): | |
| text: str | |
| def embed(req: EmbedRequest): | |
| # Step 1: start Gradio job | |
| r = requests.post( | |
| GRADIO_CALL_URL, | |
| json={"data": [req.text]}, | |
| headers={"Content-Type": "application/json"}, | |
| timeout=10, | |
| ) | |
| if r.status_code != 200: | |
| raise HTTPException(500, "Failed to start embedding job") | |
| event_id = r.json()["event_id"] | |
| # Step 2: poll result (Gradio SSE, but we consume it here) | |
| result_url = f"{GRADIO_CALL_URL}/{event_id}" | |
| with requests.get(result_url, stream=True, timeout=30) as stream: | |
| for line in stream.iter_lines(): | |
| if not line: | |
| continue | |
| decoded = line.decode() | |
| if decoded.startswith("data:"): | |
| payload = decoded[5:].strip() | |
| # payload is JSON array: | |
| # [ | |
| # "metadata string", | |
| # [embedding floats...] | |
| # ] | |
| data = eval(payload) # controlled source | |
| embedding = data[1] | |
| return {"embedding": embedding} | |
| raise HTTPException(500, "Embedding not completed") | |