Spaces:
Running
Running
Create GenAI.py
Browse files- pages/GenAI.py +130 -0
pages/GenAI.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Load Hugging Face token from environment
|
7 |
+
hf = os.getenv('HF_TOKEN')
|
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", layout="wide")
|
13 |
+
|
14 |
+
# --- Custom CSS Styling ---
|
15 |
+
st.markdown("""
|
16 |
+
<style>
|
17 |
+
body {
|
18 |
+
background-color: #1e1e2f;
|
19 |
+
font-family: 'Segoe UI', sans-serif;
|
20 |
+
}
|
21 |
+
.main {
|
22 |
+
background: linear-gradient(to right, #3e32a8, #80ffe0);
|
23 |
+
padding: 2rem;
|
24 |
+
border-radius: 12px;
|
25 |
+
}
|
26 |
+
h1, h2, h3, p, label {
|
27 |
+
color: white !important;
|
28 |
+
text-align: center;
|
29 |
+
}
|
30 |
+
.stTextInput>div>div>input {
|
31 |
+
border: 1px solid #ddd;
|
32 |
+
border-radius: 10px;
|
33 |
+
padding: 0.5rem;
|
34 |
+
font-size: 16px;
|
35 |
+
width: 100%;
|
36 |
+
}
|
37 |
+
.stButton>button {
|
38 |
+
background-color: #6a00ff;
|
39 |
+
color: white;
|
40 |
+
padding: 0.6rem 1.2rem;
|
41 |
+
font-size: 16px;
|
42 |
+
border: none;
|
43 |
+
border-radius: 10px;
|
44 |
+
transition: 0.3s ease;
|
45 |
+
}
|
46 |
+
.stButton>button:hover {
|
47 |
+
background-color: #5300e8;
|
48 |
+
}
|
49 |
+
.chat-box {
|
50 |
+
background-color: #ffffff15;
|
51 |
+
border-radius: 12px;
|
52 |
+
padding: 1rem;
|
53 |
+
margin-bottom: 1rem;
|
54 |
+
color: white;
|
55 |
+
line-height: 1.6;
|
56 |
+
}
|
57 |
+
.user-msg {
|
58 |
+
color: #e0e0e0;
|
59 |
+
font-weight: 600;
|
60 |
+
}
|
61 |
+
.bot-msg {
|
62 |
+
color: #ffffff;
|
63 |
+
margin-top: 0.3rem;
|
64 |
+
}
|
65 |
+
hr {
|
66 |
+
border: 0;
|
67 |
+
height: 1px;
|
68 |
+
background: #ffffff30;
|
69 |
+
margin: 1.5rem 0;
|
70 |
+
}
|
71 |
+
</style>
|
72 |
+
""", unsafe_allow_html=True)
|
73 |
+
|
74 |
+
# --- Title ---
|
75 |
+
st.title("🤖 GenAI Mentor Chat")
|
76 |
+
st.markdown("<p style='font-size:18px;'>Empowering your AI journey—one question at a time.</p>", unsafe_allow_html=True)
|
77 |
+
|
78 |
+
# --- Sidebar ---
|
79 |
+
st.sidebar.title("Mentor Preferences")
|
80 |
+
experience_label = st.sidebar.selectbox("Choose your experience level:", ["Beginner", "Intermediate", "Expert"])
|
81 |
+
|
82 |
+
# --- Model Initialization ---
|
83 |
+
genai_skeleton = HuggingFaceEndpoint(
|
84 |
+
repo_id='google/gemma-2-9b-it',
|
85 |
+
provider='nebius',
|
86 |
+
temperature=0.7,
|
87 |
+
max_new_tokens=50,
|
88 |
+
task='conversational'
|
89 |
+
)
|
90 |
+
|
91 |
+
genai_chat = ChatHuggingFace(
|
92 |
+
llm=genai_skeleton,
|
93 |
+
repo_id='google/gemma-2-9b-it',
|
94 |
+
provider='nebius',
|
95 |
+
temperature=0.7,
|
96 |
+
max_new_tokens=50,
|
97 |
+
task='conversational'
|
98 |
+
)
|
99 |
+
|
100 |
+
# --- Session Key ---
|
101 |
+
PAGE_KEY = "genai_chat_history"
|
102 |
+
if PAGE_KEY not in st.session_state:
|
103 |
+
st.session_state[PAGE_KEY] = []
|
104 |
+
|
105 |
+
# --- Chat Form ---
|
106 |
+
with st.form(key="chat_form"):
|
107 |
+
st.markdown("#### 💬 Ask your question:")
|
108 |
+
user_input = st.text_input("", placeholder="Type your Generative AI question here...")
|
109 |
+
submit = st.form_submit_button("Send")
|
110 |
+
|
111 |
+
# --- Handle Submission ---
|
112 |
+
if submit and user_input:
|
113 |
+
system_prompt = (
|
114 |
+
f"Act as a Generative AI mentor with {experience_label.lower()} expertise. "
|
115 |
+
f"Explain concepts in a friendly tone, within 150 words. "
|
116 |
+
f"If the question is not related to Generative AI, politely say it's out of scope."
|
117 |
+
)
|
118 |
+
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
119 |
+
result = genai_chat.invoke(messages)
|
120 |
+
st.session_state[PAGE_KEY].append((user_input, result.content))
|
121 |
+
|
122 |
+
# --- Display Chat History ---
|
123 |
+
st.subheader("🗨️ Conversation History")
|
124 |
+
for user, bot in reversed(st.session_state[PAGE_KEY]):
|
125 |
+
st.markdown(f"""
|
126 |
+
<div class="chat-box">
|
127 |
+
<div class="user-msg">🧑💻 <strong>You:</strong> {user}</div>
|
128 |
+
<div class="bot-msg">🤖 <strong>Mentor:</strong> {bot}</div>
|
129 |
+
</div>
|
130 |
+
""", unsafe_allow_html=True)
|