Update app.py
Browse files54 unmodified lines55565758585960616263646514 unmodified lines80818283848580818283868788895 unmodified lines95969795989910010110210310010110210310410510610710410510610710810911011111211311411510911011611711811912054 unmodified lines logger.info(f"Sending request to {api_url} with payload: {payload}") response = requests.post(api_url, headers=headers, json=payload) logger.info(f"Received response: {response.status_code}, {response.text}") try: return response.json() return response.json() except requests.exceptions.JSONDecodeError: logger.error(f"Failed to decode JSON response: {response.text}") return None
# Chat interfacest.title("🤖 DeepSeek Chatbot")14 unmodified lines try: with st.spinner("Generating response..."): # Prepare the payload for the API # Combine system message and user input into a single prompt full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:" payload = { "inputs": { "system_message": system_message, "user_message": prompt }, "inputs": full_prompt, "parameters": { "max_new_tokens": max_tokens, "temperature": temperature,5 unmodified lines # Dynamically construct the API URL based on the selected model api_url = f"https://api-inference.huggingface.co/models/{selected_model}" logger.info(f"Selected model: {selected_model}, API URL: {api_url}") print("payload:",payload) print("payload",payload) # Query the Hugging Face API using the selected model output = query(payload, api_url)
# Handle API response if output is not None and isinstance(output, list) and len(output) > 0: if isinstance(output, list) and len(output) > 0 and '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}) 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.") else: logger.error(f"Unexpected API response: {output}") st.error("Error: Unable to generate a response. Please try again.") logger.error(f"Empty or invalid API response: {output}") st.error("Error: Unable to generate a response. Please check the model and try again.")
except Exception as e: logger.error(f"Application Error: {str(e)}", exc_info=True)app.py+23-16