saramneena commited on
Commit
99f5605
Β·
verified Β·
1 Parent(s): 2c38bfa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -14
app.py CHANGED
@@ -4,31 +4,48 @@ from langchain.memory import ConversationBufferMemory
4
  from langchain.chains import ConversationChain
5
  from langchain_google_genai import ChatGoogleGenerativeAI
6
 
7
-
8
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
9
 
 
 
 
 
 
10
  if not GEMINI_API_KEY:
11
  st.error("❌ GEMINI_API_KEY not found. Please add it in Hugging Face β†’ Settings β†’ Variables and secrets.")
12
  else:
13
- # Initialize Gemini LLM
14
- llm = ChatGoogleGenerativeAI(model="gemini-1.5-pro", google_api_key=GEMINI_API_KEY)
 
 
 
15
 
16
- # Add memory for conversation awareness
17
  memory = ConversationBufferMemory()
18
  conversation = ConversationChain(
19
  llm=llm,
20
  memory=memory,
21
- verbose=True
22
  )
23
 
24
- # Streamlit App UI
25
- st.set_page_config(page_title="Conversational AI Data Science Tutor", page_icon="πŸ€–")
26
- st.title("πŸ€– Conversational AI Data Science Tutor")
27
- st.write("Ask me any **Data Science** related question!")
 
 
 
 
 
 
 
 
 
28
 
29
- # User input
30
- user_input = st.text_input("You:", placeholder="Ask a data science question...")
31
 
32
- if user_input:
33
- response = conversation.predict(input=user_input)
34
- st.write("**Tutor:**", response)
 
4
  from langchain.chains import ConversationChain
5
  from langchain_google_genai import ChatGoogleGenerativeAI
6
 
7
+ # Get Gemini API key from environment variable
8
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
9
 
10
+ st.set_page_config(page_title="Conversational AI Data Science Tutor", page_icon="πŸ€–")
11
+
12
+ st.title("πŸ€– Conversational AI Data Science Tutor")
13
+ st.write("Ask me any **Data Science** related question!")
14
+
15
  if not GEMINI_API_KEY:
16
  st.error("❌ GEMINI_API_KEY not found. Please add it in Hugging Face β†’ Settings β†’ Variables and secrets.")
17
  else:
18
+ # Initialize Gemini LLM
19
+ llm = ChatGoogleGenerativeAI(
20
+ model="gemini-1.5-pro",
21
+ google_api_key=GEMINI_API_KEY
22
+ )
23
 
24
+ # Add memory for conversation awareness
25
  memory = ConversationBufferMemory()
26
  conversation = ConversationChain(
27
  llm=llm,
28
  memory=memory,
29
+ verbose=False
30
  )
31
 
32
+ # Initialize chat history in session_state
33
+ if "messages" not in st.session_state:
34
+ st.session_state.messages = []
35
+
36
+ # Display previous messages
37
+ for msg in st.session_state.messages:
38
+ st.chat_message(msg["role"]).markdown(msg["content"])
39
+
40
+ # User input with chat-style box
41
+ if prompt := st.chat_input("Ask a data science question..."):
42
+ # Save user message
43
+ st.session_state.messages.append({"role": "user", "content": prompt})
44
+ st.chat_message("user").markdown(prompt)
45
 
46
+ # Get model response
47
+ response = conversation.predict(input=prompt)
48
 
49
+ # Save assistant message
50
+ st.session_state.messages.append({"role": "assistant", "content": response})
51
+ st.chat_message("assistant").markdown(response)