File size: 1,402 Bytes
592b813
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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


@app.post("/embed")
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")