67Ayush87 commited on
Commit
89df51d
·
verified ·
1 Parent(s): 592a9f8

Update pages/gen_ai.py

Browse files
Files changed (1) hide show
  1. pages/gen_ai.py +18 -22
pages/gen_ai.py CHANGED
@@ -1,21 +1,22 @@
1
  import streamlit as st
2
  import os
3
  from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
4
- from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
5
 
6
  # Set Hugging Face tokens
7
  hf = os.getenv('Data_science')
8
  os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
9
  os.environ['HF_TOKEN'] = hf
10
 
11
- # --- Config ---
12
  st.set_page_config(page_title="GenAI Mentor Chat", layout="centered")
13
  st.title("🤖 GenAI Mentor Chat")
14
 
15
- # --- Sidebar for selections ---
16
  st.sidebar.title("Mentor Preferences")
17
- exp_options = ['Beginner', 'Intermediate', 'Experienced']
18
- exp = st.sidebar.selectbox("Select your experience level:", exp_options)
 
19
 
20
  # --- Initialize Chat Model ---
21
  genai_skeleton = HuggingFaceEndpoint(
@@ -35,36 +36,31 @@ genai_chat = ChatHuggingFace(
35
  task='conversational'
36
  )
37
 
38
- # --- Session State ---
39
- if "chat_history_genai" not in st.session_state:
40
- st.session_state.chat_history_genai = []
 
 
41
 
42
- # --- Chat Form ---
43
  with st.form(key="chat_form"):
44
  user_input = st.text_input("Ask your question:")
45
  submit = st.form_submit_button("Send")
46
 
47
  # --- Chat Logic ---
48
  if submit and user_input:
49
- # Add system context
50
  system_prompt = (
51
- f"You are a Generative AI mentor with {exp.lower()} experience level. "
52
- f"Answer questions only related to Generative AI in a friendly tone and under 150 words. "
53
- f"If the question is not related to Generative AI, politely inform the user that it's out of scope."
54
  )
55
-
56
- # Create message list
57
  messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
58
-
59
- # Get model response
60
  result = genai_chat.invoke(messages)
 
61
 
62
- # Append to history
63
- st.session_state.chat_history_genai.append((user_input, result.content))
64
-
65
- # --- Display Chat History ---
66
  st.subheader("🗨️ Chat History")
67
- for user, bot in st.session_state.chat_history_genai:
68
  st.markdown(f"**You:** {user}")
69
  st.markdown(f"**Mentor:** {bot}")
70
  st.markdown("---")
 
1
  import streamlit as st
2
  import os
3
  from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
4
+ from langchain_core.messages import HumanMessage, SystemMessage
5
 
6
  # Set Hugging Face tokens
7
  hf = os.getenv('Data_science')
8
  os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
9
  os.environ['HF_TOKEN'] = hf
10
 
11
+ # --- Page Configuration ---
12
  st.set_page_config(page_title="GenAI Mentor Chat", layout="centered")
13
  st.title("🤖 GenAI Mentor Chat")
14
 
15
+ # --- Sidebar: Experience Level ---
16
  st.sidebar.title("Mentor Preferences")
17
+ experience_label = st.sidebar.selectbox(
18
+ "Select your experience level:", ["Beginner", "Intermediate", "Experienced"]
19
+ )
20
 
21
  # --- Initialize Chat Model ---
22
  genai_skeleton = HuggingFaceEndpoint(
 
36
  task='conversational'
37
  )
38
 
39
+ PAGE_KEY = "genai_chat_history"
40
+
41
+ # --- Session State Initialization ---
42
+ if PAGE_KEY not in st.session_state:
43
+ st.session_state[PAGE_KEY] = []
44
 
45
+ # --- Chat Input Form ---
46
  with st.form(key="chat_form"):
47
  user_input = st.text_input("Ask your question:")
48
  submit = st.form_submit_button("Send")
49
 
50
  # --- Chat Logic ---
51
  if submit and user_input:
 
52
  system_prompt = (
53
+ f"You are a Generative AI mentor with {experience_label.lower()} experience. "
54
+ f"Answer only Generative AI questions in a friendly tone, within 150 words. "
55
+ f"Politely inform if the question is out of scope."
56
  )
 
 
57
  messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
 
 
58
  result = genai_chat.invoke(messages)
59
+ st.session_state[PAGE_KEY].append((user_input, result.content))
60
 
61
+ # --- Chat History Display ---
 
 
 
62
  st.subheader("🗨️ Chat History")
63
+ for user, bot in st.session_state[PAGE_KEY]:
64
  st.markdown(f"**You:** {user}")
65
  st.markdown(f"**Mentor:** {bot}")
66
  st.markdown("---")