Spaces:
Sleeping
Sleeping
Update pages/python.py
Browse files- pages/python.py +144 -48
pages/python.py
CHANGED
|
@@ -1,68 +1,164 @@
|
|
| 1 |
-
import streamlit as st
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
from langchain_huggingface import HuggingFaceEndpoint,HuggingFacePipeline,ChatHuggingFace
|
| 12 |
-
from langchain_core.messages import HumanMessage,AIMessage,SystemMessage
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
task = 'conversational')
|
| 19 |
-
deep_seek = ChatHuggingFace(llm=deep_seek_skeleton,
|
| 20 |
-
repo_id='meta-llama/Llama-3.2-3B-Instruct',
|
| 21 |
-
provider = 'sambanova',
|
| 22 |
-
temperature=0.7,
|
| 23 |
-
max_new_tokens=150,
|
| 24 |
-
task = 'conversational')
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
-
selec = ['Python', 'Machine Learning', 'Deep Learning', 'Statistics', 'SQL', 'Excel']
|
| 47 |
-
sub = st.selectbox("Select experience:", selec)
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
-
user_input = st.text_input("Enter your query:")
|
| 55 |
-
l = []
|
| 56 |
-
st.write(l)
|
| 57 |
-
message = [SystemMessage(content=f'Act as {sub} mentor who has {experince} years of experience and the one who teaches in very friendly manner and also he explains everything within 150 words'),
|
| 58 |
-
|
| 59 |
-
while user_input!='end':
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 3 |
+
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
|
| 4 |
|
| 5 |
+
# --- Config ---
|
| 6 |
+
st.set_page_config(page_title="AI Mentor Chat", layout="centered")
|
| 7 |
+
st.title("🤖 AI Mentor Chat")
|
| 8 |
|
| 9 |
+
# --- Sidebar for selections ---
|
| 10 |
+
st.sidebar.title("Mentor Preferences")
|
| 11 |
+
sub = st.sidebar.text_input("Enter subject:", value="Python")
|
| 12 |
+
exp1 = ['<1', '1', '2', '3', '4', '5', '5+']
|
| 13 |
+
exp = st.sidebar.selectbox("Select experience:", exp1)
|
| 14 |
|
| 15 |
+
# Map experience to label
|
| 16 |
+
experience_map = {
|
| 17 |
+
'<1': 'New bie mentor',
|
| 18 |
+
'1': '1', '2': '2', '3': '3', '4': '4', '5': '5',
|
| 19 |
+
'5+': 'Professional'
|
| 20 |
+
}
|
| 21 |
+
experience_label = experience_map[exp]
|
| 22 |
|
| 23 |
+
# --- Initialize Chat Model ---
|
| 24 |
+
deep_seek_skeleton = HuggingFaceEndpoint(
|
| 25 |
+
repo_id='meta-llama/Llama-3.2-3B-Instruct',
|
| 26 |
+
provider='sambanova',
|
| 27 |
+
temperature=0.7,
|
| 28 |
+
max_new_tokens=150,
|
| 29 |
+
task='conversational'
|
| 30 |
+
)
|
| 31 |
|
| 32 |
+
deep_seek = ChatHuggingFace(
|
| 33 |
+
llm=deep_seek_skeleton,
|
| 34 |
+
repo_id='meta-llama/Llama-3.2-3B-Instruct',
|
| 35 |
+
provider='sambanova',
|
| 36 |
+
temperature=0.7,
|
| 37 |
+
max_new_tokens=150,
|
| 38 |
+
task='conversational'
|
| 39 |
+
)
|
| 40 |
|
| 41 |
+
# --- Session State ---
|
| 42 |
+
if "chat_history" not in st.session_state:
|
| 43 |
+
st.session_state.chat_history = []
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# --- Chat 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 |
+
# Add system context
|
| 53 |
+
system_prompt = f"Act as a {sub} mentor who has {experience_label} years of experience and teaches in a very friendly manner. Keep explanations within 150 words."
|
| 54 |
+
|
| 55 |
+
# Create message list
|
| 56 |
+
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
| 57 |
+
|
| 58 |
+
# Get model response
|
| 59 |
+
result = deep_seek.invoke(messages)
|
| 60 |
+
|
| 61 |
+
# Append to history
|
| 62 |
+
st.session_state.chat_history.append((user_input, result.content))
|
| 63 |
+
|
| 64 |
+
# --- Display Chat History ---
|
| 65 |
+
st.subheader("🗨️ Chat History")
|
| 66 |
+
for i, (user, bot) in enumerate(st.session_state.chat_history):
|
| 67 |
+
st.markdown(f"**You:** {user}")
|
| 68 |
+
st.markdown(f"**Mentor:** {bot}")
|
| 69 |
+
st.markdown("---")
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# import streamlit as st
|
| 104 |
+
# import os
|
| 105 |
+
# import langchain
|
| 106 |
+
# import langchain_huggingface
|
| 107 |
+
# from langchain_huggingface import HuggingFaceEndpoint,HuggingFacePipeline,ChatHuggingFace
|
| 108 |
+
# from langchain_core.messages import HumanMessage,AIMessage,SystemMessage
|
| 109 |
+
|
| 110 |
+
# deep_seek_skeleton = HuggingFaceEndpoint(repo_id='meta-llama/Llama-3.2-3B-Instruct',
|
| 111 |
+
# provider = 'sambanova',
|
| 112 |
+
# temperature=0.7,
|
| 113 |
+
# max_new_tokens=150,
|
| 114 |
+
# task = 'conversational')
|
| 115 |
+
# deep_seek = ChatHuggingFace(llm=deep_seek_skeleton,
|
| 116 |
+
# repo_id='meta-llama/Llama-3.2-3B-Instruct',
|
| 117 |
+
# provider = 'sambanova',
|
| 118 |
+
# temperature=0.7,
|
| 119 |
+
# max_new_tokens=150,
|
| 120 |
+
# task = 'conversational')
|
| 121 |
+
|
| 122 |
+
# exp1 = ['<1', '1', '2', '3', '4', '5', '5+']
|
| 123 |
+
# exp = st.selectbox("Select experience:", exp1)
|
| 124 |
+
# if exp == '<1':
|
| 125 |
+
# experince = 'New bie mentor'
|
| 126 |
+
# elif exp == '1':
|
| 127 |
+
# experince = '1'
|
| 128 |
+
# elif exp == '2':
|
| 129 |
+
# experince = '2'
|
| 130 |
+
# elif exp == '3':
|
| 131 |
+
# experince = '3'
|
| 132 |
+
# elif exp == '4':
|
| 133 |
+
# experince = '4'
|
| 134 |
+
# elif exp == '5':
|
| 135 |
+
# experince = '5'
|
| 136 |
+
# elif exp == '5+':
|
| 137 |
+
# experince = 'professional'
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
+
# selec = ['Python', 'Machine Learning', 'Deep Learning', 'Statistics', 'SQL', 'Excel']
|
| 143 |
+
# sub = st.selectbox("Select experience:", selec)
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
+
# user_input = st.text_input("Enter your query:")
|
| 151 |
+
# l = []
|
| 152 |
+
# st.write(l)
|
| 153 |
+
# message = [SystemMessage(content=f'Act as {sub} mentor who has {experince} years of experience and the one who teaches in very friendly manner and also he explains everything within 150 words'),
|
| 154 |
+
# HumanMessage(content=user_input)]
|
| 155 |
+
# while user_input!='end':
|
| 156 |
+
# l.append(user_input)
|
| 157 |
+
# l.append(result.content)
|
| 158 |
+
# st.write(l)
|
| 159 |
+
# user_input = st.text_input("Enter your query:")
|
| 160 |
+
# message = [SystemMessage(content=f'Act as {sub} mentor who has {experince} years of experience and the one who teaches in very friendly manner and also he explains everything within 150 words'),
|
| 161 |
+
# HumanMessage(content=user_input)]
|
| 162 |
+
# result = deep_seek.invoke(message)
|
| 163 |
|
| 164 |
|