A2Pro commited on
Commit
e259644
·
1 Parent(s): d7bd605

Update backend.py

Browse files
Files changed (1) hide show
  1. backend/backend.py +49 -49
backend/backend.py CHANGED
@@ -7,6 +7,7 @@ import openai
7
  from dotenv import load_dotenv
8
  from easycompletion import openai_text_call
9
  from fastapi import FastAPI, HTTPException
 
10
  from fastapi.middleware.gzip import GZipMiddleware
11
  from fastapi.testclient import TestClient
12
  from gradio import Interface, TabbedInterface, components, mount_gradio_app
@@ -15,30 +16,10 @@ from pygments import highlight
15
  from pygments.formatters import html
16
  from pygments.lexers import get_lexer_by_name
17
  from sqlalchemy import JSON, Column, Integer, String, create_engine
 
18
  from sqlalchemy.ext.declarative import declarative_base
19
  from sqlalchemy.orm import Session, sessionmaker
20
  from uvicorn import Config, Server
21
- from fuzzywuzzy import fuzz
22
-
23
- COMMANDS = {
24
- "scroll_up": "acc:UP",
25
- "scroll_down": "acc:DOWN",
26
- "click_first": "acc:CLICK FIRST",
27
- "click": "acc:CLICK"
28
- }
29
-
30
- def analyze_command(response):
31
- response_lower = response.lower()
32
-
33
- for command_type, command_text in COMMANDS.items():
34
- if fuzz.partial_ratio(response_lower, command_text.lower()) >= 90:
35
- return command_type
36
-
37
- return None
38
-
39
- def generate_command_response(command_type):
40
- return COMMANDS.get(command_type, None)
41
-
42
 
43
  LANGS = [
44
  "gpt-3.5-turbo",
@@ -247,33 +228,12 @@ async def saveurl(item: SaveURLItem):
247
 
248
  def assist_interface(uid, prompt, gpt_version):
249
  client = TestClient(app)
250
-
251
- modified_prompt = "If this text is asking you to either click on something, scroll up or down, or click on the first, return either acc:UP, acc:DOWN, acc:CLICK FIRST, or acc:CLICK. If it isn't, answer the question as normal. If it's asking you to click, click first, or scroll up or down, then return only acc:UP, acc:DOWN, acc:CLICK FIRST, or acc:CLICK, according to the command. Don't show anything before or after the command, nothing like 'Sure, here's the command you requested' just return the response. Here's the text:" + prompt
252
-
253
  response = client.post(
254
  "/assist",
255
- json={"uid": uid, "prompt": modified_prompt, "version": gpt_version},
256
  )
257
-
258
- response_data = json.loads(response.text)
259
- generated_text = response_data["text"]
260
-
261
- command_type = analyze_command(generated_text)
262
-
263
- if command_type:
264
- command_response = generate_command_response(command_type)
265
- add_command_response = client.post(
266
- "/add_command",
267
- json={"uid": uid, "command": command_response},
268
- )
269
-
270
- if add_command_response.status_code == 200:
271
- return add_command_response.json()
272
- else:
273
- return {"error": "Error adding command to the queue"}
274
- else:
275
- return generate_html_response_from_openai(response.text)
276
-
277
 
278
 
279
  def get_user_interface(uid):
@@ -346,7 +306,6 @@ def register_interface(openai_key):
346
  )
347
  return response.json()
348
 
349
-
350
  def get_register_interface():
351
  def wrapper(openai_key):
352
  result = register_interface(openai_key)
@@ -363,13 +322,11 @@ def get_register_interface():
363
  description="Register a new user by entering an OpenAI key.",
364
  )
365
 
366
-
367
  def get_history_interface(uid):
368
  client = TestClient(app)
369
  response = client.get(f"/get_history/{uid}")
370
  return response.json()
371
 
372
-
373
  def get_history_gradio_interface():
374
  return Interface(
375
  fn=get_history_interface,
@@ -379,7 +336,6 @@ def get_history_gradio_interface():
379
  description="Get the history of questions and answers for a given user.",
380
  )
