File size: 2,145 Bytes
18e60e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ceb530c
18e60e9
 
f14de9d
18e60e9
 
 
 
 
 
 
 
 
 
ceb530c
18e60e9
 
 
 
 
 
 
 
 
 
ceb530c
79de0b3
ceb530c
18e60e9
ceb530c
 
 
18e60e9
 
ceb530c
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import streamlit as st
import requests
import json
import os
import re
import emoji
from urllib.parse import unquote

# Define the functions as provided in the original script

def filter_text_and_preserve_emojis(text):
    pattern = re.compile(r'[a-zA-Z0-9\s.,?!\'"]|[\U0001F600-\U0001F64F\U0001F300-\U0001F5FF\U0001F680-\U0001F6FF\U0001F700-\U0001F77F\U0001F780-\U0001F7FF\U0001F800-\U0001F8FF\U0001F900-\U0001F9FF\U0001FA00-\U0001FA6F\U0001FA70-\U0001FAFF\U00002702-\U000027B0\U000024C2-\U0001F251]+')
    return ''.join(pattern.findall(text))

def chatbot_api(input_text):
    url = "https://chat.tune.app/api/chat/completions"
    decoded_text = unquote(input_text)
    headers = {
        "Authorization": os.environ.get('api'),  # Ensure this is set
        "Content-Type": "application/json"
    }
    data = {
        "temperature": 0.5,
        "messages": [
            {"role": "system", "content": "System setup text here."},
            {"role": "user", "content": decoded_text}
        ],
        "model": "goliath-120b-16k-gptq",
        "stream": False,
        "max_tokens": 350
    }
    response = requests.post(url, headers=headers, json=data)
    try:
        response_json = response.json()
        full_content = response_json['choices'][0]['message']['content']
        filtered_response = filter_text_and_preserve_emojis(full_content)
    except (KeyError, IndexError):
        filtered_response = "Error: The expected response format has changed or is missing."
    
    return {
        "actions": [
            {"type": "message", "text": filtered_response}
        ]
    }

# Streamlit UI

st.title("Chatbot Interface")

# Fetch the URL parameter
query_params = st.query_params
auto_submit_text = query_params.get("input_text", [None])[0]

# Check if there's an input from URL parameter and auto-submit
if auto_submit_text is not None:
    result = chatbot_api(auto_submit_text)
    if result:
        st.json(result)
else:
    # Manual input
    user_input = st.text_input("Enter your text here:")
    if st.button("Submit"):
        result = chatbot_api(user_input)
        if result:
            st.json(result)