Update app.py
Browse files54 unmodified lines5556575859606114 unmodified lines76777879808182838485865 unmodified lines929394959697989910010110210310410510610710810911011111211354 unmodified lineslogger.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}")return response.json()# Chat interfacest.title("🤖 DeepSeek Chatbot")14 unmodified linestry:with st.spinner("Generating response..."):# Prepare the payload for the APIpayload = {"inputs": {"system_message": system_message,"user_message": prompt},"parameters": {"max_new_tokens": max_tokens,"temperature": temperature,5 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 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})else:logger.error(f"Unexpected API response: {output}")st.error("Error: Unable to generate a response. Please try again.")except Exception as e:logger.error(f"Application Error: {str(e)}", exc_info=True)54 unmodified lines555657585960616263646514 unmodified lines808182838485868788895 unmodified lines959697989910010110210310410510610710810911011111211311411511611711811912054 unmodified lineslogger.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()except requests.exceptions.JSONDecodeError:logger.error(f"Failed to decode JSON response: {response.text}")return None# Chat interfacest.title("🤖 DeepSeek Chatbot")14 unmodified linestry:with st.spinner("Generating response..."):# Prepare the payload for the API# Combine system message and user input into a single promptfull_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"payload = {"inputs": full_prompt,"parameters": {"max_new_tokens": max_tokens,"temperature": temperature,5 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.")else: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