Spaces:
Runtime error
Runtime error
Remove gradio_app.py file and its associated functionality
Browse files- gradio_app.py +0 -147
gradio_app.py
DELETED
@@ -1,147 +0,0 @@
|
|
1 |
-
import asyncio
|
2 |
-
import base64
|
3 |
-
import json
|
4 |
-
import shlex
|
5 |
-
|
6 |
-
import gradio as gr
|
7 |
-
from gradio.oauth import attach_oauth, OAuthToken
|
8 |
-
from huggingface_hub import HfApi
|
9 |
-
|
10 |
-
from src.team import TeamChatSession
|
11 |
-
from src.db import list_sessions_info
|
12 |
-
|
13 |
-
# Store active chat sessions
|
14 |
-
_SESSIONS: dict[tuple[str, str], TeamChatSession] = {}
|
15 |
-
_API = HfApi()
|
16 |
-
|
17 |
-
|
18 |
-
def _username(token: OAuthToken) -> str:
|
19 |
-
"""Return the username for the given token."""
|
20 |
-
info = _API.whoami(token.token)
|
21 |
-
return info.get("name") or info.get("user", "unknown")
|
22 |
-
|
23 |
-
|
24 |
-
async def _get_chat(user: str, session: str) -> TeamChatSession:
|
25 |
-
"""Return an active :class:`TeamChatSession` for ``user`` and ``session``."""
|
26 |
-
key = (user, session)
|
27 |
-
chat = _SESSIONS.get(key)
|
28 |
-
if chat is None:
|
29 |
-
chat = TeamChatSession(user=user, session=session)
|
30 |
-
await chat.__aenter__()
|
31 |
-
_SESSIONS[key] = chat
|
32 |
-
return chat
|
33 |
-
|
34 |
-
|
35 |
-
async def _vm_execute(user: str, session: str, command: str) -> str:
|
36 |
-
"""Execute ``command`` inside the user's VM and return output."""
|
37 |
-
chat = await _get_chat(user, session)
|
38 |
-
vm = getattr(chat.senior, "_vm", None)
|
39 |
-
if vm is None:
|
40 |
-
raise RuntimeError("VM not running")
|
41 |
-
return await vm.execute_async(command, timeout=5)
|
42 |
-
|
43 |
-
|
44 |
-
async def send_message(
|
45 |
-
message: str, history: list[tuple[str, str]], session: str, token: OAuthToken
|
46 |
-
):
|
47 |
-
user = _username(token)
|
48 |
-
chat = await _get_chat(user, session)
|
49 |
-
history = history or []
|
50 |
-
history.append((message, ""))
|
51 |
-
async for part in chat.chat_stream(message):
|
52 |
-
history[-1] = (message, history[-1][1] + part)
|
53 |
-
yield history
|
54 |
-
|
55 |
-
|
56 |
-
def load_sessions(token: OAuthToken):
|
57 |
-
user = _username(token)
|
58 |
-
infos = list_sessions_info(user)
|
59 |
-
names = [info["name"] for info in infos]
|
60 |
-
table = [[info["name"], info["last_message"]] for info in infos]
|
61 |
-
value = names[0] if names else "default"
|
62 |
-
return gr.update(choices=names or ["default"], value=value), table
|
63 |
-
|
64 |
-
|
65 |
-
async def list_dir(path: str, session: str, token: OAuthToken):
|
66 |
-
user = _username(token)
|
67 |
-
cmd = f"ls -1ap {shlex.quote(path)}"
|
68 |
-
output = await _vm_execute(user, session, cmd)
|
69 |
-
if output.startswith("ls:"):
|
70 |
-
return []
|
71 |
-
entries: list[list[str | bool]] = []
|
72 |
-
for line in output.splitlines():
|
73 |
-
line = line.strip()
|
74 |
-
if not line or line in (".", ".."):
|
75 |
-
continue
|
76 |
-
is_dir = line.endswith("/")
|
77 |
-
name = line[:-1] if is_dir else line
|
78 |
-
entries.append([name, is_dir])
|
79 |
-
return entries
|
80 |
-
|
81 |
-
|
82 |
-
async def read_file(path: str, session: str, token: OAuthToken):
|
83 |
-
user = _username(token)
|
84 |
-
cmd = f"cat {shlex.quote(path)}"
|
85 |
-
return await _vm_execute(user, session, cmd)
|
86 |
-
|
87 |
-
|
88 |
-
async def save_file(path: str, content: str, session: str, token: OAuthToken):
|
89 |
-
user = _username(token)
|
90 |
-
encoded = base64.b64encode(content.encode()).decode()
|
91 |
-
cmd = (
|
92 |
-
f"python -c 'import base64,os; "
|
93 |
-
f'open({json.dumps(path)}, "wb").write(base64.b64decode({json.dumps(encoded)}))\''
|
94 |
-
)
|
95 |
-
await _vm_execute(user, session, cmd)
|
96 |
-
return "Saved"
|
97 |
-
|
98 |
-
|
99 |
-
async def delete_path(path: str, session: str, token: OAuthToken):
|
100 |
-
user = _username(token)
|
101 |
-
cmd = (
|
102 |
-
f"bash -c 'if [ -d {shlex.quote(path)} ]; then rm -rf {shlex.quote(path)} && echo Deleted; "
|
103 |
-
f"elif [ -e {shlex.quote(path)} ]; then rm -f {shlex.quote(path)} && echo Deleted; "
|
104 |
-
f"else echo File not found; fi'"
|
105 |
-
)
|
106 |
-
return await _vm_execute(user, session, cmd)
|
107 |
-
|
108 |
-
|
109 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
110 |
-
attach_oauth(demo.app)
|
111 |
-
|
112 |
-
login_btn = gr.LoginButton()
|
113 |
-
|
114 |
-
with gr.Tab("Chat"):
|
115 |
-
session_dd = gr.Dropdown(["default"], label="Session", value="default")
|
116 |
-
refresh = gr.Button("Refresh Sessions")
|
117 |
-
chatbox = gr.Chatbot(type="messages")
|
118 |
-
msg = gr.Textbox(label="Message")
|
119 |
-
send = gr.Button("Send")
|
120 |
-
|
121 |
-
with gr.Tab("Files"):
|
122 |
-
dir_path = gr.Textbox(label="Directory", value="/")
|
123 |
-
list_btn = gr.Button("List")
|
124 |
-
table = gr.Dataframe(headers=["name", "is_dir"], datatype=["str", "bool"])
|
125 |
-
file_path = gr.Textbox(label="File Path")
|
126 |
-
load_btn = gr.Button("Load")
|
127 |
-
content = gr.Code(label="Content", language=None)
|
128 |
-
save_btn = gr.Button("Save")
|
129 |
-
del_btn = gr.Button("Delete")
|
130 |
-
|
131 |
-
refresh.click(load_sessions, outputs=[session_dd, table])
|
132 |
-
send_click = send.click(
|
133 |
-
send_message,
|
134 |
-
inputs=[msg, chatbox, session_dd],
|
135 |
-
outputs=chatbox,
|
136 |
-
)
|
137 |
-
list_btn.click(list_dir, inputs=[dir_path, session_dd], outputs=table)
|
138 |
-
load_btn.click(read_file, inputs=[file_path, session_dd], outputs=content)
|
139 |
-
save_btn.click(save_file, inputs=[file_path, content, session_dd], outputs=content)
|
140 |
-
del_btn.click(delete_path, inputs=[file_path, session_dd], outputs=content)
|
141 |
-
send_click.then(lambda: "", None, msg)
|
142 |
-
|
143 |
-
|
144 |
-
demo.queue()
|
145 |
-
|
146 |
-
if __name__ == "__main__":
|
147 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|