Namrata27blr commited on
Commit
7f79755
·
verified ·
1 Parent(s): c2c2608

create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from langchain.chains import ConversationChain
4
+ from langchain_openai import ChatOpenAI
5
+ from langchain.memory import ConversationBufferMemory
6
+ from streamlit_extras.add_vertical_space import add_vertical_space
7
+
8
+ # Set up OpenAI API Key securely
9
+ if "HPToken" in st.secrets:
10
+ os.environ["OPENAI_API_KEY"] = st.secrets["HPToken"]
11
+ else:
12
+ st.error("🔑 OpenAI API Key is missing! Please add it to Streamlit secrets.")
13
+ st.stop()
14
+
15
+ # Initialize chatbot with memory
16
+ def init_chatbot(model="gpt-4o-mini"):
17
+ try:
18
+ memory = ConversationBufferMemory()
19
+ chatbot = ConversationChain(llm=ChatOpenAI(model=model), memory=memory, verbose=False)
20
+ return chatbot
21
+ except Exception as e:
22
+ st.error(f"⚠️ Error initializing chatbot: {e}")
23
+ return None
24
+
25
+ if "chatbot" not in st.session_state:
26
+ st.session_state.chatbot = init_chatbot()
27
+
28
+ # Custom Styling
29
+ st.markdown("""
30
+ <style>
31
+ body {
32
+ background-color: #f5f5f5;
33
+ }
34
+ .stChatMessage {
35
+ padding: 10px;
36
+ border-radius: 10px;
37
+ margin: 5px 0;
38
+ }
39
+ .user-message {
40
+ background-color: #dcf8c6;
41
+ text-align: right;
42
+ }
43
+ .bot-message {
44
+ background-color: #ffffff;
45
+ }
46
+ </style>
47
+ """, unsafe_allow_html=True)
48
+
49
+ # Sidebar - Model Selection
50
+ st.sidebar.title("⚙️ Settings")
51
+ model_choice = st.sidebar.radio("Select Model", ("gpt-4o-mini", "gpt-4", "gpt-3.5-turbo"))
52
+
53
+ # Update chatbot model if changed
54
+ if model_choice != st.session_state.chatbot.llm.model_name:
55
+ st.session_state.chatbot = init_chatbot(model_choice)
56
+
57
+ # Title and Description
58
+ st.title("💬 LangChain AI Chatbot")
59
+ st.write("### Hi, I'm a chatbot powered by GPT. How can I assist you today?")
60
+
61
+ # Chat history storage
62
+ if "chat_history" not in st.session_state:
63
+ st.session_state.chat_history = []
64
+
65
+ # User Input via Chat Input (better UX)
66
+ user_input = st.chat_input("Type your message here...")
67
+
68
+ # Process input
69
+ if user_input:
70
+ with st.spinner("Thinking..."):
71
+ try:
72
+ response = st.session_state.chatbot.run(user_input)
73
+ if response:
74
+ st.session_state.chat_history.append(("user", user_input))
75
+ st.session_state.chat_history.append(("bot", response))
76
+ except Exception as e:
77
+ st.error(f"⚠️ Error generating response: {e}")
78
+
79
+ # Display chat history
80
+ st.write("### 🗨️ Conversation")
81
+ for role, text in st.session_state.chat_history:
82
+ with st.chat_message(role):
83
+ st.markdown(f"**{role.capitalize()}**: {text}")
84
+
85
+ # Collapsible Chat History
86
+ with st.expander("📜 View Full Chat History"):
87
+ for role, text in st.session_state.chat_history:
88
+ st.write(f"**{role.capitalize()}**: {text}")
89
+
90
+ # Add spacing
91
+ add_vertical_space(2)
92
+
93
+ # Footer
94
+ st.markdown("---")
95
+ st.markdown("Developed with ❤️ using Streamlit & LangChain")