Spaces:
Running
Running
import streamlit as st | |
import requests | |
# Set up Streamlit page configuration | |
st.set_page_config(page_title="DeepSeek Chatbot", page_icon="🤖", layout="wide") | |
# API setup | |
url = "https://api.hyperbolic.xyz/v1/chat/completions" | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtaXRyYWxlc3RhcmlwZXJzYWRhQGdtYWlsLmNvbSIsImlhdCI6MTczNjUwMzQxMX0.yuIoZsH1jouAlixx_h_eQ-bltZ1sg4alrJHMHr1axvA" | |
} | |
# Chat history container | |
if 'messages' not in st.session_state: | |
st.session_state.messages = [] | |
# Function to send message and get response | |
def get_response(user_input): | |
data = { | |
"messages": [{"role": "user", "content": user_input}], | |
"model": "deepseek-ai/DeepSeek-V3", | |
"max_tokens": 512, | |
"temperature": 0.1, | |
"top_p": 0.9 | |
} | |
response = requests.post(url, headers=headers, json=data) | |
return response.json() | |
# Streamlit chat UI | |
st.title("DeepSeek AI Chatbot") | |
# Display the chat history | |
for message in st.session_state.messages: | |
if message["role"] == "user": | |
st.chat_message("user").markdown(message["content"]) | |
else: | |
st.chat_message("assistant").markdown(message["content"]) | |
# Accept user input | |
user_input = st.text_input("You: ", "") | |
# Handle user input and update the chat | |
if user_input: | |
st.session_state.messages.append({"role": "user", "content": user_input}) | |
response = get_response(user_input) | |
# Assuming the response is in the 'choices' field of the API response | |
bot_response = response.get('choices', [{}])[0].get('message', {}).get('content', 'Sorry, I did not understand that.') | |
st.session_state.messages.append({"role": "assistant", "content": bot_response}) | |
st.experimental_rerun() | |