Spaces:
Running
Running
Update pages/gen_ai.py
Browse files- pages/gen_ai.py +46 -12
pages/gen_ai.py
CHANGED
@@ -3,22 +3,56 @@ import os
|
|
3 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
4 |
from langchain_core.messages import HumanMessage, SystemMessage
|
5 |
|
6 |
-
#
|
7 |
hf = os.getenv('Data_science')
|
8 |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
9 |
os.environ['HF_TOKEN'] = hf
|
10 |
|
11 |
-
# --- Page
|
12 |
st.set_page_config(page_title="GenAI Mentor Chat", layout="centered")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
st.title("🤖 GenAI Mentor Chat")
|
14 |
|
15 |
-
# --- Sidebar
|
16 |
st.sidebar.title("Mentor Preferences")
|
17 |
-
experience_label = st.sidebar.selectbox(
|
18 |
-
"Select your experience level:", ["Beginner", "Intermediate", "Experienced"]
|
19 |
-
)
|
20 |
|
21 |
-
# ---
|
22 |
genai_skeleton = HuggingFaceEndpoint(
|
23 |
repo_id='google/gemma-2-9b-it',
|
24 |
provider='nebius',
|
@@ -42,7 +76,7 @@ PAGE_KEY = "genai_chat_history"
|
|
42 |
if PAGE_KEY not in st.session_state:
|
43 |
st.session_state[PAGE_KEY] = []
|
44 |
|
45 |
-
# --- Chat Input
|
46 |
with st.form(key="chat_form"):
|
47 |
user_input = st.text_input("Ask your question:")
|
48 |
submit = st.form_submit_button("Send")
|
@@ -50,15 +84,15 @@ with st.form(key="chat_form"):
|
|
50 |
# --- Chat Logic ---
|
51 |
if submit and user_input:
|
52 |
system_prompt = (
|
53 |
-
f"
|
54 |
-
f"
|
55 |
-
f"
|
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
|
62 |
st.subheader("🗨️ Chat History")
|
63 |
for user, bot in st.session_state[PAGE_KEY]:
|
64 |
st.markdown(f"**You:** {user}")
|
|
|
3 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
4 |
from langchain_core.messages import HumanMessage, SystemMessage
|
5 |
|
6 |
+
# Load Hugging Face token from environment
|
7 |
hf = os.getenv('Data_science')
|
8 |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
9 |
os.environ['HF_TOKEN'] = hf
|
10 |
|
11 |
+
# --- Page Config ---
|
12 |
st.set_page_config(page_title="GenAI Mentor Chat", layout="centered")
|
13 |
+
|
14 |
+
# --- Custom CSS Styling ---
|
15 |
+
st.markdown("""
|
16 |
+
<style>
|
17 |
+
.main {
|
18 |
+
background: linear-gradient(135deg, #41295a 0%, #2F0743 100%);
|
19 |
+
color: white;
|
20 |
+
padding: 2rem;
|
21 |
+
}
|
22 |
+
.stButton>button {
|
23 |
+
background: #ffffff10;
|
24 |
+
border: 1.5px solid #ffffff50;
|
25 |
+
color: white;
|
26 |
+
font-size: 17px;
|
27 |
+
padding: 0.7em 1.1em;
|
28 |
+
border-radius: 10px;
|
29 |
+
width: 100%;
|
30 |
+
transition: 0.2s ease;
|
31 |
+
font-weight: 600;
|
32 |
+
}
|
33 |
+
.stButton>button:hover {
|
34 |
+
background: #ffffff30;
|
35 |
+
border-color: #fff;
|
36 |
+
color: #ffffff;
|
37 |
+
}
|
38 |
+
h1, h3, label {
|
39 |
+
color: #ffffff;
|
40 |
+
text-align: center;
|
41 |
+
}
|
42 |
+
hr {
|
43 |
+
border: 1px solid #ffffff40;
|
44 |
+
}
|
45 |
+
</style>
|
46 |
+
""", unsafe_allow_html=True)
|
47 |
+
|
48 |
+
# --- Title ---
|
49 |
st.title("🤖 GenAI Mentor Chat")
|
50 |
|
51 |
+
# --- Sidebar for Experience ---
|
52 |
st.sidebar.title("Mentor Preferences")
|
53 |
+
experience_label = st.sidebar.selectbox("Select your experience level:", ["Beginner", "Intermediate", "Expert"])
|
|
|
|
|
54 |
|
55 |
+
# --- Model Initialization ---
|
56 |
genai_skeleton = HuggingFaceEndpoint(
|
57 |
repo_id='google/gemma-2-9b-it',
|
58 |
provider='nebius',
|
|
|
76 |
if PAGE_KEY not in st.session_state:
|
77 |
st.session_state[PAGE_KEY] = []
|
78 |
|
79 |
+
# --- Chat Input ---
|
80 |
with st.form(key="chat_form"):
|
81 |
user_input = st.text_input("Ask your question:")
|
82 |
submit = st.form_submit_button("Send")
|
|
|
84 |
# --- Chat Logic ---
|
85 |
if submit and user_input:
|
86 |
system_prompt = (
|
87 |
+
f"Act as a Generative AI mentor with {experience_label.lower()} expertise. "
|
88 |
+
f"Explain concepts in a friendly tone, within 150 words. "
|
89 |
+
f"If the question is not related to Generative AI, politely say it's out of scope."
|
90 |
)
|
91 |
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
92 |
result = genai_chat.invoke(messages)
|
93 |
st.session_state[PAGE_KEY].append((user_input, result.content))
|
94 |
|
95 |
+
# --- Display Chat History ---
|
96 |
st.subheader("🗨️ Chat History")
|
97 |
for user, bot in st.session_state[PAGE_KEY]:
|
98 |
st.markdown(f"**You:** {user}")
|