Spaces:
Sleeping
Sleeping
Create Next.js Q/A Chatbot with Deepseek V3 integration
Browse files- src/streamlit_app.py +155 -34
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,161 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Set up the page config
|
| 6 |
+
st.set_page_config(
|
| 7 |
+
page_title="Next.js Q/A Chatbot",
|
| 8 |
+
page_icon="⚛️",
|
| 9 |
+
layout="centered",
|
| 10 |
+
initial_sidebar_state="expanded"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Initialize the Hugging Face Inference Client
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def get_inference_client():
|
| 16 |
+
# Using deepseek-ai/DeepSeek-V3 as the model
|
| 17 |
+
return InferenceClient(model="deepseek-ai/DeepSeek-V3")
|
| 18 |
+
|
| 19 |
+
client = get_inference_client()
|
| 20 |
+
|
| 21 |
+
# App title and description
|
| 22 |
+
st.title("⚛️ Next.js Q/A Chatbot")
|
| 23 |
+
st.markdown(
|
| 24 |
+
"""
|
| 25 |
+
Welcome to the **Next.js Q/A Chatbot**! Ask me anything about learning Next.js,
|
| 26 |
+
from basic concepts to advanced topics. I'm powered by **Deepseek V3** to provide
|
| 27 |
+
you with helpful, accurate answers about Next.js development.
|
| 28 |
+
"""
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Sidebar with information
|
| 32 |
+
with st.sidebar:
|
| 33 |
+
st.header("About This Chatbot")
|
| 34 |
+
st.info(
|
| 35 |
+
"""
|
| 36 |
+
This chatbot specializes in answering questions about **Next.js**,
|
| 37 |
+
the React framework for production applications.
|
| 38 |
+
|
| 39 |
+
**Topics I can help with:**
|
| 40 |
+
- Getting started with Next.js
|
| 41 |
+
- Pages and routing
|
| 42 |
+
- API routes
|
| 43 |
+
- Static generation & server-side rendering
|
| 44 |
+
- Styling and CSS
|
| 45 |
+
- Deployment
|
| 46 |
+
- Performance optimization
|
| 47 |
+
- And much more!
|
| 48 |
+
"""
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
st.header("Example Questions")
|
| 52 |
+
example_questions = [
|
| 53 |
+
"How do I create a new Next.js project?",
|
| 54 |
+
"What's the difference between SSG and SSR?",
|
| 55 |
+
"How do I add CSS to my Next.js app?",
|
| 56 |
+
"How do I create API routes in Next.js?",
|
| 57 |
+
"How do I deploy my Next.js app?"
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
for question in example_questions:
|
| 61 |
+
if st.button(question, key=question):
|
| 62 |
+
st.session_state.user_input = question
|
| 63 |
|
| 64 |
+
# Initialize chat history
|
| 65 |
+
if "messages" not in st.session_state:
|
| 66 |
+
st.session_state.messages = [
|
| 67 |
+
{
|
| 68 |
+
"role": "assistant",
|
| 69 |
+
"content": "Hi! I'm your Next.js Q/A assistant. I'm here to help you learn Next.js! What would you like to know?"
|
| 70 |
+
}
|
| 71 |
+
]
|
| 72 |
|
| 73 |
+
# Display chat messages
|
| 74 |
+
for message in st.session_state.messages:
|
| 75 |
+
with st.chat_message(message["role"]):
|
| 76 |
+
st.markdown(message["content"])
|
| 77 |
+
|
| 78 |
+
# Get user input
|
| 79 |
+
if prompt := st.chat_input("Ask me anything about Next.js..."):
|
| 80 |
+
# Add user message to chat history
|
| 81 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 82 |
+
|
| 83 |
+
# Display user message
|
| 84 |
+
with st.chat_message("user"):
|
| 85 |
+
st.markdown(prompt)
|
| 86 |
+
|
| 87 |
+
# Generate assistant response
|
| 88 |
+
with st.chat_message("assistant"):
|
| 89 |
+
message_placeholder = st.empty()
|
| 90 |
+
|
| 91 |
+
# Create a context-aware prompt for Next.js questions
|
| 92 |
+
system_prompt = """
|
| 93 |
+
You are a helpful Next.js expert assistant. You specialize in helping developers learn and use Next.js effectively.
|
| 94 |
+
Provide accurate, helpful, and practical answers about Next.js development.
|
| 95 |
+
When possible, include code examples and best practices.
|
| 96 |
+
Focus specifically on Next.js topics including:
|
| 97 |
+
- Project setup and configuration
|
| 98 |
+
- Pages, routing, and navigation
|
| 99 |
+
- API routes and server-side functionality
|
| 100 |
+
- Static Site Generation (SSG) and Server-Side Rendering (SSR)
|
| 101 |
+
- Styling (CSS Modules, Tailwind CSS, etc.)
|
| 102 |
+
- Performance optimization
|
| 103 |
+
- Deployment strategies
|
| 104 |
+
- Integration with other tools and libraries
|
| 105 |
+
|
| 106 |
+
Keep your answers clear, concise, and beginner-friendly while being technically accurate.
|
| 107 |
"""
|
| 108 |
+
|
| 109 |
+
full_prompt = f"{system_prompt}\n\nUser question: {prompt}\n\nAssistant:"
|
| 110 |
+
|
| 111 |
+
try:
|
| 112 |
+
# Generate response using Hugging Face Inference API
|
| 113 |
+
response = client.text_generation(
|
| 114 |
+
full_prompt,
|
| 115 |
+
max_new_tokens=500,
|
| 116 |
+
temperature=0.7,
|
| 117 |
+
do_sample=True,
|
| 118 |
+
stop_sequences=["\n\nUser:", "User question:"]
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
assistant_response = response.strip()
|
| 122 |
+
message_placeholder.markdown(assistant_response)
|
| 123 |
+
|
| 124 |
+
# Add assistant response to chat history
|
| 125 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
| 126 |
+
|
| 127 |
+
except Exception as e:
|
| 128 |
+
error_message = f"Sorry, I encountered an error: {str(e)}. Please try again!"
|
| 129 |
+
message_placeholder.markdown(error_message)
|
| 130 |
+
st.session_state.messages.append({"role": "assistant", "content": error_message})
|
| 131 |
+
|
| 132 |
+
# Handle example questions from sidebar
|
| 133 |
+
if "user_input" in st.session_state:
|
| 134 |
+
user_question = st.session_state.user_input
|
| 135 |
+
del st.session_state.user_input
|
| 136 |
+
|
| 137 |
+
# Add user message to chat history
|
| 138 |
+
st.session_state.messages.append({"role": "user", "content": user_question})
|
| 139 |
+
|
| 140 |
+
# Rerun to process the question
|
| 141 |
+
st.rerun()
|
| 142 |
+
|
| 143 |
+
# Clear chat button
|
| 144 |
+
if st.button("🗑️ Clear Chat History"):
|
| 145 |
+
st.session_state.messages = [
|
| 146 |
+
{
|
| 147 |
+
"role": "assistant",
|
| 148 |
+
"content": "Hi! I'm your Next.js Q/A assistant. I'm here to help you learn Next.js! What would you like to know?"
|
| 149 |
+
}
|
| 150 |
+
]
|
| 151 |
+
st.rerun()
|
| 152 |
|
| 153 |
+
# Footer
|
| 154 |
+
st.markdown(
|
| 155 |
+
"""
|
| 156 |
+
---
|
| 157 |
+
|
| 158 |
+
**Note:** This chatbot uses Deepseek V3 via Hugging Face Inference API to provide Next.js assistance.
|
| 159 |
+
For the most up-to-date information, always refer to the [official Next.js documentation](https://nextjs.org/docs).
|
| 160 |
+
"""
|
| 161 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|