Spaces:
Running
Running
Update pages/sql.py
Browse files- pages/sql.py +19 -32
pages/sql.py
CHANGED
@@ -3,31 +3,21 @@ 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="
|
13 |
-
st.title("
|
14 |
|
15 |
# --- Sidebar for selections ---
|
16 |
st.sidebar.title("Mentor Preferences")
|
17 |
-
|
18 |
-
|
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 |
-
|
31 |
repo_id='marin-community/marin-8b-instruct',
|
32 |
provider='together',
|
33 |
temperature=0.7,
|
@@ -35,8 +25,8 @@ deep_seek_skeleton = HuggingFaceEndpoint(
|
|
35 |
task='conversational'
|
36 |
)
|
37 |
|
38 |
-
|
39 |
-
llm=
|
40 |
repo_id='marin-community/marin-8b-instruct',
|
41 |
provider='together',
|
42 |
temperature=0.7,
|
@@ -45,8 +35,8 @@ deep_seek = ChatHuggingFace(
|
|
45 |
)
|
46 |
|
47 |
# --- Session State ---
|
48 |
-
if "
|
49 |
-
st.session_state.
|
50 |
|
51 |
# --- Chat Form ---
|
52 |
with st.form(key="chat_form"):
|
@@ -55,21 +45,18 @@ with st.form(key="chat_form"):
|
|
55 |
|
56 |
# --- Chat Logic ---
|
57 |
if submit and user_input:
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
62 |
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
63 |
-
|
64 |
-
|
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
|
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 |
hf = os.getenv('Data_science')
|
7 |
os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
|
8 |
os.environ['HF_TOKEN'] = hf
|
9 |
+
|
10 |
# --- Config ---
|
11 |
+
st.set_page_config(page_title="SQL Mentor Chat", layout="centered")
|
12 |
+
st.title("🗃️ SQL Mentor Chat")
|
13 |
|
14 |
# --- Sidebar for selections ---
|
15 |
st.sidebar.title("Mentor Preferences")
|
16 |
+
exp_options = ['Beginner', 'Intermediate', 'Experienced']
|
17 |
+
exp = st.sidebar.selectbox("Select your experience level:", exp_options)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# --- Initialize Chat Model ---
|
20 |
+
sql_model_skeleton = HuggingFaceEndpoint(
|
21 |
repo_id='marin-community/marin-8b-instruct',
|
22 |
provider='together',
|
23 |
temperature=0.7,
|
|
|
25 |
task='conversational'
|
26 |
)
|
27 |
|
28 |
+
sql_mentor = ChatHuggingFace(
|
29 |
+
llm=sql_model_skeleton,
|
30 |
repo_id='marin-community/marin-8b-instruct',
|
31 |
provider='together',
|
32 |
temperature=0.7,
|
|
|
35 |
)
|
36 |
|
37 |
# --- Session State ---
|
38 |
+
if "chat_history_sql" not in st.session_state:
|
39 |
+
st.session_state.chat_history_sql = []
|
40 |
|
41 |
# --- Chat Form ---
|
42 |
with st.form(key="chat_form"):
|
|
|
45 |
|
46 |
# --- Chat Logic ---
|
47 |
if submit and user_input:
|
48 |
+
system_prompt = (
|
49 |
+
f"Act as a SQL mentor with {exp.lower()} experience level. "
|
50 |
+
f"Explain in a very friendly manner and keep answers within 150 words. "
|
51 |
+
f"If the question is not about SQL, politely say it is out of scope."
|
52 |
+
)
|
53 |
messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
|
54 |
+
result = sql_mentor.invoke(messages)
|
55 |
+
st.session_state.chat_history_sql.append((user_input, result.content))
|
|
|
|
|
|
|
|
|
56 |
|
57 |
# --- Display Chat History ---
|
58 |
st.subheader("🗨️ Chat History")
|
59 |
+
for user, bot in st.session_state.chat_history_sql:
|
60 |
st.markdown(f"**You:** {user}")
|
61 |
st.markdown(f"**Mentor:** {bot}")
|
62 |
+
st.markdown("---")
|