reformat
Browse files- backend/backend.py +23 -17
backend/backend.py
CHANGED
|
@@ -306,6 +306,7 @@ def register_interface(openai_key):
|
|
| 306 |
)
|
| 307 |
return response.json()
|
| 308 |
|
|
|
|
| 309 |
def get_register_interface():
|
| 310 |
def wrapper(openai_key):
|
| 311 |
result = register_interface(openai_key)
|
|
@@ -322,11 +323,13 @@ def get_register_interface():
|
|
| 322 |
description="Register a new user by entering an OpenAI key.",
|
| 323 |
)
|
| 324 |
|
|
|
|
| 325 |
def get_history_interface(uid):
|
| 326 |
client = TestClient(app)
|
| 327 |
response = client.get(f"/get_history/{uid}")
|
| 328 |
return response.json()
|
| 329 |
|
|
|
|
| 330 |
def get_history_gradio_interface():
|
| 331 |
return Interface(
|
| 332 |
fn=get_history_interface,
|
|
@@ -336,6 +339,7 @@ def get_history_gradio_interface():
|
|
| 336 |
description="Get the history of questions and answers for a given user.",
|
| 337 |
)
|
| 338 |
|
|
|
|
| 339 |
def add_command_interface(uid, command):
|
| 340 |
client = TestClient(app)
|
| 341 |
response = client.post(
|
|
@@ -344,37 +348,38 @@ def add_command_interface(uid, command):
|
|
| 344 |
)
|
| 345 |
return response.json()
|
| 346 |
|
|
|
|
| 347 |
@app.get("/.well-known/ai-plugin.json")
|
| 348 |
async def plugin_manifest(request: Request):
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
|
| 355 |
|
| 356 |
@app.get("/openapi.yaml")
|
| 357 |
async def openai_yaml(request: Request):
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
|
| 364 |
|
| 365 |
@app.get("/detectcommand/{command}")
|
| 366 |
async def get_command(command: str, item: AssistItem):
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
raise HTTPException(status_code=400, detail="Invalid uid")
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
|
|
|
| 374 |
|
| 375 |
@app.get("/logo.png")
|
| 376 |
async def plugin_logo():
|
| 377 |
-
|
|
|
|
| 378 |
|
| 379 |
def get_add_command_interface():
|
| 380 |
return Interface(
|
|
@@ -388,6 +393,7 @@ def get_add_command_interface():
|
|
| 388 |
description="Add a new command for a given user.",
|
| 389 |
)
|
| 390 |
|
|
|
|
| 391 |
app = mount_gradio_app(
|
| 392 |
app,
|
| 393 |
TabbedInterface(
|
|
|
|
| 306 |
)
|
| 307 |
return response.json()
|
| 308 |
|
| 309 |
+
|
| 310 |
def get_register_interface():
|
| 311 |
def wrapper(openai_key):
|
| 312 |
result = register_interface(openai_key)
|
|
|
|
| 323 |
description="Register a new user by entering an OpenAI key.",
|
| 324 |
)
|
| 325 |
|
| 326 |
+
|
| 327 |
def get_history_interface(uid):
|
| 328 |
client = TestClient(app)
|
| 329 |
response = client.get(f"/get_history/{uid}")
|
| 330 |
return response.json()
|
| 331 |
|
| 332 |
+
|
| 333 |
def get_history_gradio_interface():
|
| 334 |
return Interface(
|
| 335 |
fn=get_history_interface,
|
|
|
|
| 339 |
description="Get the history of questions and answers for a given user.",
|
| 340 |
)
|
| 341 |
|
| 342 |
+
|
| 343 |
def add_command_interface(uid, command):
|
| 344 |
client = TestClient(app)
|
| 345 |
response = client.post(
|
|
|
|
| 348 |
)
|
| 349 |
return response.json()
|
| 350 |
|
| 351 |
+
|
| 352 |
@app.get("/.well-known/ai-plugin.json")
|
| 353 |
async def plugin_manifest(request: Request):
|
| 354 |
+
host = request.headers["host"]
|
| 355 |
+
with open(".well-known/ai-plugin.json") as f:
|
| 356 |
+
text = f.read().replace("PLUGIN_HOSTNAME", "https://posix4e-puppet.hf.space/")
|
| 357 |
+
return JSONResponse(content=json.loads(text))
|
|
|
|
| 358 |
|
| 359 |
|
| 360 |
@app.get("/openapi.yaml")
|
| 361 |
async def openai_yaml(request: Request):
|
| 362 |
+
host = request.headers["host"]
|
| 363 |
+
with open(".well-known/openapi.yaml") as f:
|
| 364 |
+
text = f.read().replace("PLUGIN_HOSTNAME", "https://posix4e-puppet.hf.space/")
|
| 365 |
+
return JSONResponse(content=json.loads(text))
|
|
|
|
| 366 |
|
| 367 |
|
| 368 |
@app.get("/detectcommand/{command}")
|
| 369 |
async def get_command(command: str, item: AssistItem):
|
| 370 |
+
db: Session = SessionLocal()
|
| 371 |
+
user = db.query(User).filter(User.uid == item.uid).first()
|
| 372 |
+
if not user:
|
| 373 |
raise HTTPException(status_code=400, detail="Invalid uid")
|
| 374 |
+
openai.api_key = user.openai_key
|
| 375 |
+
response = openai_text_call(item.prompt, model=item.version)
|
| 376 |
+
return JSONResponse(content=response, status_code=200)
|
| 377 |
+
|
| 378 |
|
| 379 |
@app.get("/logo.png")
|
| 380 |
async def plugin_logo():
|
| 381 |
+
return FileResponse("/.well-known/logo.jpeg")
|
| 382 |
+
|
| 383 |
|
| 384 |
def get_add_command_interface():
|
| 385 |
return Interface(
|
|
|
|
| 393 |
description="Add a new command for a given user.",
|
| 394 |
)
|
| 395 |
|
| 396 |
+
|
| 397 |
app = mount_gradio_app(
|
| 398 |
app,
|
| 399 |
TabbedInterface(
|