Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Get DeepSeek API key from Space secrets
|
6 |
+
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
|
7 |
+
|
8 |
+
# API endpoint for DeepSeek
|
9 |
+
DEEPSEEK_API_URL = "https://api.deepseek.com/v1/completions"
|
10 |
+
HEADERS = {"Authorization": f"Bearer {DEEPSEEK_API_KEY}", "Content-Type": "application/json"}
|
11 |
+
|
12 |
+
# Initialize session state
|
13 |
+
if "chat_history" not in st.session_state:
|
14 |
+
st.session_state.chat_history = []
|
15 |
+
if "corrected_sentence" not in st.session_state:
|
16 |
+
st.session_state.corrected_sentence = ""
|
17 |
+
|
18 |
+
# Title of the app
|
19 |
+
st.title("Sentence Improver & Chat with DeepSeek")
|
20 |
+
|
21 |
+
# --- Sentence Correction Section ---
|
22 |
+
st.subheader("Improve a Sentence")
|
23 |
+
user_input = st.text_input("Enter a sentence to improve:", "I goed to the park and play.")
|
24 |
+
|
25 |
+
if st.button("Improve Sentence"):
|
26 |
+
if user_input:
|
27 |
+
prompt = f"Correct and improve this sentence: '{user_input}'"
|
28 |
+
payload = {
|
29 |
+
"model": "deepseek-coder", # Adjust if you have a specific DeepSeek model in mind
|
30 |
+
"prompt": prompt,
|
31 |
+
"max_tokens": 100,
|
32 |
+
"temperature": 0.7
|
33 |
+
}
|
34 |
+
try:
|
35 |
+
response = requests.post(DEEPSEEK_API_URL, headers=HEADERS, json=payload)
|
36 |
+
response.raise_for_status() # Check for HTTP errors
|
37 |
+
st.session_state.corrected_sentence = response.json()["choices"][0]["text"].strip()
|
38 |
+
st.success(f"Improved Sentence: {st.session_state.corrected_sentence}")
|
39 |
+
except Exception as e:
|
40 |
+
st.error(f"Error: {str(e)}")
|
41 |
+
else:
|
42 |
+
st.warning("Please enter a sentence first!")
|
43 |
+
|
44 |
+
# --- Chat Section ---
|
45 |
+
st.subheader("Chat About the Corrected Sentence")
|
46 |
+
if st.session_state.corrected_sentence:
|
47 |
+
# Chat history container with scrollbar
|
48 |
+
chat_container = st.container(height=300) # Fixed height with scroll
|
49 |
+
with chat_container:
|
50 |
+
for speaker, message in st.session_state.chat_history:
|
51 |
+
if speaker == "You":
|
52 |
+
st.markdown(
|
53 |
+
f"<div style='text-align: right; margin: 5px;'><span style='background-color: #DCF8C6; padding: 8px; border-radius: 10px;'>{message}</span></div>",
|
54 |
+
unsafe_allow_html=True
|
55 |
+
)
|
56 |
+
else: # LLM
|
57 |
+
st.markdown(
|
58 |
+
f"<div style='text-align: left; margin: 5px;'><span style='background-color: #ECECEC; padding: 8px; border-radius: 10px;'>{message}</span></div>",
|
59 |
+
unsafe_allow_html=True
|
60 |
+
)
|
61 |
+
|
62 |
+
# Chat input with Enter submission
|
63 |
+
chat_input = st.text_input(
|
64 |
+
"Ask something about the corrected sentence (press Enter to send) ➡️",
|
65 |
+
key="chat_input",
|
66 |
+
value="",
|
67 |
+
on_change=lambda: submit_chat(),
|
68 |
+
)
|
69 |
+
|
70 |
+
# Function to handle chat submission
|
71 |
+
def submit_chat():
|
72 |
+
chat_text = st.session_state.chat_input
|
73 |
+
if chat_text:
|
74 |
+
prompt = (
|
75 |
+
f"The corrected sentence is: '{st.session_state.corrected_sentence}'. "
|
76 |
+
f"User asks: '{chat_text}'. Respond naturally."
|
77 |
+
)
|
78 |
+
payload = {
|
79 |
+
"model": "deepseek-coder",
|
80 |
+
"prompt": prompt,
|
81 |
+
"max_tokens": 150,
|
82 |
+
"temperature": 0.7
|
83 |
+
}
|
84 |
+
try:
|
85 |
+
response = requests.post(DEEPSEEK_API_URL, headers=HEADERS, json=payload)
|
86 |
+
response.raise_for_status()
|
87 |
+
llm_response = response.json()["choices"][0]["text"].strip()
|
88 |
+
# Add to chat history
|
89 |
+
st.session_state.chat_history.append(("You", chat_text))
|
90 |
+
st.session_state.chat_history.append(("LLM", llm_response))
|
91 |
+
# Clear input
|
92 |
+
st.session_state.chat_input = ""
|
93 |
+
except Exception as e:
|
94 |
+
st.error(f"Error in chat: {str(e)}")
|
95 |
+
|
96 |
+
else:
|
97 |
+
st.write("Improve a sentence first to start chatting!")
|
98 |
+
|
99 |
+
# Optional: Add a clear chat button
|
100 |
+
if st.button("Clear Chat"):
|
101 |
+
st.session_state.chat_history = []
|
102 |
+
st.rerun()
|