|
import streamlit as st |
|
import requests |
|
|
|
|
|
API_URL = "https://api.hyperbolic.xyz/v1/chat/completions" |
|
API_HEADERS = { |
|
"Content-Type": "application/json", |
|
"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtaXRyYWxlc3RhcmlwZXJzYWRhQGdtYWlsLmNvbSIsImlhdCI6MTczNjUwMzQxMX0.yuIoZsH1jouAlixx_h_eQ-bltZ1sg4alrJHMHr1axvA" |
|
} |
|
|
|
|
|
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): |
|
|
|
messages = [ |
|
{"role": "system", "content": BOT_PERSONA}, |
|
{"role": "user", "content": user_input} |
|
] |
|
|
|
|
|
data = { |
|
"messages": messages, |
|
"model": "deepseek-ai/DeepSeek-V3", |
|
"max_tokens": 512, |
|
"temperature": 0.1, |
|
"top_p": 0.9 |
|
} |
|
|
|
try: |
|
|
|
response = requests.post(API_URL, headers=API_HEADERS, json=data) |
|
response_data = response.json() |
|
|
|
|
|
print("API Response:", response_data) |
|
print("Status Code:", response.status_code) |
|
|
|
|
|
if response.status_code == 200: |
|
return response_data["choices"][0]["message"]["content"] |
|
else: |
|
return f"Error: Unable to get a response from the bot. Status Code: {response.status_code}" |
|
except Exception as e: |
|
return f"Error: An exception occurred - {str(e)}" |
|
|
|
|
|
def main(): |
|
st.title("Nietzsche Chatbot") |
|
st.markdown("Ask Friedrich Nietzsche anything, and he will respond from his philosophical perspective.") |
|
|
|
|
|
if "history" not in st.session_state: |
|
st.session_state.history = [] |
|
|
|
|
|
for i, (user_input, bot_response) in enumerate(st.session_state.history): |
|
st.text_area("You", value=user_input, height=68, disabled=True, key=f"user_input_{i}") |
|
st.text_area("Nietzsche", value=bot_response, height=150, disabled=True, key=f"bot_response_{i}") |
|
|
|
|
|
user_input = st.text_input("Your Question", placeholder="Ask Nietzsche...", key="user_input") |
|
|
|
|
|
if st.button("Submit"): |
|
if user_input.strip(): |
|
bot_response = chat_with_nietzsche(user_input) |
|
st.session_state.history.append((user_input, bot_response)) |
|
st.rerun() |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |