Akhil4839 commited on
Commit
361b3c0
Β·
verified Β·
1 Parent(s): 79ad7f1

Create 6_SQL.py

Browse files
Files changed (1) hide show
  1. pages/6_SQL.py +121 -0
pages/6_SQL.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 Environment Token ---
7
+ hf = os.getenv('Data_science')
8
+ os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
9
+ os.environ['HF_TOKEN'] = hf
10
+
11
+ # --- Streamlit Page Configuration ---
12
+ st.set_page_config(page_title="πŸ—ƒοΈ SQL Mentor Chat", page_icon="πŸ—ƒοΈ", layout="centered")
13
+
14
+ # --- Custom CSS Styling ---
15
+ st.markdown("""
16
+ <style>
17
+ .main {
18
+ background: linear-gradient(135deg, #430089 0%, #82ffa1 100%);
19
+ padding: 2rem;
20
+ font-family: 'Segoe UI', sans-serif;
21
+ }
22
+ h1, h2, h3, h4, h5, h6, p, label, .css-10trblm, .css-q8sbsg {
23
+ color: #ffffff !important;
24
+ text-align: center;
25
+ }
26
+ .stTextInput > div > div > input {
27
+ background-color: rgba(255, 255, 255, 0.1);
28
+ color: white;
29
+ border: 1px solid rgba(255, 255, 255, 0.5);
30
+ border-radius: 8px;
31
+ padding: 0.6em;
32
+ }
33
+ .stTextInput > div > div > input::placeholder {
34
+ color: rgba(255, 255, 255, 0.6);
35
+ }
36
+ .stButton>button {
37
+ background: rgba(255, 255, 255, 0.15);
38
+ border: 2px solid rgba(255, 255, 255, 0.4);
39
+ color: white;
40
+ font-size: 18px;
41
+ font-weight: bold;
42
+ padding: 0.8em 1.2em;
43
+ border-radius: 12px;
44
+ width: 100%;
45
+ transition: all 0.3s ease;
46
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
47
+ }
48
+ .stButton>button:hover {
49
+ background: rgba(255, 255, 255, 0.3);
50
+ border-color: white;
51
+ color: white;
52
+ }
53
+ .stSidebar > div:first-child {
54
+ background: #2c3e50;
55
+ padding: 1rem;
56
+ border-radius: 0 15px 15px 0;
57
+ }
58
+ .stSidebar h1, .stSidebar h2, .stSidebar h3, .stSidebar label, .stSidebar p {
59
+ color: white !important;
60
+ }
61
+ hr {
62
+ border: 1px solid rgba(255, 255, 255, 0.3);
63
+ margin: 2em 0;
64
+ }
65
+ </style>
66
+ """, unsafe_allow_html=True)
67
+
68
+ # --- Title ---
69
+ st.title("πŸ—ƒοΈ SQL Mentor Chat")
70
+ st.markdown("### πŸ’¬ Ask your SQL queries below. Learn by doing!")
71
+
72
+ # --- Sidebar for Experience ---
73
+ st.sidebar.title("πŸŽ“ Mentor Preferences")
74
+ exp = st.sidebar.selectbox("πŸ“š Select your experience level:", ["Beginner", "Intermediate", "Expert"])
75
+
76
+ # --- Model Initialization ---
77
+ sql_model_skeleton = HuggingFaceEndpoint(
78
+ repo_id='HuggingFaceH4/zephyr-7b-beta',
79
+ provider='hf-inference',
80
+ temperature=0.7,
81
+ max_new_tokens=110,
82
+ task='conversational'
83
+ )
84
+
85
+ sql_mentor = ChatHuggingFace(
86
+ llm=sql_model_skeleton,
87
+ repo_id='HuggingFaceH4/zephyr-7b-beta',
88
+ provider='hf-inference',
89
+ temperature=0.7,
90
+ max_new_tokens=110,
91
+ task='conversational'
92
+ )
93
+
94
+ # --- Session History Key ---
95
+ PAGE_KEY = "chat_history_sql"
96
+ if PAGE_KEY not in st.session_state:
97
+ st.session_state[PAGE_KEY] = []
98
+
99
+ # --- Chat Form ---
100
+ with st.form(key="chat_form"):
101
+ user_input = st.text_input("πŸ“Œ Ask your SQL question:", placeholder="e.g. What does GROUP BY do in SQL?")
102
+ submit = st.form_submit_button("πŸ“€ Send")
103
+
104
+ # --- Process Query ---
105
+ if submit and user_input:
106
+ system_prompt = (
107
+ f"Act as a SQL mentor with {exp.lower()} expertise. "
108
+ f"Respond in a clear and friendly tone. "
109
+ f"Only answer SQL-related questions and keep responses under 150 words. "
110
+ f"If a question is outside SQL, politely say it's out of scope."
111
+ )
112
+ messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
113
+ result = sql_mentor.invoke(messages)
114
+ st.session_state[PAGE_KEY].append((user_input, result.content))
115
+
116
+ # --- Display Chat History ---
117
+ st.subheader("πŸ“œ Chat History")
118
+ for user, bot in st.session_state[PAGE_KEY]:
119
+ st.markdown(f"**πŸ™‹ You:** {user}")
120
+ st.markdown(f"**🧠 Mentor:** {bot}")
121
+ st.markdown("---")