Mpavan45 commited on
Commit
cbca970
Β·
verified Β·
1 Parent(s): 8959c06

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -39
app.py CHANGED
@@ -1,32 +1,13 @@
1
- import streamlit as st
2
  import sqlite3
3
  import uuid
 
4
  from langchain_google_genai import GoogleGenerativeAI
5
  from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
6
  from langchain_core.output_parsers import StrOutputParser
7
  from langchain_community.chat_message_histories import SQLChatMessageHistory
8
  from langchain_core.runnables.history import RunnableWithMessageHistory
9
- import time
10
-
11
- # Set page config
12
- st.set_page_config(page_title="Gemini Code Reviewer", layout="wide")
13
-
14
- # Animated text function
15
- def animated_text(text, speed=0.05):
16
- placeholder = st.empty()
17
- displayed_text = ""
18
- for letter in text:
19
- displayed_text += letter
20
- placeholder.markdown(f"""
21
- <h1 style="text-align:center; color: #00D1FF;">{displayed_text} πŸš€</h1>
22
- """, unsafe_allow_html=True) # Corrected f-string formatting
23
- time.sleep(speed)
24
-
25
- # Display animated text
26
- animated_text("Conversational AI Data Science Tutor", speed=0.1)
27
 
28
- # Streamlit Ui
29
- st.write("Ask me anything about Data Science!")
30
  # Load API key
31
  GOOGLE_API_KEY = st.secrets.get("GOOGLE_API_KEY")
32
 
@@ -67,12 +48,49 @@ def chat_history(session_id):
67
  if "session_id" not in st.session_state:
68
  st.session_state.session_id = str(uuid.uuid4())
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  # "New Chat" button
71
  if st.button("πŸ†• New Chat"):
72
  st.session_state.session_id = str(uuid.uuid4()) # Generate new session ID
73
  st.session_state.messages = [] # Clear chat history
74
  st.rerun() # Refresh the app
75
 
 
76
  session_id = st.session_state.session_id
77
  chat_history_instance = chat_history(session_id)
78
 
@@ -102,46 +120,42 @@ chat = RunnableWithMessageHistory(
102
  history_messages_key="history"
103
  )
104
 
105
-
106
- # πŸ”Ή Chat history container
107
  chat_container = st.container()
108
 
109
-
110
- # Load chat history and keep it sticky
111
  if "messages" not in st.session_state:
112
  st.session_state.messages = load_chat_history(session_id)
113
 
114
-
115
- # Display chat messages in order
116
  with chat_container:
117
  for role, content in st.session_state.messages:
118
  with st.chat_message(role):
119
  st.markdown(content)
120
- # Display chat history
121
 
 
 
122
 
123
- with input_container:
124
- # User input at the bottom
125
- user_input = st.text_input("Type your message here:", key="user_message")
126
-
127
  if user_input:
128
  # Save user message
129
  save_message(session_id, "user", user_input)
130
  st.session_state.messages.append(("user", user_input))
131
-
132
  # Invoke AI model
133
  config = {'configurable': {'session_id': session_id}}
134
  response = chat.invoke({'prompt': user_input}, config)
135
-
136
  # Save AI response
137
  save_message(session_id, "assistant", response)
138
  st.session_state.messages.append(("assistant", response))
139
-
140
  # Display AI response
141
- with st.chat_message("assistant"):
142
- st.markdown(response)
 
143
 
144
- # βœ… Clear input field
145
  st.session_state.pop("user_message")
146
- st.session_state["user_message"] = ""
147
  st.rerun() # Refresh the app
 
 
1
+ import streamlit as st
2
  import sqlite3
3
  import uuid
4
+ import time
5
  from langchain_google_genai import GoogleGenerativeAI
6
  from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
7
  from langchain_core.output_parsers import StrOutputParser
8
  from langchain_community.chat_message_histories import SQLChatMessageHistory
9
  from langchain_core.runnables.history import RunnableWithMessageHistory
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
 
 
11
  # Load API key
12
  GOOGLE_API_KEY = st.secrets.get("GOOGLE_API_KEY")
13
 
 
48
  if "session_id" not in st.session_state:
49
  st.session_state.session_id = str(uuid.uuid4())
50
 
51
+ # Custom CSS for UI enhancements
52
+ st.markdown("""
53
+ <style>
54
+ /* Style for the title animation */
55
+ .title-text {
56
+ text-align: center;
57
+ font-size: 30px;
58
+ font-weight: bold;
59
+ color: #FF4500;
60
+ margin-bottom: 20px;
61
+ }
62
+
63
+ /* Keep input field fixed at the bottom */
64
+ .stTextInput {
65
+ position: fixed;
66
+ bottom: 10px;
67
+ width: 80%;
68
+ left: 10%;
69
+ z-index: 999;
70
+ }
71
+ </style>
72
+ """, unsafe_allow_html=True)
73
+
74
+ # πŸ”Ή **Animated Title Function**
75
+ def animated_text(text, speed=0.05):
76
+ placeholder = st.empty()
77
+ displayed_text = ""
78
+
79
+ for letter in text:
80
+ displayed_text += letter
81
+ placeholder.markdown(f"<h1 class='title-text'>{displayed_text} πŸš€</h1>", unsafe_allow_html=True)
82
+ time.sleep(speed)
83
+
84
+ # πŸ”Ή **Display Animated Welcome Message**
85
+ animated_text("Welcome to Gemini Data Science Tutor!")
86
+
87
  # "New Chat" button
88
  if st.button("πŸ†• New Chat"):
89
  st.session_state.session_id = str(uuid.uuid4()) # Generate new session ID
90
  st.session_state.messages = [] # Clear chat history
91
  st.rerun() # Refresh the app
92
 
93
+ # Get session ID
94
  session_id = st.session_state.session_id
95
  chat_history_instance = chat_history(session_id)
96
 
 
120
  history_messages_key="history"
121
  )
122
 
123
+ # πŸ”Ή **Chat History Container**
 
124
  chat_container = st.container()
125
 
126
+ # Load chat history and display it
 
127
  if "messages" not in st.session_state:
128
  st.session_state.messages = load_chat_history(session_id)
129
 
 
 
130
  with chat_container:
131
  for role, content in st.session_state.messages:
132
  with st.chat_message(role):
133
  st.markdown(content)
 
134
 
135
+ # πŸ”Ή **Fixed Bottom User Input**
136
+ user_input = st.text_input("Type your message here:", key="user_message")
137
 
138
+ # If user submits a message
 
 
 
139
  if user_input:
140
  # Save user message
141
  save_message(session_id, "user", user_input)
142
  st.session_state.messages.append(("user", user_input))
143
+
144
  # Invoke AI model
145
  config = {'configurable': {'session_id': session_id}}
146
  response = chat.invoke({'prompt': user_input}, config)
147
+
148
  # Save AI response
149
  save_message(session_id, "assistant", response)
150
  st.session_state.messages.append(("assistant", response))
151
+
152
  # Display AI response
153
+ with chat_container:
154
+ with st.chat_message("assistant"):
155
+ st.markdown(response)
156
 
157
+ # βœ… Clear the input field after message submission
158
  st.session_state.pop("user_message")
159
+ st.session_state["user_message"] = ""
160
  st.rerun() # Refresh the app
161
+