Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -80,4 +80,58 @@ elif st.session_state.page == "genai":
|
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
+
def python_page():
|
| 84 |
+
st.title("Python Chatbot")
|
| 85 |
+
hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN") or os.getenv("HF_TOKEN")
|
| 86 |
+
if not hf_token:
|
| 87 |
+
st.error("Please add your Hugging Face API token to Secrets (HUGGINGFACEHUB_API_TOKEN or HF_TOKEN).")
|
| 88 |
+
st.stop()
|
| 89 |
+
|
| 90 |
+
# Setup the LangChain HuggingFaceEndpoint and ChatHuggingFace LLM
|
| 91 |
+
deep_seek_model = HuggingFaceEndpoint(
|
| 92 |
+
repo_id="deepseek-ai/DeepSeek-R1",
|
| 93 |
+
provider="nebius",
|
| 94 |
+
temperature=0.7,
|
| 95 |
+
max_new_tokens=100,
|
| 96 |
+
task="conversational",
|
| 97 |
+
huggingfacehub_api_token=hf_token,
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
deepseek = ChatHuggingFace(
|
| 101 |
+
llm=deep_seek_model,
|
| 102 |
+
repo_id="deepseek-ai/DeepSeek-R1",
|
| 103 |
+
provider="nebius",
|
| 104 |
+
temperature=0.7,
|
| 105 |
+
max_new_tokens=100,
|
| 106 |
+
task="conversational"
|
| 107 |
+
)
|
| 108 |
+
# Initialize session state for chat history
|
| 109 |
+
if "messages" not in st.session_state:
|
| 110 |
+
st.session_state.messages = [
|
| 111 |
+
SystemMessage(content="Answer like a data scientist")
|
| 112 |
+
]
|
| 113 |
+
|
| 114 |
+
def generate_response(user_input):
|
| 115 |
+
# Append user message
|
| 116 |
+
st.session_state.messages.append(HumanMessage(content=user_input))
|
| 117 |
+
# Invoke the model
|
| 118 |
+
response = deepseek.invoke(st.session_state.messages)
|
| 119 |
+
# Append AI response
|
| 120 |
+
st.session_state.messages.append(AIMessage(content=response))
|
| 121 |
+
return response
|
| 122 |
+
|
| 123 |
+
# User input
|
| 124 |
+
user_input = st.text_input("Ask a question about Data Science:")
|
| 125 |
+
|
| 126 |
+
if user_input:
|
| 127 |
+
with st.spinner("Getting answer..."):
|
| 128 |
+
answer = generate_response(user_input)
|
| 129 |
+
st.markdown(f"**Answer:** {answer}")
|
| 130 |
+
|
| 131 |
+
# Display chat history
|
| 132 |
+
if st.session_state.messages:
|
| 133 |
+
for msg in st.session_state.messages[1:]: # skip initial SystemMessage
|
| 134 |
+
if isinstance(msg, HumanMessage):
|
| 135 |
+
st.markdown(f"**You:** {msg.content}")
|
| 136 |
+
elif isinstance(msg, AIMessage):
|
| 137 |
+
st.markdown(f"**Bot:** {msg.content}")
|