|
import streamlit as st |
|
import requests |
|
import os |
|
|
|
|
|
if 'chat_history' not in st.session_state: |
|
st.session_state.chat_history = [] |
|
|
|
st.title("DeepSeek-V3 Chatbot") |
|
|
|
|
|
for message in st.session_state.chat_history: |
|
st.write(f"**{message['role'].capitalize()}:** {message['content']}") |
|
|
|
|
|
user_input = st.text_input("You:", key="user_input") |
|
|
|
if st.button("Send"): |
|
if user_input: |
|
|
|
st.session_state.chat_history.append({"role": "user", "content": user_input}) |
|
|
|
|
|
payload = { |
|
"inputs": { |
|
"past_user_inputs": [msg['content'] for msg in st.session_state.chat_history if msg['role'] == 'user'], |
|
"generated_responses": [msg['content'] for msg in st.session_state.chat_history if msg['role'] == 'assistant'], |
|
"text": user_input |
|
} |
|
} |
|
|
|
|
|
API_TOKEN = os.getenv("API_TOKEN") |
|
if not API_TOKEN: |
|
st.error("API_TOKEN not found. Please set it as an environment variable.") |
|
else: |
|
api_url = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-V3" |
|
headers = {"Authorization": f"Bearer {API_TOKEN}"} |
|
|
|
|
|
response = requests.post(api_url, headers=headers, json=payload) |
|
|
|
if response.status_code == 200: |
|
assistant_reply = response.json().get('generated_text', '') |
|
|
|
st.session_state.chat_history.append({"role": "assistant", "content": assistant_reply}) |
|
st.write(f"**Assistant:** {assistant_reply}") |
|
else: |
|
st.error("Error: Unable to get response from DeepSeek-V3 model.") |
|
else: |
|
st.warning("Please enter a message.") |
|
|
|
if st.button("Clear Chat"): |
|
st.session_state.chat_history = [] |