Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 4 |
+
|
| 5 |
+
# Load StarCoder2 model
|
| 6 |
+
model_name = "bigcode/starcoder2-7b"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
|
| 9 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device_map="auto")
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# HTML and CSS embedded directly
|
| 14 |
+
html = """
|
| 15 |
+
<!DOCTYPE html>
|
| 16 |
+
<html lang="en">
|
| 17 |
+
<head>
|
| 18 |
+
<meta charset="UTF-8">
|
| 19 |
+
<title>StarCoder2 Docker Terminal</title>
|
| 20 |
+
<style>
|
| 21 |
+
body { background-color: #1e1e1e; color: #00ff99; font-family: monospace; margin: 0; height: 100vh; display:flex; justify-content:center; align-items:center; }
|
| 22 |
+
#terminal { width: 80%; height: 90%; }
|
| 23 |
+
</style>
|
| 24 |
+
<script src="https://cdn.jsdelivr.net/npm/xterm@5.3.0/lib/xterm.js"></script>
|
| 25 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/xterm@5.3.0/css/xterm.css" />
|
| 26 |
+
</head>
|
| 27 |
+
<body>
|
| 28 |
+
<div id="terminal"></div>
|
| 29 |
+
<script>
|
| 30 |
+
const term = new Terminal();
|
| 31 |
+
term.open(document.getElementById('terminal'));
|
| 32 |
+
term.write("🐳 StarCoder2 Terminal\\nroot@docker:~$ ");
|
| 33 |
+
|
| 34 |
+
const ws = new WebSocket(`ws://${window.location.host}/ws`);
|
| 35 |
+
|
| 36 |
+
term.onKey(e => {
|
| 37 |
+
if (e.domEvent.key === "Enter") {
|
| 38 |
+
const command = term.buffer.active.getLine(term.buffer.active.cursorY).translateToString().replace("root@docker:~$ ","").trim();
|
| 39 |
+
ws.send(command);
|
| 40 |
+
term.write("\\r\\n");
|
| 41 |
+
} else {
|
| 42 |
+
term.write(e.key);
|
| 43 |
+
}
|
| 44 |
+
});
|
| 45 |
+
|
| 46 |
+
ws.onmessage = (event) => {
|
| 47 |
+
term.write(event.data + "\\r\\nroot@docker:~$ ");
|
| 48 |
+
};
|
| 49 |
+
</script>
|
| 50 |
+
</body>
|
| 51 |
+
</html>
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
@app.get("/")
|
| 55 |
+
async def get_root():
|
| 56 |
+
return HTMLResponse(html)
|
| 57 |
+
|
| 58 |
+
@app.websocket("/ws")
|
| 59 |
+
async def websocket_endpoint(ws: WebSocket):
|
| 60 |
+
await ws.accept()
|
| 61 |
+
history = []
|
| 62 |
+
try:
|
| 63 |
+
while True:
|
| 64 |
+
user_input = await ws.receive_text()
|
| 65 |
+
prompt = "".join([f"User: {h[0]}\\nAssistant: {h[1]}\\n" for h in history])
|
| 66 |
+
prompt += f"User: {user_input}\\nAssistant:"
|
| 67 |
+
|
| 68 |
+
output = pipe(prompt, max_new_tokens=256, temperature=0.7, pad_token_id=tokenizer.eos_token_id)
|
| 69 |
+
reply = output[0]["generated_text"][len(prompt):].strip()
|
| 70 |
+
history.append((user_input, reply))
|
| 71 |
+
|
| 72 |
+
await ws.send_text(reply)
|
| 73 |
+
except WebSocketDisconnect:
|
| 74 |
+
print("Client disconnected")
|