Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,17 +4,16 @@ import gradio as gr
|
|
| 4 |
from fastapi import Request
|
| 5 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 6 |
|
| 7 |
-
# 1. Читаем
|
| 8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
def proxy_to_hf(model, payload):
|
| 12 |
try:
|
| 13 |
headers = {
|
| 14 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 15 |
"Content-Type": "application/json"
|
| 16 |
}
|
| 17 |
-
# Сервер Hugging Face сам делает внутренний запрос к моделям
|
| 18 |
response = requests.post(
|
| 19 |
f"https://huggingface.co{model}",
|
| 20 |
headers=headers,
|
|
@@ -25,14 +24,14 @@ def proxy_to_hf(model, payload):
|
|
| 25 |
return {"error": str(e)}, 500
|
| 26 |
|
| 27 |
|
| 28 |
-
# Создаем
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
-
gr.Markdown("#
|
| 31 |
|
| 32 |
-
#
|
| 33 |
app = demo.app
|
| 34 |
|
| 35 |
-
# 2. Отдаем тво
|
| 36 |
@app.get("/", response_class=HTMLResponse)
|
| 37 |
def read_index():
|
| 38 |
with open("index.html", "r", encoding="utf-8") as f:
|
|
|
|
| 4 |
from fastapi import Request
|
| 5 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 6 |
|
| 7 |
+
# 1. Читаем токен из секретов Спейса
|
| 8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
|
| 10 |
+
# Прокси-функция, которая прячет ключ на сервере
|
| 11 |
def proxy_to_hf(model, payload):
|
| 12 |
try:
|
| 13 |
headers = {
|
| 14 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 15 |
"Content-Type": "application/json"
|
| 16 |
}
|
|
|
|
| 17 |
response = requests.post(
|
| 18 |
f"https://huggingface.co{model}",
|
| 19 |
headers=headers,
|
|
|
|
| 24 |
return {"error": str(e)}, 500
|
| 25 |
|
| 26 |
|
| 27 |
+
# Создаем стандартный интерфейс Gradio, чтобы Hugging Face не ругался
|
| 28 |
with gr.Blocks() as demo:
|
| 29 |
+
gr.Markdown("# Сервер скрытого прокси Hugging Face успешно запущен!")
|
| 30 |
|
| 31 |
+
# Забираем встроенный сервер FastAPI у Gradio
|
| 32 |
app = demo.app
|
| 33 |
|
| 34 |
+
# 2. Отдаем твой готовый HTML-сайт (аналог YouTube) на главной странице
|
| 35 |
@app.get("/", response_class=HTMLResponse)
|
| 36 |
def read_index():
|
| 37 |
with open("index.html", "r", encoding="utf-8") as f:
|