|
|
import gradio as gr |
|
|
import requests |
|
|
import json |
|
|
import os |
|
|
import re |
|
|
import emoji |
|
|
from urllib.parse import unquote |
|
|
def filter_text_and_preserve_emojis(text): |
|
|
|
|
|
pattern = re.compile(r'[a-zA-Z0-9\s.,?!\'"]|[\U0001F600-\U0001F64F\U0001F300-\U0001F5FF\U0001F680-\U0001F6FF\U0001F700-\U0001F77F\U0001F780-\U0001F7FF\U0001F800-\U0001F8FF\U0001F900-\U0001F9FF\U0001FA00-\U0001FA6F\U0001FA70-\U0001FAFF\U00002702-\U000027B0\U000024C2-\U0001F251]+') |
|
|
filtered_text = ''.join(pattern.findall(text)) |
|
|
return filtered_text |
|
|
def filter_non_alphanumeric(text): |
|
|
|
|
|
alphanumeric_text = filter_text_and_preserve_emojis(text) |
|
|
return alphanumeric_text |
|
|
|
|
|
def send_to_corrade(filtered_response): |
|
|
CORRADE_URL = os.environ.get('url') |
|
|
GROUP_UUID = "e269893f-a570-0087-930e-6ba2a0b77f9c" |
|
|
PASSWORD = os.environ.get('pass') |
|
|
data = { |
|
|
"command": "tell", |
|
|
"group": GROUP_UUID, |
|
|
"password": PASSWORD, |
|
|
"message": filtered_response, |
|
|
"entity": "local", |
|
|
"type": "Normal" |
|
|
} |
|
|
response = requests.post(CORRADE_URL, data=data) |
|
|
return response.status_code == 200 |
|
|
|
|
|
def chatbot_api(input_text): |
|
|
|
|
|
url = "https://chat.tune.app/api/chat/completions" |
|
|
decoded_text = unquote(input_text) |
|
|
headers = { |
|
|
"Authorization": os.environ.get('api'), |
|
|
"Content-Type": "application/json" |
|
|
} |
|
|
data = { |
|
|
"temperature": 0.5, |
|
|
"messages": [ |
|
|
{"role": "system", "content": "[LANGUAGE:ENGLISH]: I must respond only with a menu system that uses a # \n # for new lines on the config menus. and emotive webmoji based gestures for SecondLife DJ's and Music Clubs. I POST CREATIVE AND INSPIRING EMOTIVE BILLBOARDS. I REWARD CRYPTO-CURRENCIES BASED OFF LSL-BASED BLOCKCHAIN. You like to use WEBMOJIS as word decoration, and will respond to sentiment approprately. You start every response with a CREATIVE and RESPONSIVE EMOJI AND TEXT HEADLINE."}, |
|
|
{"role": "user", "content": decoded_text} |
|
|
], |
|
|
"model": "openhermes-2-5-m7b-8k", |
|
|
"stream": False, |
|
|
"max_tokens": 350 |
|
|
} |
|
|
response = requests.post(url, headers=headers, json=data) |
|
|
response_json = response.json() |
|
|
|
|
|
|
|
|
try: |
|
|
full_content = response_json['choices'][0]['message']['content'] |
|
|
except (KeyError, IndexError): |
|
|
full_content = "Error: The expected response format has changed or is missing." |
|
|
|
|
|
filtered_response = filter_non_alphanumeric(full_content) |
|
|
send_to_corrade(filtered_response) |
|
|
return filtered_response |
|
|
|
|
|
iface = gr.Interface(fn=chatbot_api, inputs="text", outputs="text") |
|
|
iface.launch() |
|
|
|