Update app.py
Browse files94 unmodified lines95969798991001011021031041051061071081091101111121131143 unmodified lines11811912012194 unmodified lines# Dynamically construct the API URL based on the selected modelapi_url = f"https://api-inference.huggingface.co/models/{selected_model}"logger.info(f"Selected model: {selected_model}, API URL: {api_url}")print("payload",payload)# Query the Hugging Face API using the selected modeloutput = query(payload, api_url)# Handle API responseif output is not None and isinstance(output, list) and len(output) > 0:if 'generated_text' in output[0]:assistant_response = output[0]['generated_text']logger.info(f"Generated response: {assistant_response}")with st.chat_message("assistant"):st.markdown(assistant_response)st.session_state.messages.append({"role": "assistant", "content": assistant_response})else:logger.error(f"Unexpected API response structure: {output}")st.error("Error: Unexpected response from the model. Please try again.")3 unmodified linesexcept Exception as e:logger.error(f"Application Error: {str(e)}", exc_info=True)st.error(f"Application Error: {str(e)}")94 unmodified lines95969798991001011021031041051061071081091101111121131141151161171181191201213 unmodified lines12512612712894 unmodified lines# Dynamically construct the API URL based on the selected modelapi_url = f"https://api-inference.huggingface.co/models/{selected_model}"logger.info(f"Selected model: {selected_model}, API URL: {api_url}")# Query the Hugging Face API using the selected modeloutput = query(payload, api_url)# Handle API responseif output is not None and isinstance(output, list) and len(output) > 0:if 'generated_text' in output[0]:# Extract the assistant's responseassistant_response = output[0]['generated_text'].strip()# Check for and remove duplicate responsesresponses = assistant_response.split("\n</think>\n")unique_response = responses[0].strip()logger.info(f"Generated response: {unique_response}")# Append response to chat only oncewith st.chat_message("assistant"):st.markdown(unique_response)st.session_state.messages.append({"role": "assistant", "content": unique_response})else:logger.error(f"Unexpected API response structure: {output}")st.error("Error: Unexpected response from the model. Please try again.")3 unmodified linesexcept Exception as e:logger.error(f"Application Error: {str(e)}", exc_info=True)st.error(f"Application Error: {str(e)}")
app.py+13-6