Spaces:
Running
Running
import streamlit as st | |
import requests | |
# Third-party API endpoint and headers | |
API_KEY = st.secrets["API_KEY"] | |
API_URL = "https://api.hyperbolic.xyz/v1/chat/completions" | |
API_HEADERS = { | |
"Content-Type": "application/json", | |
"Authorization": "Bearer {API_KEY}" | |
} | |
# 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): | |
# Define the conversation history | |
messages = [ | |
{"role": "system", "content": BOT_PERSONA}, | |
{"role": "user", "content": user_input} | |
] | |
# Prepare the request payload | |
data = { | |
"messages": messages, | |
"model": "deepseek-ai/DeepSeek-V3", # Model specified by the third-party API | |
"max_tokens": 512, # Maximum number of tokens in the response | |
"temperature": 0.1, # Controls randomness (lower = more deterministic) | |
"top_p": 0.9 # Controls diversity (higher = more diverse) | |
} | |
try: | |
# Send the request to the third-party API | |
response = requests.post(API_URL, headers=API_HEADERS, json=data) | |
response_data = response.json() | |
# Print the API response and status code for debugging | |
print("API Response:", response_data) | |
print("Status Code:", response.status_code) | |
# Extract the bot's reply | |
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)}" | |
# Streamlit app | |
def main(): | |
st.title("Nietzsche dihidupkan oleh HERTOG akan jawab dari neraka") | |
st.markdown("Tanya 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 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}") | |
# Use st.markdown with HTML to style the bot's response | |
st.markdown( | |
f""" | |
<div style=" | |
font-size: 16px; | |
font-family: Arial, sans-serif; | |
color: #000000; /* black font color */ | |
"> | |
<strong>Nietzsche:</strong> {bot_response} | |
</div> | |
""", | |
unsafe_allow_html=True | |
) | |
# User input | |
user_input = st.text_input("Your Question", placeholder="Ask Nietzsche...", key="user_input") | |
# 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() |