Spaces:
Runtime error
Runtime error
| 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 | |
| def read_root(request: Request): | |
| return templates.TemplateResponse("index.html", {"request": request}) | |
| 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} | |
| 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>") | |