File size: 1,752 Bytes
6dc0e5e
 
 
7ad392a
6dc0e5e
 
7b1ec46
6dc0e5e
 
7b1ec46
7ad392a
6dc0e5e
 
 
 
 
 
 
 
 
 
7ad392a
6dc0e5e
7ad392a
6dc0e5e
 
7b1ec46
7ad392a
7b1ec46
7ad392a
7b1ec46
7ad392a
 
7b1ec46
7ad392a
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import requests
import gradio as gr
from fastapi import Request
from fastapi.responses import HTMLResponse, JSONResponse

# 1. Читаем токен из секретов Спейса
HF_TOKEN = os.getenv("HF_TOKEN")

# Прокси-функция, которая прячет ключ на сервере
def proxy_to_hf(model, payload):
    try:
        headers = {
            "Authorization": f"Bearer {HF_TOKEN}",
            "Content-Type": "application/json"
        }
        response = requests.post(
            f"https://huggingface.co{model}",
            headers=headers,
            json=payload
        )
        return response.json(), response.status_code
    except Exception as e:
        return {"error": str(e)}, 500


# Создаем стандартный интерфейс Gradio, чтобы Hugging Face не ругался
with gr.Blocks() as demo:
    gr.Markdown("# Сервер скрытого прокси Hugging Face успешно запущен!")

# Забираем встроенный сервер FastAPI у Gradio
app = demo.app

# 2. Отдаем твой готовый HTML-сайт (аналог YouTube) на главной странице
@app.get("/", response_class=HTMLResponse)
def read_index():
    with open("index.html", "r", encoding="utf-8") as f:
        return f.read()

# 3. Безопасный эндпоинт, который принимает запросы от твоего index.html
@app.post("/api/proxy")
async def handle_proxy(request: Request):
    body = await request.json()
    model = body.get("model")
    payload = body.get("payload")
    
    result, status_code = proxy_to_hf(model, payload)
    return JSONResponse(content=result, status_code=status_code)