Anshini commited on
Commit
2af7ee5
Β·
verified Β·
1 Parent(s): f459124

Create Sql.py

Browse files
Files changed (1) hide show
  1. pages/Sql.py +102 -0
pages/Sql.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Set environment variables
7
+ hf = os.getenv('HF_TOKEN')
8
+ os.environ['HUGGINGFACEHUB_API_TOKEN'] = hf
9
+ os.environ['HF_TOKEN'] = hf
10
+ # Page setup
11
+ st.set_page_config(page_title="SQL Mentor Chat", layout="centered")
12
+
13
+ # Improved custom CSS
14
+ st.markdown("""
15
+ <style>
16
+ .main {
17
+ background: linear-gradient(to right, #1f4037, #99f2c8);
18
+ padding: 2rem;
19
+ font-family: 'Segoe UI', sans-serif;
20
+ }
21
+ .stButton>button {
22
+ background-color: #ffffff10;
23
+ color: white;
24
+ font-weight: 600;
25
+ border-radius: 10px;
26
+ padding: 0.6rem 1rem;
27
+ transition: all 0.3s ease;
28
+ border: 1px solid white;
29
+ }
30
+ .stButton>button:hover {
31
+ background-color: #ffffff30;
32
+ color: white;
33
+ border-color: #fff;
34
+ }
35
+ h1, h3, p, label {
36
+ color: white;
37
+ text-align: center;
38
+ }
39
+ .chat-bubble-user {
40
+ background-color: #ffffff25;
41
+ padding: 0.75rem 1rem;
42
+ border-radius: 1rem;
43
+ margin-bottom: 0.5rem;
44
+ color: #fff;
45
+ font-weight: 500;
46
+ text-align: left;
47
+ width: fit-content;
48
+ max-width: 90%;
49
+ align-self: flex-end;
50
+ }
51
+ .chat-bubble-bot {
52
+ background-color: #ffffff15;
53
+ padding: 0.75rem 1rem;
54
+ border-radius: 1rem;
55
+ margin-bottom: 0.5rem;
56
+ color: #fff;
57
+ text-align: left;
58
+ width: fit-content;
59
+ max-width: 90%;
60
+ align-self: flex-start;
61
+ }
62
+ .chat-container {
63
+ display: flex;
64
+ flex-direction: column;
65
+ gap: 0.5rem;
66
+ margin-top: 2rem;
67
+ }
68
+ </style>
69
+ """, unsafe_allow_html=True)
70
+
71
+ # Page title
72
+ st.markdown("<h1 style='margin-bottom: 0;'>πŸ—ƒοΈ SQL Mentor Chat</h1>", unsafe_allow_html=True)
73
+ st.markdown("<p style='margin-top: 0.2rem;'>Learn SQL interactively from your AI mentor!</p>", unsafe_allow_html=True)
74
+
75
+ # Sidebar for user preference
76
+ st.sidebar.title("Mentor Preferences")
77
+ exp = st.sidebar.selectbox("Choose your experience level:", ["Beginner", "Intermediate", "Expert"])
78
+
79
+ # Chat input box
80
+ st.markdown("<hr>", unsafe_allow_html=True)
81
+ with st.form(key="chat_form"):
82
+ user_input = st.text_input("πŸ’¬ Ask me anything about SQL:")
83
+ submit = st.form_submit_button("Send")
84
+
85
+ # Chat logic
86
+ if submit and user_input:
87
+ system_prompt = (
88
+ f"Act as a SQL mentor with {exp.lower()} expertise. "
89
+ f"Answer in a friendly tone and within 150 words. "
90
+ f"If the question is not SQL-related, politely say it's out of scope."
91
+ )
92
+ messages = [SystemMessage(content=system_prompt), HumanMessage(content=user_input)]
93
+ result = sql_mentor.invoke(messages)
94
+ st.session_state[PAGE_KEY].append((user_input, result.content))
95
+
96
+ # Show conversation as chat bubbles
97
+ if st.session_state[PAGE_KEY]:
98
+ st.markdown('<div class="chat-container">', unsafe_allow_html=True)
99
+ for user, bot in st.session_state[PAGE_KEY]:
100
+ st.markdown(f'<div class="chat-bubble-user">πŸ‘€ <strong>You:</strong> {user}</div>', unsafe_allow_html=True)
101
+ st.markdown(f'<div class="chat-bubble-bot">πŸ§‘β€πŸ« <strong>Mentor:</strong> {bot}</div>', unsafe_allow_html=True)
102
+ st.markdown('</div>', unsafe_allow_html=True)