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"