sgram66 commited on
Commit
4409665
·
verified ·
1 Parent(s): f4366c6

Application file

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from streamlit_extras.chat_elements import message
8
+
9
+ # Set OpenAI API Key
10
+ os.environ["OPENAI_API_KEY"] = ""
11
+
12
+ # Initialize the chatbot
13
+ @st.cache_resource
14
+ def init_chatbot():
15
+ memory = ConversationBufferMemory()
16
+ chatbot = ConversationChain(
17
+ llm=ChatOpenAI(model="gpt-4o-mini"),
18
+ memory=memory,
19
+ verbose=False
20
+ )
21
+ return chatbot
22
+
23
+ chatbot = init_chatbot()
24
+
25
+ # Custom Styling
26
+ st.markdown("""
27
+ <style>
28
+ body {
29
+ background-color: #f5f5f5;
30
+ }
31
+ .chat-container {
32
+ background-color: #ffffff;
33
+ padding: 20px;
34
+ border-radius: 10px;
35
+ box-shadow: 0px 4px 6px rgba(0,0,0,0.1);
36
+ }
37
+ </style>
38
+ """, unsafe_allow_html=True)
39
+
40
+ # Sidebar - Settings
41
+ st.sidebar.title("⚙️ Settings")
42
+ model_choice = st.sidebar.radio("Select Model", ("gpt-4o-mini", "gpt-4", "gpt-3.5-turbo"))
43
+
44
+ # Update model based on user selection
45
+ if model_choice:
46
+ chatbot.llm = ChatOpenAI(model=model_choice)
47
+
48
+ # Title and Description
49
+ st.title("💬 LangChain AI Chatbot")
50
+ st.write("### Hi, I'm a chatbot built with Langchain powered by GPT. How can I assist you today?")
51
+
52
+ # Chat history
53
+ if "chat_history" not in st.session_state:
54
+ st.session_state.chat_history = []
55
+
56
+ # User Input
57
+ user_input = st.text_input("You:", placeholder="Ask me anything...")
58
+
59
+ # Process input
60
+ if user_input:
61
+ with st.spinner("Thinking..."):
62
+ response = chatbot.run(user_input)
63
+ st.session_state.chat_history.append(("user", user_input))
64
+ st.session_state.chat_history.append(("bot", response))
65
+
66
+ # Display chat history
67
+ st.write("### 🗨️ Conversation")
68
+ chat_container = st.container()
69
+
70
+ with chat_container:
71
+ for role, text in st.session_state.chat_history:
72
+ if role == "user":
73
+ message(text, is_user=True, avatar_style="thumbs")
74
+ else:
75
+ message(text, is_user=False, avatar_style="bottts")
76
+
77
+ # Add some spacing
78
+ add_vertical_space(2)
79
+
80
+ # Collapsible Chat History
81
+ with st.expander("📜 Chat History"):
82
+ for role, text in st.session_state.chat_history:
83
+ st.write(f"**{role.capitalize()}**: {text}")
84
+
85
+ # Footer
86
+ st.markdown("---")
87
+ st.markdown("Developed with ❤️ using Streamlit & LangChain")