Spaces:
Runtime error
Runtime error
It was working. Now testing by adding webinterface in docker without gradio.
Browse files
app.py
CHANGED
|
@@ -1,21 +1,50 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from gradio_client import Client
|
| 4 |
|
| 5 |
-
app = FastAPI(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
class Message(BaseModel):
|
| 8 |
text: str
|
| 9 |
|
| 10 |
-
@app.get("/")
|
| 11 |
-
def read_root():
|
| 12 |
-
return
|
| 13 |
|
| 14 |
@app.post("/ask")
|
| 15 |
-
async def ask_searchgpt(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
client = Client("https://umint-searchgpt.hf.space/")
|
| 17 |
result = client.predict(
|
| 18 |
user_message={"text": msg.text},
|
| 19 |
api_name="/public"
|
| 20 |
)
|
| 21 |
return {"response": result}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Header, HTTPException, Request, Form
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from fastapi.templating import Jinja2Templates
|
| 4 |
+
from fastapi.staticfiles import StaticFiles
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from gradio_client import Client
|
| 7 |
|
| 8 |
+
app = FastAPI(
|
| 9 |
+
title="SearchGPT Relay API",
|
| 10 |
+
description="A simple wrapper around SearchGPT using gradio_client",
|
| 11 |
+
version="1.0.0"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
API_KEY = "your-secret-api-key"
|
| 15 |
+
|
| 16 |
+
templates = Jinja2Templates(directory="templates")
|
| 17 |
|
| 18 |
class Message(BaseModel):
|
| 19 |
text: str
|
| 20 |
|
| 21 |
+
@app.get("/", response_class=HTMLResponse)
|
| 22 |
+
def read_root(request: Request):
|
| 23 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
| 24 |
|
| 25 |
@app.post("/ask")
|
| 26 |
+
async def ask_searchgpt(
|
| 27 |
+
msg: Message,
|
| 28 |
+
x_api_key: str = Header(..., description="Your API key")
|
| 29 |
+
):
|
| 30 |
+
if x_api_key != API_KEY:
|
| 31 |
+
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 32 |
+
|
| 33 |
client = Client("https://umint-searchgpt.hf.space/")
|
| 34 |
result = client.predict(
|
| 35 |
user_message={"text": msg.text},
|
| 36 |
api_name="/public"
|
| 37 |
)
|
| 38 |
return {"response": result}
|
| 39 |
+
|
| 40 |
+
@app.post("/web", response_class=HTMLResponse)
|
| 41 |
+
async def ask_via_web(request: Request, apikey: str = Form(...), text: str = Form(...)):
|
| 42 |
+
if apikey != API_KEY:
|
| 43 |
+
return HTMLResponse(content="<h3>Invalid API Key</h3>", status_code=401)
|
| 44 |
+
|
| 45 |
+
client = Client("https://umint-searchgpt.hf.space/")
|
| 46 |
+
result = client.predict(
|
| 47 |
+
user_message={"text": text},
|
| 48 |
+
api_name="/public"
|
| 49 |
+
)
|
| 50 |
+
return HTMLResponse(content=f"<h3>Response:</h3><p>{result}</p>")
|