File size: 2,393 Bytes
4b3d161
c693557
 
 
eda21ce
c693557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b3d161
 
 
 
 
 
 
 
 
 
 
9d0f2b2
 
4b3d161
5cc3227
 
 
 
 
 
 
 
7eb4b57
5cc3227
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import streamlit as st
import requests

# Replace with your DeepSeek API key and endpoint
DEEPSEEK_API_KEY = "sk-cb27c81768e443868a194fad0bb91abc"
DEEPSEEK_ENDPOINT = "https://api.deepseek.com/v1/chat"

# Define the bot's persona
BOT_PERSONA = (
    "You are Friedrich Nietzsche, the philosopher. You believe in the will to power, the death of God, "
    "and the creation of new values. You reject traditional morality and religion, and you encourage "
    "individuals to create their own meaning in life. Respond to all questions from this perspective."
)

def chat_with_nietzsche(user_input):
    headers = {
        "Authorization": f"Bearer {DEEPSEEK_API_KEY}",
        "Content-Type": "application/json"
    }

    # Define the conversation history
    messages = [
        {"role": "system", "content": BOT_PERSONA},
        {"role": "user", "content": user_input}
    ]

    # Send the request to the DeepSeek API
    data = {
        "model": "deepseek-chat",  # Replace with the appropriate model
        "messages": messages
    }

    response = requests.post(DEEPSEEK_ENDPOINT, headers=headers, json=data)
    response_data = response.json()

    # Extract the bot's reply
    if response.status_code == 200:
        return response_data["choices"][0]["message"]["content"]
    else:
        return "Error: Unable to get a response from the bot."

# Streamlit app
def main():
    st.title("Nietzsche Chatbot")
    st.markdown("Ask Friedrich Nietzsche anything, and he will respond from his philosophical perspective.")

    # Initialize session state for conversation history
    if "history" not in st.session_state:
        st.session_state.history = []

    # Display conversation history
    for user_input, bot_response in st.session_state.history:
        st.text_area("You", value=user_input, height=68, disabled=True)
        st.text_area("Nietzsche", value=bot_response, height=150, disabled=True)

    # User input
    user_input = st.text_input("Your Question", placeholder="Ask Nietzsche...")

    # Submit button
    if st.button("Submit"):
        if user_input.strip():  # Check if input is not empty
            bot_response = chat_with_nietzsche(user_input)
            st.session_state.history.append((user_input, bot_response))
            st.rerun()  # Refresh the app to display the new response

# Run the Streamlit app
if __name__ == "__main__":
    main()