jarif commited on
Commit
96cf2a7
·
verified ·
1 Parent(s): a11ebca

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from dotenv import load_dotenv
4
+ from langchain_groq import ChatGroq
5
+ from langchain_core.messages import HumanMessage, AIMessage
6
+ from langchain_community.chat_message_histories import ChatMessageHistory
7
+ from langchain_core.chat_history import BaseChatMessageHistory
8
+ from langchain_core.runnables.history import RunnableWithMessageHistory
9
+
10
+ load_dotenv()
11
+ groq_api_key = os.getenv("GROQ_API_KEY")
12
+ model = ChatGroq(model="Gemma2-9b-It", groq_api_key=groq_api_key)
13
+
14
+ store = {}
15
+
16
+ def get_session_history(session_id: str) -> BaseChatMessageHistory:
17
+ if session_id not in store:
18
+ store[session_id] = ChatMessageHistory()
19
+ return store[session_id]
20
+
21
+ with_message_history = RunnableWithMessageHistory(model, get_session_history)
22
+
23
+ st.set_page_config(page_title="AI Chatbot", page_icon="🤖", layout="centered")
24
+ st.title("🤖Chat with Urmi: Your Personal AI Companion")
25
+
26
+ st.markdown("""
27
+ <style>
28
+ .stTextInput>div>div>input {
29
+ border-radius: 15px;
30
+ padding: 10px;
31
+ font-size: 1rem;
32
+ }
33
+ .stTextInput>div>div>input:focus {
34
+ border-color: #4caf50;
35
+ }
36
+ .stButton>button {
37
+ background-color: #4CAF50;
38
+ color: white;
39
+ border-radius: 10px;
40
+ padding: 10px 20px;
41
+ font-size: 1rem;
42
+ }
43
+ .chat-bubble-user {
44
+ background-color: #f1f1f1;
45
+ border-radius: 15px;
46
+ padding: 10px;
47
+ margin-bottom: 10px;
48
+ }
49
+ .chat-bubble-ai {
50
+ background-color: #007bff;
51
+ color: white;
52
+ border-radius: 15px;
53
+ padding: 10px;
54
+ margin-bottom: 10px;
55
+ }
56
+ .chat-container {
57
+ max-height: 500px;
58
+ overflow-y: scroll;
59
+ margin-bottom: 20px;
60
+ padding: 20px;
61
+ background-color: #fafafa;
62
+ border-radius: 10px;
63
+ }
64
+ </style>
65
+ """, unsafe_allow_html=True)
66
+
67
+ if "messages" not in st.session_state:
68
+ st.session_state.messages = []
69
+
70
+ user_input = st.text_input("Type your message here...", "")
71
+
72
+ if user_input:
73
+ st.session_state.messages.append(HumanMessage(content=user_input))
74
+
75
+ config = {"configurable": {"session_id": "chat1"}}
76
+
77
+ response = with_message_history.invoke(
78
+ [HumanMessage(content=user_input)],
79
+ config=config
80
+ )
81
+
82
+ st.session_state.messages.append(AIMessage(content=response.content))
83
+
84
+ st.markdown('<div class="chat-container">', unsafe_allow_html=True)
85
+ for message in st.session_state.messages:
86
+ if isinstance(message, HumanMessage):
87
+ st.markdown(f'<div class="chat-bubble-user">{message.content}</div>', unsafe_allow_html=True)
88
+ elif isinstance(message, AIMessage):
89
+ st.markdown(f'<div class="chat-bubble-ai">{message.content}</div>', unsafe_allow_html=True)
90
+ st.markdown('</div>', unsafe_allow_html=True)