Spaces:
Sleeping
Sleeping
File size: 4,224 Bytes
0068791 926e7f2 0068791 926e7f2 f975d03 926e7f2 |
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
import streamlit as st
import requests
import os
# Load your OpenRouter API key
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY") or st.secrets.get("OPENROUTER_API_KEY", "")
MODEL_NAME = "deepseek/deepseek-r1-0528:free"
# App title
st.title("WhatsApp-like Chat with DeepSeek-R1")
# Custom CSS
st.markdown("""
<style>
.stChatMessage { background-color: transparent !important; padding: 0 !important; width: 100%; }
.user-container, .assistant-container { display: flex; flex-direction: column; margin-bottom: 12px; }
.user-container { align-items: flex-end; }
.assistant-container { align-items: flex-start; }
.user-message, .assistant-message {
padding: 8px 12px; border-radius: 15px; max-width: 80%; margin-top: 4px;
}
.user-message { background-color: #DCF8C6; border-radius: 15px 15px 0 15px; }
.assistant-message { background-color: #FFF; border: 1px solid #ECECEC; border-radius: 15px 15px 15px 0; }
.sender-label { font-weight: bold; font-size: 0.8em; color: #555; }
.user-label { margin-right: 8px; }
.assistant-label { margin-left: 8px; }
[data-testid="stChatMessage"] { background-color: transparent !important; }
[data-testid="stChatMessage"] > div:first-child { display: none !important; }
</style>
""", unsafe_allow_html=True)
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = [{"role": "system", "content": "You are a helpful assistant."}]
# Display chat messages
for message in st.session_state.messages:
if message["role"] == "system":
continue
with st.chat_message(message["role"]):
if message["role"] == "user":
st.markdown(f"""
<div class="user-container">
<div class="user-label sender-label">You:</div>
<div class="user-message">{message['content']}</div>
</div>
""", unsafe_allow_html=True)
else:
st.markdown(f"""
<div class="assistant-container">
<div class="assistant-label sender-label">Bot:</div>
<div class="assistant-message">{message['content']}</div>
</div>
""", unsafe_allow_html=True)
# Input field
if prompt := st.chat_input("Type your message..."):
st.session_state.messages.append({"role": "user", "content": prompt})
# Show user message immediately
with st.chat_message("user"):
st.markdown(f"""
<div class="user-container">
<div class="user-label sender-label">Me:</div>
<div class="user-message">{prompt}</div>
</div>
""", unsafe_allow_html=True)
# Show assistant reply placeholder
with st.chat_message("assistant"):
placeholder = st.empty()
placeholder.markdown("""
<div class="assistant-container">
<div class="assistant-label sender-label">Bot:</div>
<div class="assistant-message">▌</div>
</div>
""", unsafe_allow_html=True)
# Call OpenRouter
headers = {
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
"Content-Type": "application/json",
"HTTP-Referer": "https://your-app-name.streamlit.app/",
"X-Title": "DeepSeek WhatsApp Chat"
}
body = {
"model": MODEL_NAME,
"messages": st.session_state.messages,
"stream": False # OpenRouter's free models might not support streaming
}
try:
res = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=body)
res.raise_for_status()
full_response = res.json()["choices"][0]["message"]["content"]
except Exception as e:
full_response = f"Error: {str(e)}"
# Render final response
placeholder.markdown(f"""
<div class="assistant-container">
<div class="assistant-label sender-label">Bot:</div>
<div class="assistant-message">{full_response}</div>
</div>
""", unsafe_allow_html=True)
# Save assistant reply
st.session_state.messages.append({"role": "assistant", "content": full_response})
|