A2Pro commited on
Commit ·
f4390dc
1
Parent(s): 52fe63a
Update backend.py
Browse files- backend/backend.py +56 -11
backend/backend.py
CHANGED
|
@@ -18,6 +18,27 @@ 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 |
|
| 22 |
LANGS = [
|
| 23 |
"gpt-3.5-turbo",
|
|
@@ -226,21 +247,33 @@ async def saveurl(item: SaveURLItem):
|
|
| 226 |
|
| 227 |
def assist_interface(uid, prompt, gpt_version):
|
| 228 |
client = TestClient(app)
|
| 229 |
-
|
| 230 |
-
|
|
|
|
| 231 |
response = client.post(
|
| 232 |
"/assist",
|
| 233 |
-
json={"uid": uid, "prompt":
|
| 234 |
)
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
else:
|
| 243 |
-
|
|
|
|
| 244 |
|
| 245 |
|
| 246 |
def get_user_interface(uid):
|
|
@@ -356,6 +389,18 @@ def add_command_interface(uid, command):
|
|
| 356 |
return response.json()
|
| 357 |
|
| 358 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
app = mount_gradio_app(
|
| 361 |
app,
|
|
|
|
| 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 |
|
| 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):
|
|
|
|
| 389 |
return response.json()
|
| 390 |
|
| 391 |
|
| 392 |
+
def get_add_command_interface():
|
| 393 |
+
return Interface(
|
| 394 |
+
fn=add_command_interface,
|
| 395 |
+
inputs=[
|
| 396 |
+
components.Textbox(label="UID", type="text"),
|
| 397 |
+
components.Textbox(label="Command", type="text"),
|
| 398 |
+
],
|
| 399 |
+
outputs="json",
|
| 400 |
+
title="Add Command",
|
| 401 |
+
description="Add a new command for a given user.",
|
| 402 |
+
)
|
| 403 |
+
|
| 404 |
|
| 405 |
app = mount_gradio_app(
|
| 406 |
app,
|