Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import os
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
def filter_non_alphanumeric(text):
|
| 8 |
+
# Use regular expressions to filter out non-alphanumeric characters
|
| 9 |
+
alphanumeric_text = re.sub(r'[^a-zA-Z0-9\s]', '', text)
|
| 10 |
+
return alphanumeric_text
|
| 11 |
+
|
| 12 |
+
def send_to_corrade(filtered_response):
|
| 13 |
+
CORRADE_URL = "http://216.168.124.120:8080"
|
| 14 |
+
GROUP_UUID = "e269893f-a570-0087-930e-6ba2a0b77f9c"
|
| 15 |
+
PASSWORD = "nucleus"
|
| 16 |
+
data = {
|
| 17 |
+
"command": "tell",
|
| 18 |
+
"group": GROUP_UUID,
|
| 19 |
+
"password": PASSWORD,
|
| 20 |
+
"message": filtered_response,
|
| 21 |
+
"entity": "local",
|
| 22 |
+
"type": "Normal"
|
| 23 |
+
}
|
| 24 |
+
response = requests.post(CORRADE_URL, data=data)
|
| 25 |
+
return response.status_code == 200
|
| 26 |
+
|
| 27 |
+
def chatbot_api(input_text):
|
| 28 |
+
url = "https://chat.tune.app/api/chat/completions"
|
| 29 |
+
headers = {
|
| 30 |
+
"Authorization": os.environ.get('api'), # Make sure to set this environment variable
|
| 31 |
+
"Content-Type": "application/json"
|
| 32 |
+
}
|
| 33 |
+
data = {
|
| 34 |
+
"temperature": 0.5,
|
| 35 |
+
"messages": [
|
| 36 |
+
{"role": "system", "content": "You are TuneChat"},
|
| 37 |
+
{"role": "user", "content": input_text}
|
| 38 |
+
],
|
| 39 |
+
"model": "goliath-120b-16k-gptq",
|
| 40 |
+
"stream": True,
|
| 41 |
+
"max_tokens": 1000
|
| 42 |
+
}
|
| 43 |
+
response = requests.post(url, headers=headers, json=data, stream=True)
|
| 44 |
+
|
| 45 |
+
full_content = ""
|
| 46 |
+
for line in response.iter_lines(decode_unicode=True):
|
| 47 |
+
if line:
|
| 48 |
+
if line == 'data: [DONE]':
|
| 49 |
+
break # Exit the loop when [DONE] marker is reached
|
| 50 |
+
line_content = json.loads(line[6:]) # Skip the "data: " prefix
|
| 51 |
+
if 'choices' in line_content and len(line_content['choices']) > 0:
|
| 52 |
+
delta_content = line_content['choices'][0].get('delta', {}).get('content', '')
|
| 53 |
+
full_content += delta_content
|
| 54 |
+
|
| 55 |
+
filtered_response = filter_non_alphanumeric(full_content)
|
| 56 |
+
send_to_corrade(filtered_response)
|
| 57 |
+
return filtered_response
|
| 58 |
+
|
| 59 |
+
# Example usage with Gradio
|
| 60 |
+
import gradio as gr
|
| 61 |
+
|
| 62 |
+
iface = gr.Interface(fn=chatbot_api, inputs="text", outputs="text")
|
| 63 |
+
iface.launch()
|