|
|
from flask import Flask, request, jsonify |
|
|
import requests |
|
|
import json |
|
|
import os |
|
|
import re |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
def filter_non_alphanumeric(text): |
|
|
alphanumeric_text = re.sub(r'[^a-zA-Z0-9\s]', '', text) |
|
|
return alphanumeric_text |
|
|
|
|
|
def send_to_corrade(filtered_response): |
|
|
CORRADE_URL = "http://216.168.124.120:8080" |
|
|
GROUP_UUID = "e269893f-a570-0087-930e-6ba2a0b77f9c" |
|
|
PASSWORD = "nucleus" |
|
|
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 process_input_text(input_text): |
|
|
url = "https://chat.tune.app/api/chat/completions" |
|
|
headers = { |
|
|
"Authorization": os.environ.get('api'), |
|
|
"Content-Type": "application/json" |
|
|
} |
|
|
data = { |
|
|
"temperature": 0.5, |
|
|
"messages": [ |
|
|
{"role": "system", "content": "You are TuneChat"}, |
|
|
{"role": "user", "content": input_text} |
|
|
], |
|
|
"model": "goliath-120b-16k-gptq", |
|
|
"stream": True, |
|
|
"max_tokens": 1000 |
|
|
} |
|
|
response = requests.post(url, headers=headers, json=data, stream=True) |
|
|
|
|
|
full_content = "" |
|
|
for line in response.iter_lines(decode_unicode=True): |
|
|
if line: |
|
|
if line == 'data: [DONE]': |
|
|
break |
|
|
line_content = json.loads(line[6:]) |
|
|
if 'choices' in line_content and len(line_content['choices']) > 0: |
|
|
delta_content = line_content['choices'][0].get('delta', {}).get('content', '') |
|
|
full_content += delta_content |
|
|
|
|
|
return filter_non_alphanumeric(full_content) |
|
|
|
|
|
@app.route('/chat', methods=['POST']) |
|
|
def chat(): |
|
|
data = request.json |
|
|
if not data or 'text' not in data: |
|
|
return jsonify({'error': 'Missing text'}), 400 |
|
|
input_text = data['text'] |
|
|
filtered_response = process_input_text(input_text) |
|
|
if send_to_corrade(filtered_response): |
|
|
return jsonify({'response': filtered_response}), 200 |
|
|
else: |
|
|
return jsonify({'error': 'Failed to send response to Corrade'}), 500 |
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run(port=5000, debug=True) |
|
|
|