-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
97 lines (66 loc) · 3.24 KB
/
server.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from flask import Flask, request
import subprocess
import shutil
import os
import urllib.parse
app = Flask(__name__)
# Run 'whoami' and get the username
username = subprocess.check_output("whoami", shell=True).decode().strip().split("\\")[-1]
# Construct the path using the username
replays_path = f"/mnt/c/Users/{username}/OneDrive/Documents/My Games/Rocket League/TAGame/DemosEpic/"
# Construct the path using the username
replays_path = f"/mnt/c/Users/{username}/OneDrive/Documents/My Games/Rocket League/TAGame/DemosEpic/"
replays_dest = "./replays"
if not os.path.exists(replays_dest):
os.makedirs(replays_dest)
@app.route('/replays/<guid>', methods=['GET'])
def handle_replay(guid):
print("Got request for ",guid)
source_file = os.path.join(replays_path, guid + ".replay")
destination_file = os.path.join(replays_dest, guid + ".replay")
try:
if not os.path.isfile(source_file):
return f"Replay file for GUID {guid} not found at {source_file}.", 404
shutil.copy(source_file, destination_file)
except Exception as e:
return f"Error copying file: {str(e)}", 500
command = ["./ReplayAssistant", "prepare", destination_file]
try:
env = os.environ.copy()
print("Running ReplayAssistant prepare...")
result = subprocess.run(command, check=True, env=env, capture_output=True, text=True)
print(result)
# Split the output into lines and get the last one (which should be the assistant_id)
output_lines = result.stdout.splitlines()
thread_id = output_lines[-1] if output_lines else "Unknown"
print("ReplayAssistant Thread ID ",thread_id)
return f"{thread_id}", 200
except subprocess.CalledProcessError as e:
return f"Command failed with error: {e}", 500
@app.route('/messages/<thread_id>/', methods=['GET'])
def messages(thread_id):
print("Got request for ",thread_id)
command = ["./ReplayAssistant", "messages", thread_id]
try:
env = os.environ.copy()
print("Running ReplayAssistant messages...")
result = subprocess.run(command, check=True, env=env, capture_output=True, text=True)
print("ReplayAssistant Thread Messages ",result.stdout)
return f"{result.stdout}", 200
except subprocess.CalledProcessError as e:
return f"Command failed with error: {e}", 500
@app.route('/query/<assistant_id>/<thread_id>/<query>', methods=['GET'])
def query(assistant_id,thread_id, query):
decoded_query = urllib.parse.unquote(query)
print("Got request for ",assistant_id,thread_id, decoded_query)
command = ["./ReplayAssistant", "prompt", assistant_id,thread_id, decoded_query]
try:
env = os.environ.copy()
print("Running ReplayAssistant prompt...")
result = subprocess.run(command, check=True, env=env, capture_output=True, text=True)
print("ReplayAssistant Thread Messages ",result)
return f"{result}", 200
except subprocess.CalledProcessError as e:
return f"Command failed with error: {e}", 500
if __name__ == '__main__':
app.run(host='localhost', port=5000)