Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -107,7 +107,62 @@ elif st.session_state.page == "python":
|
|
| 107 |
|
| 108 |
elif st.session_state.page == "sql":
|
| 109 |
st.title("SQL Chatbot 🛢️")
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
# Load SQL chatbot here
|
| 112 |
|
| 113 |
elif st.session_state.page == "powerbi":
|
|
|
|
| 107 |
|
| 108 |
elif st.session_state.page == "sql":
|
| 109 |
st.title("SQL Chatbot 🛢️")
|
| 110 |
+
if not hf_token:
|
| 111 |
+
st.error("Please add your Hugging Face API token as an environment variable.")
|
| 112 |
+
st.stop()
|
| 113 |
+
|
| 114 |
+
# Initialize the LLaMA model from HuggingFace (via Nebius provider)
|
| 115 |
+
llama_model = HuggingFaceEndpoint(
|
| 116 |
+
repo_id="meta-llama/Llama-3.1-8B-Instruct",
|
| 117 |
+
provider="nebius",
|
| 118 |
+
temperature=0.7,
|
| 119 |
+
max_new_tokens=512,
|
| 120 |
+
task="conversational",
|
| 121 |
+
huggingfacehub_api_token=hf_token,
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
llama = ChatHuggingFace(
|
| 125 |
+
llm=llama_model,
|
| 126 |
+
repo_id="meta-llama/Llama-3.1-8B-Instruct",
|
| 127 |
+
provider="nebius",
|
| 128 |
+
temperature=0.7,
|
| 129 |
+
max_new_tokens=512,
|
| 130 |
+
task="conversational"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
# Streamlit A
|
| 134 |
+
|
| 135 |
+
st.markdown("Ask anything related to SQL interviews!")
|
| 136 |
+
|
| 137 |
+
# Initialize chat history
|
| 138 |
+
if "messages" not in st.session_state:
|
| 139 |
+
st.session_state.messages = [
|
| 140 |
+
SystemMessage(content="Answer clearly like a technical SQL interviewer.")
|
| 141 |
+
]
|
| 142 |
+
|
| 143 |
+
# User input
|
| 144 |
+
user_input = st.text_input("💡 Ask your SQL interview question:", placeholder="e.g., give me 10 SQL interview questions with answers")
|
| 145 |
+
|
| 146 |
+
def generate_response(user_input):
|
| 147 |
+
st.session_state.messages.append(HumanMessage(content=user_input))
|
| 148 |
+
response = llama.invoke(st.session_state.messages)
|
| 149 |
+
st.session_state.messages.append(AIMessage(content=response))
|
| 150 |
+
return response
|
| 151 |
+
|
| 152 |
+
# Display response
|
| 153 |
+
if user_input:
|
| 154 |
+
with st.spinner("Thinking..."):
|
| 155 |
+
answer = generate_response(user_input)
|
| 156 |
+
st.markdown(f"**Answer:** {answer}")
|
| 157 |
+
|
| 158 |
+
# Show chat history
|
| 159 |
+
st.markdown("### 📜 Chat History")
|
| 160 |
+
for msg in st.session_state.messages[1:]: # Skip SystemMessage
|
| 161 |
+
if isinstance(msg, HumanMessage):
|
| 162 |
+
st.markdown(f"**You:** {msg.content}")
|
| 163 |
+
elif isinstance(msg, AIMessage):
|
| 164 |
+
st.markdown(f"**Bot:** {msg.content}")
|
| 165 |
+
st.button("⬅️ Back to Home", on_click=lambda: switch_page("home"))
|
| 166 |
# Load SQL chatbot here
|
| 167 |
|
| 168 |
elif st.session_state.page == "powerbi":
|