67Ayush87 commited on
Commit
1ae40b2
Β·
verified Β·
1 Parent(s): dc6d8bd

Update pages/python.py

Browse files
Files changed (1) hide show
  1. pages/python.py +64 -24
pages/python.py CHANGED
@@ -1,63 +1,103 @@
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
- # Load API token from environment
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="Python Mentor Chat", layout="centered")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  st.title("🐍 Python 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
- python_model_skeleton = HuggingFaceEndpoint(
22
  repo_id='meta-llama/Llama-3.2-3B-Instruct',
23
  provider='sambanova',
24
  temperature=0.7,
25
  max_new_tokens=150,
26
  task='conversational'
27
  )
28
-
29
- python_mentor = ChatHuggingFace(
30
- llm=python_model_skeleton,
31
  repo_id='meta-llama/Llama-3.2-3B-Instruct',
32
  provider='sambanova',
33
  temperature=0.7,
34
- max_new_tokens=150,
35
  task='conversational'
36
  )
37
 
38
- # --- Session State ---
39
- if "chat_history_python" not in st.session_state:
40
- st.session_state.chat_history_python = []
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
  system_prompt = (
50
- f"You are a Python mentor with {exp.lower()} experience level. "
51
- f"Answer only Python-related questions in a very friendly tone and under 150 words. "
52
- f"If the question is not about Python, politely say it's out of scope."
53
  )
54
  messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
55
- result = python_mentor.invoke(messages)
56
- st.session_state.chat_history_python.append((user_input, result.content))
57
 
58
- # --- Display Chat History ---
59
  st.subheader("πŸ—¨οΈ Chat History")
60
- for user, bot in st.session_state.chat_history_python:
61
  st.markdown(f"**You:** {user}")
62
  st.markdown(f"**Mentor:** {bot}")
63
  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
  hf = os.getenv('Data_science')
7
  os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
8
  os.environ['HF_TOKEN'] = hf
9
 
10
+ # Page config
11
  st.set_page_config(page_title="Python Mentor Chat", layout="centered")
12
+
13
+ # Inject home page CSS style
14
+ st.markdown("""
15
+ <style>
16
+ .main {
17
+ background: linear-gradient(135deg, #430089 0%, #82ffa1 100%);
18
+ padding: 2rem;
19
+ font-family: 'Segoe UI', sans-serif;
20
+ }
21
+ .stButton>button {
22
+ background: #ffffff10;
23
+ border: 2px solid #ffffff50;
24
+ color: white;
25
+ font-size: 18px;
26
+ font-weight: 600;
27
+ padding: 0.8em 1.2em;
28
+ border-radius: 12px;
29
+ width: 100%;
30
+ transition: 0.3s ease;
31
+ box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
32
+ }
33
+ .stButton>button:hover {
34
+ background: #ffffff30;
35
+ border-color: #fff;
36
+ color: #ffffff;
37
+ }
38
+ h1, h3, p, label {
39
+ color: #ffffff;
40
+ text-align: center;
41
+ }
42
+ hr {
43
+ border: 1px solid #ffffff50;
44
+ margin: 2em 0;
45
+ }
46
+ .css-1aumxhk {
47
+ color: white;
48
+ }
49
+ </style>
50
+ """, unsafe_allow_html=True)
51
+
52
+ # Title
53
  st.title("🐍 Python Mentor Chat")
54
 
55
+ # Sidebar
56
  st.sidebar.title("Mentor Preferences")
57
+ experience_label = st.sidebar.selectbox(
58
+ "Select your experience level:", ["Beginner", "Intermediate", "Experienced"]
59
+ )
60
 
61
+ # Initialize model
62
+ deep_seek_skeleton = HuggingFaceEndpoint(
63
  repo_id='meta-llama/Llama-3.2-3B-Instruct',
64
  provider='sambanova',
65
  temperature=0.7,
66
  max_new_tokens=150,
67
  task='conversational'
68
  )
69
+ deep_seek = ChatHuggingFace(
70
+ llm=deep_seek_skeleton,
 
71
  repo_id='meta-llama/Llama-3.2-3B-Instruct',
72
  provider='sambanova',
73
  temperature=0.7,
74
+ max_new_tokens=50,
75
  task='conversational'
76
  )
77
 
78
+ PAGE_KEY = "python_chat_history"
79
+ if PAGE_KEY not in st.session_state:
80
+ st.session_state[PAGE_KEY] = []
81
 
82
+ # Chat input form
83
  with st.form(key="chat_form"):
84
  user_input = st.text_input("Ask your question:")
85
  submit = st.form_submit_button("Send")
86
 
87
+ # Chat logic
88
  if submit and user_input:
89
  system_prompt = (
90
+ f"Act as a python mentor with {experience_label.lower()} experience. "
91
+ f"Teach in a friendly manner and keep answers within 150 words. "
92
+ f"If a question is not about python, politely mention it's out of scope."
93
  )
94
  messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
95
+ result = deep_seek.invoke(messages)
96
+ st.session_state[PAGE_KEY].append((user_input, result.content))
97
 
98
+ # Chat history
99
  st.subheader("πŸ—¨οΈ Chat History")
100
+ for user, bot in st.session_state[PAGE_KEY]:
101
  st.markdown(f"**You:** {user}")
102
  st.markdown(f"**Mentor:** {bot}")
103
  st.markdown("---")