Spaces:
Sleeping
Sleeping
File size: 4,214 Bytes
239c6c1 f84e000 98e8e0c 20287c3 545b2d1 20287c3 f84e000 239c6c1 20287c3 239c6c1 |
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 |
import streamlit as st
import requests
import os
# Get DeepSeek API key from Space secrets
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
# 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-coder", # Adjust if you have a specific DeepSeek model in mind
"prompt": prompt_content,
"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-coder",
"prompt": prompt,
"max_tokens": 150,
"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() |