diff --git a/miapia_own/aihandler.py b/miapia_own/aihandler.py index 9ec193a..f5033c7 100644 --- a/miapia_own/aihandler.py +++ b/miapia_own/aihandler.py @@ -1,4 +1,5 @@ - +from time import time +import urllib.parse import requests @@ -7,6 +8,12 @@ class AIHandler(object): pass def __call__(self, text): + """ + resp = requests.get(f"https://ai.travely24.com/generate?input_query={urllib.parse.quote_plus(text)}", + headers={"accept": "application/json"}, + stream=True) + """ + resp = requests.post("https://fast-pia.avemio.technology/chat-completion", json={ "session-id": "chatcmpl", @@ -28,9 +35,24 @@ class AIHandler(object): "content": text } ] - }) + }, + headers={"accept": "application/json"}, + stream=True) resp = resp.json() return { "text": resp[0]['text'], "emotion": resp[0]['emotion'] } + """ + for chunk in resp.iter_content(chunk_size=1024): + if chunk: + yield str(chunk) + """ + + +if __name__ == "__main__": + aihandler = AIHandler() + t1 = time() + for text in aihandler("Hello, how are you, what is your name?"): + print(time() - t1) + print(text) diff --git a/miapia_own/aihandler_new.py b/miapia_own/aihandler_new.py index 1d1ed34..9d856a4 100644 --- a/miapia_own/aihandler_new.py +++ b/miapia_own/aihandler_new.py @@ -4,32 +4,30 @@ openai.api_key = "sk-proj-PdTVVVvYzcd6vs2qcRxpT3BlbkFJtq78XfSrzwEK2fqyOVHE" import requests -class AIHandlerStream(object): +class AIHandlerNewStream(object): def __init__(self): - self.ai = openai.OpenAI(api_key="sk-proj-GaouEG2QuAcfAr1an2uBT3BlbkFJaIh0XVFXWrYQpJazlbeO") + pass def __call__(self, text): + url = f"https://ai.travely24.com/generate/?prompt={text}"; + response = requests.get( + url, + stream=True, + headers={"accept": "application/json"}, + ) out = "" - for chunk in self.ai.chat.completions.create( - model="gpt-3.5-turbo", - messages=[ - {"role": "system", "content": "You are PIA. You talk with short sentences. And help people."}, - {"role": "user", "content": text} - ], stream=True - ): - delta = chunk.choices[0].delta.content - if delta is None: - continue - out += delta - if len(out) > 0 and out[-1] in ['.', '!', ',', '?']: - yield out - out = "" + for delta in response.iter_content(chunk_size=1024): + if delta: + out += delta.decode("utf-8") + if len(out) > 0 and out.strip()[-1] in ['.', '!', ',', '?']: + yield out + out = "" if len(out) > 0: yield out if __name__ == "__main__": - aihandler = AIHandlerStream() + aihandler = AIHandlerNewStream() t1 = time() for text in aihandler("Hello, how are you, what is your name?"): print(time() - t1) diff --git a/miapia_own/main.py b/miapia_own/main.py index c7a6a0d..9abd06a 100644 --- a/miapia_own/main.py +++ b/miapia_own/main.py @@ -1,6 +1,12 @@ import json import sys import re +import spacy + +# Load the language model +nlp = spacy.load("en_core_web_lg") + +import sys from time import sleep, time import logging from collections import defaultdict @@ -9,6 +15,11 @@ from flask import redirect import argparse import base64 +import pandas as pd +import openai, numpy as np +openai.api_key = "sk-proj-GaouEG2QuAcfAr1an2uBT3BlbkFJaIh0XVFXWrYQpJazlbeO" +# from openai.embeddings_utils import get_embedding, cosine_similarity + from flask import send_file, Response, request, jsonify from flask_socketio import emit from piedemo.fields.ajax_group import AjaxChatField, AjaxGroup @@ -44,9 +55,14 @@ from tqdm import tqdm from aihandler import AIHandler from aihandler_stream import AIHandlerStream +from aihandler_new import AIHandlerNewStream from pieinfer import PieInfer, render_video, construct_video import torch from TTS.api import TTS + +sys.path.append("/home/ubuntu/repo/slavaBIM") +from pages.demo import DemoPage + logging.getLogger('socketio').setLevel(logging.ERROR) logging.getLogger('engineio').setLevel(logging.ERROR) @@ -133,6 +149,7 @@ class MainPage(Page): self.r = redis.Redis(host='localhost', port=6379, decode_responses=True) self.aihandler = AIHandler() self.aihandler_stream = AIHandlerStream() + self.aihandler_new_stream = AIHandlerNewStream() self.fields = Navigation(AjaxGroup("ChatGroup", VStack([ HStack([ @@ -263,8 +280,11 @@ web = Web({ "": "simple", "simple": page, "nice": page, + "secret_demo_of_c574d349": DemoPage(), + "secret_demo_of_c574dnew": DemoPage(load_new_model=True) }, use_socketio_support=True) - +web.add_assets_directory("IFC", + "/home/ubuntu/repo/slavaBIM/data/IFC") host = '0.0.0.0' port = 8011 @@ -518,6 +538,150 @@ def perform_surgery(sid, duration=5): sleep(max(0., duration - (t2 - t1))) +BODY_ANIMATION_DESCRIPTIONS = [ + "Get on your knees", + "Show butt", + "Show ass", + "Pull back", + "Doggy style", + + "Sit on ass", + "Spread legs", + "Show pussy", + "Missionary position", + "Let fuck", + + "Lie on side", + "Show legs", + "Rest", + "Relax", + "Sleep", + + "Stand up", + "Showcase yourself", + "Show yourself", + "Show your boobs", + "Touch your boobs", + + "Make a blowjob", + "On your knees", + "Sit on the dildo", + "Kiss my ass", + "Be on top" +] + + +resp = page.aihandler_stream.ai.embeddings.create(input=BODY_ANIMATION_DESCRIPTIONS, + model="text-embedding-3-small") +BODY_ANIMATION_EMBEDDINGS = np.array(list(map(lambda x: x.embedding, + resp.data))) + + +def to_back_view(sid): + emit("io_set_view", + [{"type": "back"}], + sid=sid) + + +def to_front_view(sid): + emit("io_set_view", + [{"type": "front"}], + sid=sid) + + +def to_up_to_down_view(sid): + emit("io_set_view", + [{"type": "up_to_down"}], + sid=sid) + + +@io.on("io_set_text_body") +def io_set_text_body(data): + data = json.loads(data) + data = data[0] + sid = None + print(data, file=sys.stderr) + if "text" not in data: + emit("io_error", {"message": "Text not found"}, + to=sid) + return + + text = data["text"] + user_id = data.get('user_id') + print(user_id) + emb = page.aihandler_stream.ai.embeddings.create(input=[text], + model="text-embedding-3-small") + + idx = int(np.argmax(np.sum(np.array([emb.data[0].embedding]) * BODY_ANIMATION_EMBEDDINGS, axis=-1))) + + animations_durations = { + 1: 13, + 2: 9.33, + 3: 10.5, + 4: 7.52, + 5: 10, + } + print("Text: ", text, "\n", + "Detected:", BODY_ANIMATION_DESCRIPTIONS[idx], "\n", + "Index: ", idx, "\n") + # to_body_view(sid) + + if 1 + idx // 5 == 1: + to_back_view(sid) + elif 1 + idx // 5 == 3: + to_up_to_down_view(sid) + else: + to_front_view(sid) + + emit("io_set_body_animation", + [{ + "index": 1 + idx // 5, + "timestamp": 0 + }], + to=sid) + """ + start_speaking(text, + user_id=user_id, + sid=sid) + """ + sleep(animations_durations[1 + idx // 5] // 2) + to_front_view(sid) + sleep(animations_durations[1 + idx // 5] // 2) + + emit("io_finish", {}, to=sid) + + +def start_speaking(text, user_id, sid): + TEXTS[user_id].append({ + "id": "user", + "text": text + }) + voice = SPEAKER.get(user_id, "voice1") + + if sid not in head_memories: + head_memories[sid] = get_head_memory() + head_memory = head_memories[sid] + # output_texts = [page.aihandler(text)['text']] + output_texts = page.aihandler_stream(text) + # output_texts = page.aihandler_new_stream(text) + bot_text = "" + for output_text in output_texts: + sign = [2 * (random.random() > 0.5) - 1 + for _ in range(8)] + head_memory = perform_on_text(output_text, sid, head_memory, + sign=sign, + voice=voice) + bot_text += " " + output_text + print("SURGERY STARTED!") + # perform_surgery(sid) + print("SURGERY ENDED!") + TEXTS[user_id].append({ + "id": "bot", + "text": bot_text + }) + + + @io.on("io_set_text") def io_set_text(data): data = json.loads(data) @@ -537,32 +701,9 @@ def io_set_text(data): return""" user_id = data.get('user_id') print(user_id) - TEXTS[user_id].append({ - "id": "user", - "text": text - }) - voice = SPEAKER.get(user_id, "voice1") - - if sid not in head_memories: - head_memories[sid] = get_head_memory() - head_memory = head_memories[sid] - # output_texts = [page.aihandler(text)['text']] - output_texts = page.aihandler_stream(text) - bot_text = "" - for output_text in output_texts: - sign = [2 * (random.random() > 0.5) - 1 - for _ in range(8)] - head_memory = perform_on_text(output_text, sid, head_memory, - sign=sign, - voice=voice) - bot_text += " " + output_text - print("SURGERY STARTED!") - # perform_surgery(sid) - print("SURGERY ENDED!") - TEXTS[user_id].append({ - "id": "bot", - "text": bot_text - }) + start_speaking(text, + user_id=user_id, + sid=sid) emit("io_finish", {}, to=sid) diff --git a/miapia_own/test_new_ai.py b/miapia_own/test_new_ai.py index e69de29..408b62a 100644 --- a/miapia_own/test_new_ai.py +++ b/miapia_own/test_new_ai.py @@ -0,0 +1,11 @@ +import requests +url = "https://ai.travely24.com/generate/?prompt=Hi, what is your name?"; +response = requests.get( + url, + stream=True, + headers={"accept": "application/json"}, +) + +for chunk in response.iter_content(chunk_size=1024): + if chunk: + print(str(chunk, encoding="utf-8"), end="")