Update voice_chat.py
Browse files- voice_chat.py +78 -7
voice_chat.py
CHANGED
@@ -9,6 +9,69 @@ import torch
|
|
9 |
import sentencepiece as spm
|
10 |
import onnxruntime as ort
|
11 |
from huggingface_hub import hf_hub_download, InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Speech Recognition Model Configuration
|
14 |
model_name = "neongeckocom/stt_en_citrinet_512_gamma_0_25"
|
@@ -21,7 +84,7 @@ tokenizer = spm.SentencePieceProcessor(hf_hub_download(model_name, "tokenizer.sp
|
|
21 |
|
22 |
# Mistral Model Configuration
|
23 |
client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
24 |
-
system_instructions1 = "[SYSTEM] Answer as Real OpenGPT 4o, Made by 'KingNish', Keep conversation very short, clear, friendly and concise. The text provided is a request for a specific type of response from you, the virtual assistant.
|
25 |
|
26 |
def resample(audio_fp32, sr):
|
27 |
return soxr.resample(audio_fp32, sr, sample_rate)
|
@@ -49,14 +112,22 @@ def transcribe(audio_path):
|
|
49 |
|
50 |
return text
|
51 |
|
52 |
-
def model(text):
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
async def respond(audio):
|
58 |
user = transcribe(audio)
|
59 |
-
reply = model(user)
|
60 |
communicate = edge_tts.Communicate(reply)
|
61 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
62 |
tmp_path = tmp_file.name
|
|
|
9 |
import sentencepiece as spm
|
10 |
import onnxruntime as ort
|
11 |
from huggingface_hub import hf_hub_download, InferenceClient
|
12 |
+
import requests
|
13 |
+
from bs4 import BeautifulSoup
|
14 |
+
import urllib
|
15 |
+
|
16 |
+
def extract_text_from_webpage(html_content):
|
17 |
+
"""Extracts visible text from HTML content using BeautifulSoup."""
|
18 |
+
soup = BeautifulSoup(html_content, "html.parser")
|
19 |
+
# Remove unwanted tags
|
20 |
+
for tag in soup(["script", "style", "header", "footer", "nav"]):
|
21 |
+
tag.extract()
|
22 |
+
# Get the remaining visible text
|
23 |
+
visible_text = soup.get_text(strip=True)
|
24 |
+
return visible_text
|
25 |
+
|
26 |
+
# Perform a Google search and return the results
|
27 |
+
def search(term, num_results=3, lang="en", advanced=True, timeout=5, safe="active", ssl_verify=None):
|
28 |
+
"""Performs a Google search and returns the results."""
|
29 |
+
escaped_term = urllib.parse.quote_plus(term)
|
30 |
+
start = 0
|
31 |
+
all_results = []
|
32 |
+
# Limit the number of characters from each webpage to stay under the token limit
|
33 |
+
max_chars_per_page = 3000 # Adjust this value based on your token limit and average webpage length
|
34 |
+
|
35 |
+
with requests.Session() as session:
|
36 |
+
while start < num_results:
|
37 |
+
resp = session.get(
|
38 |
+
url="https://www.google.com/search",
|
39 |
+
headers={"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62'},
|
40 |
+
params={
|
41 |
+
"q": term,
|
42 |
+
"num": num_results - start,
|
43 |
+
"hl": lang,
|
44 |
+
"start": start,
|
45 |
+
"safe": safe,
|
46 |
+
},
|
47 |
+
timeout=timeout,
|
48 |
+
verify=ssl_verify,
|
49 |
+
)
|
50 |
+
resp.raise_for_status()
|
51 |
+
soup = BeautifulSoup(resp.text, "html.parser")
|
52 |
+
result_block = soup.find_all("div", attrs={"class": "g"})
|
53 |
+
if not result_block:
|
54 |
+
start += 1
|
55 |
+
continue
|
56 |
+
for result in result_block:
|
57 |
+
link = result.find("a", href=True)
|
58 |
+
if link:
|
59 |
+
link = link["href"]
|
60 |
+
try:
|
61 |
+
webpage = session.get(link, headers={"User-Agent": get_useragent()})
|
62 |
+
webpage.raise_for_status()
|
63 |
+
visible_text = extract_text_from_webpage(webpage.text)
|
64 |
+
# Truncate text if it's too long
|
65 |
+
if len(visible_text) > max_chars_per_page:
|
66 |
+
visible_text = visible_text[:max_chars_per_page] + "..."
|
67 |
+
all_results.append({"text": visible_text})
|
68 |
+
except requests.exceptions.RequestException as e:
|
69 |
+
print(f"Error fetching or processing {link}: {e}")
|
70 |
+
all_results.append({"text": None})
|
71 |
+
else:
|
72 |
+
all_results.append({"text": None})
|
73 |
+
start += len(result_block)
|
74 |
+
return all_results
|
75 |
|
76 |
# Speech Recognition Model Configuration
|
77 |
model_name = "neongeckocom/stt_en_citrinet_512_gamma_0_25"
|
|
|
84 |
|
85 |
# Mistral Model Configuration
|
86 |
client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
87 |
+
system_instructions1 = "<s>[SYSTEM] Answer as Real OpenGPT 4o, Made by 'KingNish', Keep conversation very short, clear, friendly and concise. The text provided is a request for a specific type of response from you, the virtual assistant. The request asks you to provide friendly responses. The expectation is that I will avoid introductions and start answering the query directly, elaborating on all aspects of the request. As an AI-powered assistant, your task is to generate responses that appear as if they are created by the user and not an AI-powered assistant. [USER]"
|
88 |
|
89 |
def resample(audio_fp32, sr):
|
90 |
return soxr.resample(audio_fp32, sr, sample_rate)
|
|
|
112 |
|
113 |
return text
|
114 |
|
115 |
+
def model(text, web_search):
|
116 |
+
if web_search is True:
|
117 |
+
"""Performs a web search, feeds the results to a language model, and returns the answer."""
|
118 |
+
web_results = search(text)
|
119 |
+
web2 = ' '.join([f"Text: {res['text']}\n\n" for res in web_results])
|
120 |
+
formatted_prompt = system_instructions1 + text + "[WEB]" + str(web2) + "[OpenGPT 4o]"
|
121 |
+
stream = client1.text_generation(formatted_prompt, max_new_tokens=512, stream=True, details=True, return_full_text=False)
|
122 |
+
return "".join([response.token.text for response in stream if response.token.text != "</s>"])
|
123 |
+
else:
|
124 |
+
formatted_prompt = system_instructions1 + text + "[OpenGPT 4o]"
|
125 |
+
stream = client1.text_generation(formatted_prompt, max_new_tokens=512, stream=True, details=True, return_full_text=False)
|
126 |
+
return "".join([response.token.text for response in stream if response.token.text != "</s>"])
|
127 |
|
128 |
+
async def respond(audio, web_search):
|
129 |
user = transcribe(audio)
|
130 |
+
reply = model(user, web_search)
|
131 |
communicate = edge_tts.Communicate(reply)
|
132 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
133 |
tmp_path = tmp_file.name
|