-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
165 lines (140 loc) · 6.09 KB
/
server.py
File metadata and controls
165 lines (140 loc) · 6.09 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import sys
import ollama
import re
import socket
import torch
from TTS.api import TTS
from pydub import AudioSegment
import random
import time
import threading
"""Server file"""
device = "cuda" if torch.cuda.is_available() else "cpu"
def trim_to_full_sentences(text):
# Define sentence-ending characters
sentence_endings = {'.', '?', '!'}
last_index = -1
for i in range(len(text) - 1, -1, -1):
if text[i] in sentence_endings:
last_index = i
break
return text[:last_index + 1] if last_index != -1 else text
def generate_response(history, model_name="innosama", stream=False):
response = ollama.chat(model=model_name, messages=history, stream=stream)
resp = ""
if stream:
for chunk in response:
resp += chunk['message']['content']
print(chunk['message']['content'], end='', flush=True)
print()
else:
return response['message']['content']
def get_response(prompt, model_name="innosama", cut=True, stream=False):
global conversation_history
conversation_history.append({'role': 'user', 'content': prompt})
response = generate_response(conversation_history, model_name)
if cut:
response = trim_to_full_sentences(response)
conversation_history.append({'role': 'assistant', 'content': response})
return response
def split_into_sentences(text):
return re.split(r'(?<=[.!?])\s+', text)
def response2audio(response):
global tts
response = response.replace("«", "").replace("»", "")
sentences = split_into_sentences(response)
audio_files = []
for i, sentence in enumerate(sentences):
file_path = f"sentence_{i}.wav"
tts.tts_to_file(text=sentence, speaker_wav="sam.wav", language="en", file_path=file_path, speed=1.2)
audio_files.append(file_path)
final_audio = AudioSegment.silent(duration=0)
silence_between = 0
trim_duration = 300
for audio_file in audio_files:
sentence_audio = AudioSegment.from_wav(audio_file)
if trim_duration > 0:
sentence_audio = sentence_audio[:-trim_duration]
final_audio += sentence_audio + AudioSegment.silent(duration=silence_between)
final_audio.export("final_output.wav", format="wav")
def get_sudden_response():
r = random.randint(1, 10)
if r <= 5:
conversation_history.append({'role': 'SYSTEM', 'content': "say something random to continue topic"})
print("continuing topic")
else:
conversation_history.append({'role': 'SYSTEM', 'content': "say something random to change topic"})
print("changing topic")
return get_response("", model_name)
def discard_sudden_response():
global conversation_history
if len(conversation_history) > 0 and conversation_history[-1]['role'] == "SYSTEM":
conversation_history = conversation_history[:-1]
elif len(conversation_history) > 1 and conversation_history[-2]['role'] == "SYSTEM":
conversation_history = conversation_history[:-2]
if __name__ == "__main__":
model_name = "innosama"
HOST = '0.0.0.0'
PORT = 12345
conversation_history = []
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((HOST, PORT))
server_socket.listen(1)
print(f"Server Launched on {HOST}:{PORT}, awaiting connections...")
data = ""
while True:
try:
client_socket, addr = server_socket.accept()
last_received_time = time.time()
print(f"Connected: {addr}")
while True:
# Check for timeout and trigger sudden response if needed
if time.time() - last_received_time > 44:
print("Timeout detected, generating sudden response...")
response = get_sudden_response()
last_received_time = time.time() # Reset the timeout clock
response2audio(response)
with open("final_output.wav", "rb") as f:
audio_data = f.read()
client_socket.sendall(audio_data)
print(f"Sent response: {response}")
try:
# Use socket timeout for non-blocking checks
client_socket.settimeout(1)
data = client_socket.recv(1024).decode('utf-8')
if len(data) == 0:
continue
if data == "DISCARD_SUDDEN_RESPONSE":
last_received_time = time.time()
discard_sudden_response() # Call the function to handle discard logic
continue
print(f"Receiving data: {data}")
if data == "bye":
print(f"Client {addr} ended the session.")
break
last_received_time = time.time()
response = get_response(data, model_name)
response2audio(response)
with open("final_output.wav", "rb") as f:
audio_data = f.read()
client_socket.sendall(audio_data)
print(f"Sent response: {response}")
except socket.timeout:
# Ignore socket timeout to continue checking for data or timeout
pass
except ConnectionResetError:
print(f"Client {addr} forcibly closed the connection.")
conversation_history = []
break
except Exception as e:
print(f"Error handling client {addr}: {e}")
conversation_history = []
break
except Exception as e:
print(f"Error accepting connection: {e}")
finally:
try:
client_socket.close()
except NameError:
pass