Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
# Streamlit page configuration
|
4 |
+
st.title("Chatlytic")
|
5 |
+
|
6 |
+
# Initialize session state for model and messages if not already present
|
7 |
+
if "openai_model" not in st.session_state:
|
8 |
+
st.session_state["openai_model"] = "mixtral-8x7b"
|
9 |
+
|
10 |
+
if "messages" not in st.session_state:
|
11 |
+
st.session_state.messages = []
|
12 |
+
|
13 |
+
# Function to clear the chat
|
14 |
+
def clear_chat():
|
15 |
+
st.session_state.messages = []
|
16 |
+
|
17 |
+
# Button to clear the chat
|
18 |
+
if st.button('Clear Chat'):
|
19 |
+
clear_chat()
|
20 |
+
|
21 |
+
# Display previous messages
|
22 |
+
for message in st.session_state.messages:
|
23 |
+
with st.chat_message(message["role"]):
|
24 |
+
st.markdown(message["content"])
|
25 |
+
|
26 |
+
# Input for new message
|
27 |
+
if prompt := st.chat_input("What is up?"):
|
28 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
29 |
+
with st.chat_message("user"):
|
30 |
+
st.markdown(prompt)
|
31 |
+
|
32 |
+
# Define the API endpoint
|
33 |
+
api_endpoint = "https://ka1kuk-llm-api.hf.space/api/v1/chat/completions"
|
34 |
+
|
35 |
+
# Prepare the data for the POST request
|
36 |
+
data = {
|
37 |
+
"model": st.session_state["openai_model"],
|
38 |
+
"messages": st.session_state.messages,
|
39 |
+
"temperature": 0.5,
|
40 |
+
"top_p": 0.95,
|
41 |
+
"max_tokens": -1,
|
42 |
+
"use_cache": False,
|
43 |
+
"stream": False
|
44 |
+
}
|
45 |
+
|
46 |
+
# Send the POST request to the custom API
|
47 |
+
response = requests.post(api_endpoint, json=data)
|
48 |
+
|
49 |
+
# Check if the request was successful
|
50 |
+
if response.status_code == 200:
|
51 |
+
# Get the response content
|
52 |
+
response_data = response.json()
|
53 |
+
# Append the assistant's response to the messages
|
54 |
+
st.session_state.messages.append({"role": "assistant", "content": response_data["choices"][0]["message"]["content"]})
|
55 |
+
# Display the assistant's response
|
56 |
+
with st.chat_message("assistant"):
|
57 |
+
st.markdown(response_data["choices"][0]["message"]["content"])
|
58 |
+
else:
|
59 |
+
# Display an error message if the request failed
|
60 |
+
st.error("Failed to get a response from the custom API.")
|