Spaces:
Sleeping
Sleeping
import streamlit as st | |
import requests | |
import os | |
# Get DeepSeek API key from Space secrets | |
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY") | |
st.write("API Key:", DEEPSEEK_API_KEY[:8] if DEEPSEEK_API_KEY else "Not Found") | |
# API endpoint for DeepSeek | |
#DEEPSEEK_API_URL = "https://api.deepseek.com/v1/completions" | |
# API endpoint for DeepSeek | |
DEEPSEEK_API_URL = "https://api.deepseek.com/chat/completions" | |
#DEEPSEEK_API_URL = "https://api.deepseek.com/v1/completions" | |
# Parse as response.json()["choices"][0]["text"] | |
HEADERS = {"Authorization": f"Bearer {DEEPSEEK_API_KEY}", "Content-Type": "application/json"} | |
# Initialize session state | |
if "chat_history" not in st.session_state: | |
st.session_state.chat_history = [] | |
if "corrected_sentence" not in st.session_state: | |
st.session_state.corrected_sentence = "" | |
# Title of the app | |
st.title("Sentence Improver & Chat with DeepSeek") | |
# --- Sentence Correction Section --- | |
st.subheader("Improve a Sentence") | |
user_input = st.text_input("Enter a sentence to improve:", "I goed to the park and play.") | |
if st.button("Improve Sentence"): | |
if user_input: | |
prompt = f"Correct and improve this sentence: '{user_input}'" | |
payload = { | |
"model": "deepseek-chat", # Adjust if you have a specific DeepSeek model in mind | |
"prompt": prompt, | |
"max_tokens": 100, | |
"temperature": 0.7 | |
} | |
try: | |
response = requests.post(DEEPSEEK_API_URL, headers=HEADERS, json=payload) | |
response.raise_for_status() # Check for HTTP errors | |
st.session_state.corrected_sentence = response.json()["choices"][0]["text"].strip() | |
st.success(f"Improved Sentence: {st.session_state.corrected_sentence}") | |
except Exception as e: | |
st.error(f"Error: {str(e)}") | |
else: | |
st.warning("Please enter a sentence first!") | |
# --- Chat Section --- | |
st.subheader("Chat About the Corrected Sentence") | |
if st.session_state.corrected_sentence: | |
# Chat history container with scrollbar | |
chat_container = st.container(height=300) # Fixed height with scroll | |
with chat_container: | |
for speaker, message in st.session_state.chat_history: | |
if speaker == "You": | |
st.markdown( | |
f"<div style='text-align: right; margin: 5px;'><span style='background-color: #DCF8C6; padding: 8px; border-radius: 10px;'>{message}</span></div>", | |
unsafe_allow_html=True | |
) | |
else: # LLM | |
st.markdown( | |
f"<div style='text-align: left; margin: 5px;'><span style='background-color: #ECECEC; padding: 8px; border-radius: 10px;'>{message}</span></div>", | |
unsafe_allow_html=True | |
) | |
# Chat input with Enter submission | |
chat_input = st.text_input( | |
"Ask something about the corrected sentence (press Enter to send) ➡️", | |
key="chat_input", | |
value="", | |
on_change=lambda: submit_chat(), | |
) | |
# Function to handle chat submission | |
def submit_chat(): | |
chat_text = st.session_state.chat_input | |
if chat_text: | |
prompt = ( | |
f"The corrected sentence is: '{st.session_state.corrected_sentence}'. " | |
f"User asks: '{chat_text}'. Respond naturally." | |
) | |
payload = { | |
"model": "deepseek-chat", | |
"prompt": prompt, | |
"max_tokens": 100, | |
"temperature": 0.7 | |
} | |
try: | |
response = requests.post(DEEPSEEK_API_URL, headers=HEADERS, json=payload) | |
response.raise_for_status() | |
llm_response = response.json()["choices"][0]["text"].strip() | |
# Add to chat history | |
st.session_state.chat_history.append(("You", chat_text)) | |
st.session_state.chat_history.append(("LLM", llm_response)) | |
# Clear input | |
st.session_state.chat_input = "" | |
except Exception as e: | |
st.error(f"Error in chat: {str(e)}") | |
else: | |
st.write("Improve a sentence first to start chatting!") | |
# Optional: Add a clear chat button | |
if st.button("Clear Chat"): | |
st.session_state.chat_history = [] | |
st.rerun() |