Update app.py
Browse files
app.py
CHANGED
|
@@ -9,28 +9,10 @@ def filter_non_alphanumeric(text):
|
|
| 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 |
-
import os
|
| 28 |
-
import requests
|
| 29 |
-
|
| 30 |
def chatbot_api(input_text):
|
| 31 |
url = "https://chat.tune.app/api/chat/completions"
|
| 32 |
headers = {
|
| 33 |
-
"Authorization": os.environ.get('api'), #
|
| 34 |
"Content-Type": "application/json"
|
| 35 |
}
|
| 36 |
data = {
|
|
@@ -45,13 +27,21 @@ def chatbot_api(input_text):
|
|
| 45 |
}
|
| 46 |
response = requests.post(url, headers=headers, json=data, stream=True)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
response.encoding = 'utf-8'
|
| 50 |
-
|
| 51 |
for line in response.iter_lines(decode_unicode=True):
|
| 52 |
if line:
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
iface = gr.Interface(fn=chatbot_api, inputs="text", outputs="text")
|
| 57 |
iface.launch()
|
|
|
|
| 9 |
alphanumeric_text = re.sub(r'[^a-zA-Z0-9\s]', '', text)
|
| 10 |
return alphanumeric_text
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def chatbot_api(input_text):
|
| 13 |
url = "https://chat.tune.app/api/chat/completions"
|
| 14 |
headers = {
|
| 15 |
+
"Authorization": os.environ.get('api'), # Make sure to set this environment variable
|
| 16 |
"Content-Type": "application/json"
|
| 17 |
}
|
| 18 |
data = {
|
|
|
|
| 27 |
}
|
| 28 |
response = requests.post(url, headers=headers, json=data, stream=True)
|
| 29 |
|
| 30 |
+
full_content = ""
|
|
|
|
|
|
|
| 31 |
for line in response.iter_lines(decode_unicode=True):
|
| 32 |
if line:
|
| 33 |
+
if line == 'data: [DONE]':
|
| 34 |
+
break # Exit the loop when [DONE] marker is reached
|
| 35 |
+
line_content = json.loads(line[6:]) # Skip the "data: " prefix
|
| 36 |
+
if 'choices' in line_content and len(line_content['choices']) > 0:
|
| 37 |
+
delta_content = line_content['choices'][0].get('delta', {}).get('content', '')
|
| 38 |
+
full_content += delta_content
|
| 39 |
|
| 40 |
+
filtered_response = filter_non_alphanumeric(full_content)
|
| 41 |
+
return filtered_response
|
| 42 |
+
|
| 43 |
+
# Example usage with Gradio
|
| 44 |
+
import gradio as gr
|
| 45 |
|
| 46 |
iface = gr.Interface(fn=chatbot_api, inputs="text", outputs="text")
|
| 47 |
iface.launch()
|