import streamlit as st import requests import json # Function to call the API def call_chatgpt_api(message, dialog_messages): url = "https://api.safone.dev/chatgpt" headers = { "accept": "application/json", "Content-Type": "application/json" } payload = { "message": message, "version": 3, "chat_mode": "assistant", "dialog_messages": dialog_messages } response = requests.post(url, headers=headers, data=json.dumps(payload)) if response.status_code == 200: return response.json() else: return None # Initialize session state if not already done if 'dialog_messages' not in st.session_state: st.session_state.dialog_messages = [{"bot": "", "user": ""}] if 'responses' not in st.session_state: st.session_state.responses = [] # Streamlit App st.title('SlimshadowGPT') st.write("This is a simple interface to interact with the GPT-3.5 API.") # Placeholder for user input input_placeholder = st.empty() user_input = input_placeholder.text_input("Enter your message:") if st.button('Send'): if user_input: st.session_state.dialog_messages.append({"bot": "", "user": user_input}) with st.spinner('Generating response...'): response = call_chatgpt_api(user_input, st.session_state.dialog_messages) if response: assistant_message = response['choices'][0]['message']['content'] st.session_state.dialog_messages.append({"bot": assistant_message, "user": ""}) st.session_state.responses.append((user_input, assistant_message)) else: st.write("Failed to get a response from the API.") # Clear input box after sending input_placeholder.text_input("Enter your message:", value="", key="user_input_clear") # Display the conversation history st.write("### Chat History") for user_msg, bot_msg in st.session_state.responses: st.write(f"**You:** {user_msg}") st.write(f"**Assistant:** {bot_msg}") # Run the app using `streamlit run app.py` in the terminal