-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
79 lines (57 loc) · 2.54 KB
/
Copy pathserver.py
File metadata and controls
79 lines (57 loc) · 2.54 KB
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
from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*")
code_content = {"text": "", "language": "python"}
@app.route("/")
def index():
return render_template("index.html")
@socketio.on("code_update")
def handle_code_update(data):
code_content["text"] = data["code"]
emit("update_code", data, broadcast=True)
@socketio.on("language_change")
def handle_language_change(data):
code_content["language"] = data["language"]
emit("update_language", data, broadcast=True)
MODEL_NAME = "bigcode/santacoder"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16, device_map="auto")
@app.route("/debug", methods=["POST"])
def debug_code():
"""AI-assisted debugging using SantaCoder (lightweight model)."""
user_code = request.json.get("code")
language = request.json.get("language")
print("Received Code for Debugging")
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
prompt = f"Analyze the following {language} code for syntax errors and optimizations:\n\n" \
f"```{language}\n{user_code}\n```\n\nProvide structured suggestions."
device = "cuda" if torch.cuda.is_available() else "cpu"
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True).to(device)
model.config.pad_token_id = tokenizer.pad_token_id
stopping_criteria = torch.tensor([tokenizer.eos_token_id], device=device)
print("Generating Debugging Suggestions...")
try:
output = model.generate(
**inputs,
max_length=256,
do_sample=True,
top_k=50,
top_p=0.95,
temperature=0.7,
num_return_sequences=1,
eos_token_id=tokenizer.eos_token_id
)
print("Model Output Generated")
suggestion = tokenizer.decode(output[0], skip_special_tokens=True).strip()
if not suggestion or suggestion == prompt:
suggestion = "No issues detected, or AI couldn't analyze the code properly."
return {"suggestion": suggestion}
except Exception as e:
print("Error in AI Debugging:", str(e))
return {"suggestion": "AI debugging failed due to an error."}
if __name__ == "__main__":
socketio.run(app, debug=True, host="0.0.0.0", port=5000)