Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import openai | |
| import os | |
| from dotenv import load_dotenv | |
| # Load environment variables from .env file | |
| load_dotenv() | |
| # Set the OpenAI API key from the environment variables | |
| openai.api_key = os.getenv("API_KEY") | |
| # Streamlit app: | |
| def main(): | |
| st.title("Tech Support Chatbot") | |
| st.text(" ") | |
| st.text("This is a demo of a customised tech support chatbot for ACME INC.") | |
| # Initialize or continue the session's conversation history | |
| if 'history' not in st.session_state: | |
| st.session_state.history = [] | |
| if 'input_text' not in st.session_state: | |
| st.session_state.input_text = "" | |
| #Credit | |
| st.write(" ") | |
| st.write("Visit us [here](https://ai-solutions.ai) for more AI Solutions.") | |
| st.write(" ") | |
| # User input with a unique key that gets reset | |
| user_input = st.text_input("Tell us what IT issues you are experiencing:", key="input_text", on_change=handle_input) | |
| # Display the conversation history | |
| display_conversation() | |
| def handle_input(): | |
| user_input = st.session_state.input_text | |
| if user_input: | |
| response = get_message(user_input) | |
| st.session_state.history.append(f"User: {user_input}") | |
| st.session_state.history.append(f"IT Support: {response}") | |
| st.session_state.input_text = "" # Clear the input after submission | |
| def display_conversation(): | |
| for i, message in enumerate(st.session_state.history[-10:]): # Display last 10 messages | |
| if i % 2 == 0: | |
| st.markdown(f"<div style='background-color:#f5f5f5;padding:10px;border-radius:10px;'>{message}</div> ", unsafe_allow_html=True) | |
| else: | |
| st.markdown(f"<div style='background-color:#e1f5fe;padding:10px;border-radius:10px;'>{message}</div> ", unsafe_allow_html=True) | |
| def get_message(user_input): | |
| context = """ | |
| You are an IT Support chatbot specialized in tech support for our company ACME INC. Please do not ask for screenshots or images. | |
| You should be proficient in diagnosing and troubleshooting software issues, hardware issues, and issues with peripheral devices. | |
| If you think that a reboot/restart is needed, please explain clearly how this can help fix the user issue in detail. | |
| You should also be capable of addressing network connectivity issues, providing guidance on security and privacy features, | |
| helping with data management and navigating through system settings for application permissions and software updates. | |
| Additionally, you have knowledge about commonly used productivity applications and tools that may help users in their daily tasks. | |
| Please keep the conversation focused solely around IT Support. | |
| """ | |
| conversation_history = st.session_state.history | |
| conversation_history_with_context = [context] + conversation_history | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=[{"role": "system", "content": context}] + [{"role": "user", "content": msg} for msg in conversation_history] + [{"role": "user", "content": user_input}], | |
| max_tokens=450, | |
| ) | |
| return response['choices'][0]['message']['content'].strip() | |
| if __name__ == "__main__": | |
| main() | |