381
 
382
-
383
  def add_command_interface(uid, command):
384
  client = TestClient(app)
385
  response = client.post(
@@ -388,6 +344,49 @@ def add_command_interface(uid, command):
388
  )
389
  return response.json()
390
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
 
392
  app = mount_gradio_app(
393
  app,
@@ -397,6 +396,7 @@ app = mount_gradio_app(
397
  get_db_interface(),
398
  get_register_interface(),
399
  get_history_gradio_interface(),
 
400
  ]
401
  ),
402
  path="/",
 
7
  from dotenv import load_dotenv
8
  from easycompletion import openai_text_call
9
  from fastapi import FastAPI, HTTPException
10
+ from fastapi import Request
11
  from fastapi.middleware.gzip import GZipMiddleware
12
  from fastapi.testclient import TestClient
13
  from gradio import Interface, TabbedInterface, components, mount_gradio_app
 
16
  from pygments.formatters import html
17
  from pygments.lexers import get_lexer_by_name
18
  from sqlalchemy import JSON, Column, Integer, String, create_engine
19
+ from fastapi.responses import FileResponse, JSONResponse
20
  from sqlalchemy.ext.declarative import declarative_base
21
  from sqlalchemy.orm import Session, sessionmaker
22
  from uvicorn import Config, Server
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  LANGS = [
25
  "gpt-3.5-turbo",
 
228
 
229
  def assist_interface(uid, prompt, gpt_version):
230
  client = TestClient(app)
231
+
 
 
232
  response = client.post(
233
  "/assist",
234
+ json={"uid": uid, "prompt": prompt, "version": gpt_version},
235
  )
236
+ return generate_html_response_from_openai(response.text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
 
238
 
239
  def get_user_interface(uid):
 
306
  )
307
  return response.json()
308
 
 
309
  def get_register_interface():
310
  def wrapper(openai_key):
311
  result = register_interface(openai_key)
 
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
  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
  )
345
  return response.json()
346
 
347
+ @app.get("/.well-known/ai-plugin.json")
348
+ async def plugin_manifest(request: Request):
349
+ host = request.headers['host']
350
+ with open(".well-known/ai-plugin.json") as f:
351
+ text = f.read().replace("PLUGIN_HOSTNAME", "https://posix4e-puppet.hf.space/")
352
+ return JSONResponse(content=json.loads(text))
353
+
354
+
355
+
356
+ @app.get("/openapi.yaml")
357
+ async def openai_yaml(request: Request):
358
+ host = request.headers['host']
359
+ with open(".well-known/openapi.yaml") as f:
360
+ text = f.read().replace("PLUGIN_HOSTNAME","https://posix4e-puppet.hf.space/")
361
+ return JSONResponse(content=json.loads(text))
362
+
363
+
364
+
365
+ @app.get("/detectcommand/{command}")
366
+ async def get_command(command: str, item: AssistItem):
367
+ db: Session = SessionLocal()
368
+ user = db.query(User).filter(User.uid == item.uid).first()
369
+ if not user:
370
+ raise HTTPException(status_code=400, detail="Invalid uid")
371
+ openai.api_key = user.openai_key
372
+ response = openai_text_call(item.prompt, model=item.version)
373
+ return JSONResponse(content= response, status_code=200)
374
+
375
+ @app.get("/logo.png")
376
+ async def plugin_logo():
377
+ return FileResponse('/.well-known/logo.jpeg')
378
+
379
+ def get_add_command_interface():
380
+ return Interface(
381
+ fn=add_command_interface,
382
+ inputs=[
383
+ components.Textbox(label="UID", type="text"),
384
+ components.Textbox(label="Command", type="text"),
385
+ ],
386
+ outputs="json",
387
+ title="Add Command",
388
+ description="Add a new command for a given user.",
389
+ )
390
 
391
  app = mount_gradio_app(
392
  app,
 
396
  get_db_interface(),
397
  get_register_interface(),
398
  get_history_gradio_interface(),
399
+ get_add_command_interface(),
400
  ]
401
  ),
402
  path="/",