Update app.py
Browse files
app.py
CHANGED
|
@@ -5,47 +5,62 @@ import os
|
|
| 5 |
import re
|
| 6 |
|
| 7 |
def filter_non_alphanumeric(text):
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def chatbot_api(input_text):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
stream = False
|
| 14 |
url = "https://chat.tune.app/api/chat/completions"
|
| 15 |
headers = {
|
| 16 |
-
"Authorization": os.environ.get('
|
| 17 |
"Content-Type": "application/json"
|
| 18 |
}
|
| 19 |
data = {
|
| 20 |
-
"temperature": 0.1,
|
| 21 |
"messages": [
|
| 22 |
{
|
| 23 |
"role": "system",
|
| 24 |
-
|
|
|
|
| 25 |
},
|
| 26 |
{
|
| 27 |
"role": "user",
|
| 28 |
-
"content": input_text
|
| 29 |
}
|
| 30 |
],
|
| 31 |
-
"model": "goliath-120b-16k-gptq",
|
| 32 |
"stream": stream,
|
| 33 |
"max_tokens": 256
|
| 34 |
}
|
| 35 |
response = requests.post(url, headers=headers, json=data)
|
|
|
|
| 36 |
if stream:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
l = line[6:]
|
| 40 |
-
if l != b'[DONE]':
|
| 41 |
-
filtered_response = filter_non_alphanumeric(json.loads(l))
|
| 42 |
-
print(filtered_response)
|
| 43 |
else:
|
| 44 |
-
chat_response = response.json()
|
| 45 |
-
|
| 46 |
chat_content = chat_response['choices'][0]['message']['content']
|
| 47 |
filtered_response = filter_non_alphanumeric(chat_content)
|
| 48 |
-
return filtered_response
|
| 49 |
|
| 50 |
iface = gr.Interface(fn=chatbot_api, inputs="text", outputs="text")
|
| 51 |
iface.launch()
|
|
|
|
| 5 |
import re
|
| 6 |
|
| 7 |
def filter_non_alphanumeric(text):
|
| 8 |
+
# Improved regex filtering based on configurable rules
|
| 9 |
+
filter_pattern = os.environ.get('FILTER_PATTERN', r'[^a-zA-Z0-9\s]')
|
| 10 |
+
return re.sub(filter_pattern, '', text)
|
| 11 |
+
|
| 12 |
+
def configure_chatbot(level, input_text):
|
| 13 |
+
# Function to configure chatbot behavior based on level or specific inputs
|
| 14 |
+
configuration = {
|
| 15 |
+
# Placeholder for configuration settings (models, temperatures, etc.)
|
| 16 |
+
# This could be loaded from a JSON file or environment variables
|
| 17 |
+
}
|
| 18 |
+
# Example of adjusting configuration based on level
|
| 19 |
+
if level == 'level1':
|
| 20 |
+
configuration.update({
|
| 21 |
+
# Level-specific configuration
|
| 22 |
+
})
|
| 23 |
+
# Additional levels or specific configurations can go here
|
| 24 |
+
return configuration
|
| 25 |
|
| 26 |
def chatbot_api(input_text):
|
| 27 |
+
level = "default" # Placeholder for determining chatbot level
|
| 28 |
+
# Function to configure behavior based on input or predetermined settings
|
| 29 |
+
chatbot_config = configure_chatbot(level, input_text)
|
| 30 |
+
|
| 31 |
stream = False
|
| 32 |
url = "https://chat.tune.app/api/chat/completions"
|
| 33 |
headers = {
|
| 34 |
+
"Authorization": os.environ.get('API_KEY'),
|
| 35 |
"Content-Type": "application/json"
|
| 36 |
}
|
| 37 |
data = {
|
| 38 |
+
"temperature": chatbot_config.get('temperature', 0.1),
|
| 39 |
"messages": [
|
| 40 |
{
|
| 41 |
"role": "system",
|
| 42 |
+
# Configuration can adjust the initial message or role behavior
|
| 43 |
+
"content": chatbot_config.get('initial_message', "")
|
| 44 |
},
|
| 45 |
{
|
| 46 |
"role": "user",
|
| 47 |
+
"content": input_text
|
| 48 |
}
|
| 49 |
],
|
| 50 |
+
"model": chatbot_config.get('model', "goliath-120b-16k-gptq"),
|
| 51 |
"stream": stream,
|
| 52 |
"max_tokens": 256
|
| 53 |
}
|
| 54 |
response = requests.post(url, headers=headers, json=data)
|
| 55 |
+
# Handling response, including streaming if applicable
|
| 56 |
if stream:
|
| 57 |
+
# Stream response handling
|
| 58 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
else:
|
| 60 |
+
chat_response = response.json()
|
|
|
|
| 61 |
chat_content = chat_response['choices'][0]['message']['content']
|
| 62 |
filtered_response = filter_non_alphanumeric(chat_content)
|
| 63 |
+
return filtered_response
|
| 64 |
|
| 65 |
iface = gr.Interface(fn=chatbot_api, inputs="text", outputs="text")
|
| 66 |
iface.launch()
|