Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,7 @@ import os
|
|
6 |
API_TOKEN = os.getenv("HF_TOKEN")
|
7 |
client = InferenceClient(token=API_TOKEN)
|
8 |
|
9 |
-
# Initialize session state
|
10 |
if "chat_history" not in st.session_state:
|
11 |
st.session_state.chat_history = []
|
12 |
if "corrected_sentence" not in st.session_state:
|
@@ -43,14 +43,22 @@ if st.session_state.corrected_sentence:
|
|
43 |
for speaker, message in st.session_state.chat_history:
|
44 |
st.write(f"**{speaker}:** {message}")
|
45 |
|
46 |
-
# Chat input
|
47 |
-
chat_input = st.text_input(
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
# Build prompt with corrected sentence as context
|
51 |
prompt = (
|
52 |
f"The corrected sentence is: '{st.session_state.corrected_sentence}'. "
|
53 |
-
f"User asks: '{
|
54 |
)
|
55 |
try:
|
56 |
response = client.text_generation(
|
@@ -60,13 +68,12 @@ if st.session_state.corrected_sentence:
|
|
60 |
temperature=0.7,
|
61 |
)
|
62 |
# Add to chat history
|
63 |
-
st.session_state.chat_history.append(("You",
|
64 |
st.session_state.chat_history.append(("LLM", response.strip()))
|
65 |
-
#
|
66 |
-
st.
|
67 |
except Exception as e:
|
68 |
st.error(f"Error in chat: {str(e)}")
|
69 |
-
|
70 |
-
st.warning("Please enter a question!")
|
71 |
else:
|
72 |
st.write("Improve a sentence first to start chatting!")
|
|
|
6 |
API_TOKEN = os.getenv("HF_TOKEN")
|
7 |
client = InferenceClient(token=API_TOKEN)
|
8 |
|
9 |
+
# Initialize session state
|
10 |
if "chat_history" not in st.session_state:
|
11 |
st.session_state.chat_history = []
|
12 |
if "corrected_sentence" not in st.session_state:
|
|
|
43 |
for speaker, message in st.session_state.chat_history:
|
44 |
st.write(f"**{speaker}:** {message}")
|
45 |
|
46 |
+
# Chat input with Enter key submission
|
47 |
+
chat_input = st.text_input(
|
48 |
+
"Ask something about the corrected sentence (press Enter to send):",
|
49 |
+
key="chat_input",
|
50 |
+
value="", # Clear after each submission
|
51 |
+
on_change=lambda: submit_chat(), # Trigger on Enter
|
52 |
+
)
|
53 |
+
|
54 |
+
# Function to handle chat submission
|
55 |
+
def submit_chat():
|
56 |
+
chat_text = st.session_state.chat_input
|
57 |
+
if chat_text:
|
58 |
# Build prompt with corrected sentence as context
|
59 |
prompt = (
|
60 |
f"The corrected sentence is: '{st.session_state.corrected_sentence}'. "
|
61 |
+
f"User asks: '{chat_text}'. Respond naturally."
|
62 |
)
|
63 |
try:
|
64 |
response = client.text_generation(
|
|
|
68 |
temperature=0.7,
|
69 |
)
|
70 |
# Add to chat history
|
71 |
+
st.session_state.chat_history.append(("You", chat_text))
|
72 |
st.session_state.chat_history.append(("LLM", response.strip()))
|
73 |
+
# Clear the input field by resetting the key
|
74 |
+
st.session_state.chat_input = ""
|
75 |
except Exception as e:
|
76 |
st.error(f"Error in chat: {str(e)}")
|
77 |
+
|
|
|
78 |
else:
|
79 |
st.write("Improve a sentence first to start chatting!")
|