NassimeBejaia commited on
Commit
1c06c0b
·
verified ·
1 Parent(s): 96a6ad0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -11
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 for chat history
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("Ask something about the corrected sentence:", key="chat_input")
48
- if st.button("Send"):
49
- if chat_input:
 
 
 
 
 
 
 
 
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: '{chat_input}'. Respond naturally."
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", chat_input))
64
  st.session_state.chat_history.append(("LLM", response.strip()))
65
- # Rerun to refresh the display
66
- st.rerun()
67
  except Exception as e:
68
  st.error(f"Error in chat: {str(e)}")
69
- else:
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!")