File size: 1,580 Bytes
c471691
 
 
 
0f9e4fa
13ff441
 
c471691
 
 
 
 
 
af60249
c471691
 
0f9e4fa
 
 
 
c471691
 
 
d31c706
0f9e4fa
c471691
 
 
 
 
 
 
13ff441
 
0f9e4fa
13ff441
 
0f9e4fa
c471691
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, Header, HTTPException, Request, Form
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from gradio_client import Client

app = FastAPI(
    title="SearchGPT Relay API",
    description="A simple wrapper around SearchGPT using gradio_client",
    version="1.0.0"
)

API_KEY = "" # "your-secret-api-key"

templates = Jinja2Templates(directory="templates")

class Message(BaseModel):
    text: str

@app.get("/", response_class=HTMLResponse)
def read_root(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})

@app.post("/ask")
async def ask_searchgpt(
    msg: Message,
    x_api_key: str = Header(..., description="Your API key")
):
    if x_api_key != API_KEY:
        raise HTTPException(status_code=401, detail="Invalid API key")

    client = Client("https://umint-searchgpt.hf.space/")
    result = client.predict(
        user_message={"text": msg.text},
        api_name="/public"
    )
    return {"response": result}

@app.post("/web", response_class=HTMLResponse)
async def ask_via_web(request: Request, apikey: str = Form(...), text: str = Form(...)):
    if apikey != API_KEY:
        return HTMLResponse(content="<h3>Invalid API Key</h3>", status_code=401)

    client = Client("https://umint-searchgpt.hf.space/")
    result = client.predict(
        user_message={"text": text},
        api_name="/public"
    )
    return HTMLResponse(content=f"<h3>Response:</h3><p>{result}</p>")