File size: 2,111 Bytes
4b3d161
c693557
ae34c03
c693557
a0231a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a81bc79
a0231a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import streamlit as st
import requests
import os

# Initialize chat history in session state
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []

st.title("DeepSeek-V3 Chatbot")

# Display chat messages from history
for message in st.session_state.chat_history:
    st.write(f"**{message['role'].capitalize()}:** {message['content']}")

# Input for user message
user_input = st.text_input("You:", key="user_input")

if st.button("Send"):
    if user_input:
        # Append user message to chat history
        st.session_state.chat_history.append({"role": "user", "content": user_input})

        # Prepare payload for Hugging Face Inference API
        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
            }
        }

        # Fetch API token from environment variables
        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}"}

            # Send request to Hugging Face Inference API
            response = requests.post(api_url, headers=headers, json=payload)

            if response.status_code == 200:
                assistant_reply = response.json().get('generated_text', '')
                # Append assistant's reply to chat history
                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 = []