|
import streamlit as st |
|
import requests |
|
import os |
|
|
|
|
|
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY") |
|
|
|
|
|
|
|
|
|
|
|
|
|
DEEPSEEK_API_URL = "https://api.deepseek.com/v1/completions" |
|
|
|
|
|
|
|
HEADERS = {"Authorization": f"Bearer {DEEPSEEK_API_KEY}", "Content-Type": "application/json"} |
|
|
|
|
|
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 = "" |
|
|
|
|
|
st.title("Sentence Improver & Chat with DeepSeek") |
|
|
|
|
|
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", |
|
"prompt": prompt_content, |
|
"max_tokens": 100, |
|
"temperature": 0.7 |
|
} |
|
try: |
|
response = requests.post(DEEPSEEK_API_URL, headers=HEADERS, json=payload) |
|
response.raise_for_status() |
|
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!") |
|
|
|
|
|
st.subheader("Chat About the Corrected Sentence") |
|
if st.session_state.corrected_sentence: |
|
|
|
chat_container = st.container(height=300) |
|
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: |
|
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 = st.text_input( |
|
"Ask something about the corrected sentence (press Enter to send) ➡️", |
|
key="chat_input", |
|
value="", |
|
on_change=lambda: submit_chat(), |
|
) |
|
|
|
|
|
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() |
|
|
|
st.session_state.chat_history.append(("You", chat_text)) |
|
st.session_state.chat_history.append(("LLM", llm_response)) |
|
|
|
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!") |
|
|
|
|
|
if st.button("Clear Chat"): |
|
st.session_state.chat_history = [] |
|
st.rerun() |