Update app.py
Browse files
app.py
CHANGED
|
@@ -10,9 +10,9 @@ def filter_non_alphanumeric(text):
|
|
| 10 |
return alphanumeric_text
|
| 11 |
|
| 12 |
def send_to_corrade(filtered_response):
|
| 13 |
-
CORRADE_URL = os.environ.get('url')
|
| 14 |
GROUP_UUID = "e269893f-a570-0087-930e-6ba2a0b77f9c"
|
| 15 |
-
PASSWORD = os.environ.get('pass')
|
| 16 |
data = {
|
| 17 |
"command": "tell",
|
| 18 |
"group": GROUP_UUID,
|
|
@@ -23,7 +23,7 @@ def send_to_corrade(filtered_response):
|
|
| 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 = {
|
|
@@ -31,33 +31,30 @@ def chatbot_api(input_text):
|
|
| 31 |
"Content-Type": "application/json"
|
| 32 |
}
|
| 33 |
data = {
|
| 34 |
-
"temperature": 0.
|
| 35 |
"messages": [
|
| 36 |
{"role": "system", "content": "You are TuneChat"},
|
| 37 |
{"role": "user", "content": input_text}
|
| 38 |
],
|
| 39 |
"model": "goliath-120b-16k-gptq",
|
| 40 |
-
"stream":
|
| 41 |
-
"max_tokens":
|
| 42 |
}
|
| 43 |
-
response = requests.post(url, headers=headers, json=data
|
| 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 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
| 62 |
iface = gr.Interface(fn=chatbot_api, inputs="text", outputs="text")
|
| 63 |
iface.launch()
|
|
|
|
| 10 |
return alphanumeric_text
|
| 11 |
|
| 12 |
def send_to_corrade(filtered_response):
|
| 13 |
+
CORRADE_URL = os.environ.get('url') # Make sure to set this environment variable
|
| 14 |
GROUP_UUID = "e269893f-a570-0087-930e-6ba2a0b77f9c"
|
| 15 |
+
PASSWORD = os.environ.get('pass') # Make sure to set this environment variable
|
| 16 |
data = {
|
| 17 |
"command": "tell",
|
| 18 |
"group": GROUP_UUID,
|
|
|
|
| 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 = {
|
|
|
|
| 31 |
"Content-Type": "application/json"
|
| 32 |
}
|
| 33 |
data = {
|
| 34 |
+
"temperature": 0.2,
|
| 35 |
"messages": [
|
| 36 |
{"role": "system", "content": "You are TuneChat"},
|
| 37 |
{"role": "user", "content": input_text}
|
| 38 |
],
|
| 39 |
"model": "goliath-120b-16k-gptq",
|
| 40 |
+
"stream": False, # Adjusted to False to receive a non-streamed response
|
| 41 |
+
"max_tokens": 250
|
| 42 |
}
|
| 43 |
+
response = requests.post(url, headers=headers, json=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Directly parse the JSON from the response
|
| 46 |
+
response_json = response.json()
|
| 47 |
+
if 'choices' in response_json and len(response_json['choices']) > 0:
|
| 48 |
+
# Assuming the structure contains a list of choices with a text content
|
| 49 |
+
full_content = response_json['choices'][0]['text']
|
| 50 |
|
| 51 |
+
# Filter and send the response
|
| 52 |
+
filtered_response = filter_non_alphanumeric(full_content)
|
| 53 |
+
send_to_corrade(filtered_response)
|
| 54 |
+
return filtered_response
|
| 55 |
+
else:
|
| 56 |
+
return "No response received from the API."
|
| 57 |
|
| 58 |
+
# Example usage with Gradio
|
| 59 |
iface = gr.Interface(fn=chatbot_api, inputs="text", outputs="text")
|
| 60 |
iface.launch()
|