67Ayush87 commited on
Commit
91c4568
·
verified ·
1 Parent(s): b82e693

Update pages/gen_ai.py

Browse files
Files changed (1) hide show
  1. pages/gen_ai.py +20 -25
pages/gen_ai.py CHANGED
@@ -3,31 +3,22 @@ import os
3
  from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
4
  from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
5
 
6
-
7
-
8
  hf = os.getenv('Data_science')
9
  os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
10
  os.environ['HF_TOKEN'] = hf
 
11
  # --- Config ---
12
- st.set_page_config(page_title="AI Mentor Chat", layout="centered")
13
- st.title("🤖 AI Mentor Chat")
14
 
15
  # --- Sidebar for selections ---
16
  st.sidebar.title("Mentor Preferences")
17
- sub = st.sidebar.text_input("Enter subject:", value="Python")
18
- exp1 = ['<1', '1', '2', '3', '4', '5', '5+']
19
- exp = st.sidebar.selectbox("Select experience:", exp1)
20
-
21
- # Map experience to label
22
- experience_map = {
23
- '<1': 'New bie mentor',
24
- '1': '1', '2': '2', '3': '3', '4': '4', '5': '5',
25
- '5+': 'Professional'
26
- }
27
- experience_label = experience_map[exp]
28
 
29
  # --- Initialize Chat Model ---
30
- deep_seek_skeleton = HuggingFaceEndpoint(
31
  repo_id='google/gemma-2-9b-it',
32
  provider='nebius',
33
  temperature=0.7,
@@ -35,8 +26,8 @@ deep_seek_skeleton = HuggingFaceEndpoint(
35
  task='conversational'
36
  )
37
 
38
- deep_seek = ChatHuggingFace(
39
- llm=deep_seek_skeleton,
40
  repo_id='google/gemma-2-9b-it',
41
  provider='nebius',
42
  temperature=0.7,
@@ -45,8 +36,8 @@ deep_seek = ChatHuggingFace(
45
  )
46
 
47
  # --- Session State ---
48
- if "chat_history" not in st.session_state:
49
- st.session_state.chat_history = []
50
 
51
  # --- Chat Form ---
52
  with st.form(key="chat_form"):
@@ -56,20 +47,24 @@ with st.form(key="chat_form"):
56
  # --- Chat Logic ---
57
  if submit and user_input:
58
  # Add system context
59
- system_prompt = f"Act as a microsoft excel mentor who has {experience_label} years of experience who teaches in a very friendly manner and also tells everything in within 150 words. If any question is asked other than microsoft excel tell politely that this is out of the topic question"
 
 
 
 
60
 
61
  # Create message list
62
  messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
63
 
64
  # Get model response
65
- result = deep_seek.invoke(messages)
66
 
67
  # Append to history
68
- st.session_state.chat_history.append((user_input, result.content))
69
 
70
  # --- Display Chat History ---
71
  st.subheader("🗨️ Chat History")
72
- for i, (user, bot) in enumerate(st.session_state.chat_history):
73
  st.markdown(f"**You:** {user}")
74
  st.markdown(f"**Mentor:** {bot}")
75
- st.markdown("---")
 
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(
22
  repo_id='google/gemma-2-9b-it',
23
  provider='nebius',
24
  temperature=0.7,
 
26
  task='conversational'
27
  )
28
 
29
+ genai_chat = ChatHuggingFace(
30
+ llm=genai_skeleton,
31
  repo_id='google/gemma-2-9b-it',
32
  provider='nebius',
33
  temperature=0.7,
 
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"):
 
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("---")