-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference_server.py
75 lines (58 loc) · 1.96 KB
/
inference_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
import socket
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
model_name = "/home/dockuser1/users/Interns/kartik-final/7b1"
model = AutoModelForCausalLM.from_pretrained(
model_name,
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
model.use_cache = True
model.eval()
def format_instruction(sample):
return f"""### Instruction:
You are a coding assistant that will write a Solution to resolve the following
Task:
### Task:
{sample}
### Solution:
""".strip()
def chatbot_response(query):
final = format_instruction(query)
pipe = pipeline(
task="text-generation",
model=model,
tokenizer=tokenizer,
torch_dtype=torch.float16,
repetition_penalty=1.1
)
result = pipe(
f"{final}",
do_sample=True,
top_p=0.5,
temperature=0.5,
top_k=10,
num_return_sequences=1,
eos_token_id=tokenizer.eos_token_id,
max_length=2048
)
return result[0]['generated_text']
def main():
host = '0.0.0.0' # Listen on all available network interfaces
port = 12345 # Choose a port for your server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen(1) # Listen for a single client connection
print(f"Server is listening on {host}:{port}")
while True:
client_socket, client_address = server.accept()
print(f"Accepted connection from {client_address}")
while True:
data = client_socket.recv(1024).decode('utf-8')
if not data:
break # Connection closed by the client
response = chatbot_response(data)
client_socket.send(response.encode('utf-8'))
client_socket.close()
if __name__ == '__main__':
main()