ruslanmv commited on
Commit
2e44d20
·
verified ·
1 Parent(s): be8fcde

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -95,20 +95,27 @@ if prompt := st.chat_input("Type your message..."):
95
  # Dynamically construct the API URL based on the selected model
96
  api_url = f"https://api-inference.huggingface.co/models/{selected_model}"
97
  logger.info(f"Selected model: {selected_model}, API URL: {api_url}")
98
- print("payload",payload)
99
  # Query the Hugging Face API using the selected model
100
  output = query(payload, api_url)
101
 
102
  # Handle API response
103
  if output is not None and isinstance(output, list) and len(output) > 0:
104
  if 'generated_text' in output[0]:
105
- assistant_response = output[0]['generated_text']
106
- logger.info(f"Generated response: {assistant_response}")
 
 
 
 
 
 
107
 
 
108
  with st.chat_message("assistant"):
109
- st.markdown(assistant_response)
110
 
111
- st.session_state.messages.append({"role": "assistant", "content": assistant_response})
112
  else:
113
  logger.error(f"Unexpected API response structure: {output}")
114
  st.error("Error: Unexpected response from the model. Please try again.")
@@ -118,4 +125,4 @@ if prompt := st.chat_input("Type your message..."):
118
 
119
  except Exception as e:
120
  logger.error(f"Application Error: {str(e)}", exc_info=True)
121
- st.error(f"Application Error: {str(e)}")
 
95
  # Dynamically construct the API URL based on the selected model
96
  api_url = f"https://api-inference.huggingface.co/models/{selected_model}"
97
  logger.info(f"Selected model: {selected_model}, API URL: {api_url}")
98
+
99
  # Query the Hugging Face API using the selected model
100
  output = query(payload, api_url)
101
 
102
  # Handle API response
103
  if output is not None and isinstance(output, list) and len(output) > 0:
104
  if 'generated_text' in output[0]:
105
+ # Extract the assistant's response
106
+ assistant_response = output[0]['generated_text'].strip()
107
+
108
+ # Check for and remove duplicate responses
109
+ responses = assistant_response.split("\n</think>\n")
110
+ unique_response = responses[0].strip()
111
+
112
+ logger.info(f"Generated response: {unique_response}")
113
 
114
+ # Append response to chat only once
115
  with st.chat_message("assistant"):
116
+ st.markdown(unique_response)
117
 
118
+ st.session_state.messages.append({"role": "assistant", "content": unique_response})
119
  else:
120
  logger.error(f"Unexpected API response structure: {output}")
121
  st.error("Error: Unexpected response from the model. Please try again.")
 
125
 
126
  except Exception as e:
127
  logger.error(f"Application Error: {str(e)}", exc_info=True)
128
+ st.error(f"Application Error: {str(e)}")