Untitled conversation
- User
['I will give you code for of a GUI for a chat application with a language model. I need you to make the last chat message to be bigger than the previous ones. Also, implement a Ctrl + Enter for sending the message.\nPython code:\n
py\nimport streamlit as st\nfrom gradio_client import Client\nimport json\nimport os\n\n# 1. Set the page to wide mode\nst.set_page_config(layout="wide")\n\n# Initialize client\n@st.cache_resource\ndef get_client():\n return Client(\'yuntian-deng/o1mini\')\n\nclient = get_client()\n\n# File path for history\nHISTORY_FILE = \'history.json\'\n\n# Load history\ndef load_history():\n if os.path.exists(HISTORY_FILE):\n try:\n with open(HISTORY_FILE, \'r\') as f:\n history = json.load(f)\n # Ensure history is a list of dictionaries with \'user\' and \'bot\'\n if isinstance(history, list) and all(isinstance(entry, dict) for entry in history):\n return history\n except:\n pass\n return []\n\n# Save history\ndef save_history(history):\n with open(HISTORY_FILE, \'w\') as f:\n json.dump(history, f, indent=2)\n\n# Initialize session state\nif \'history\' not in st.session_state:\n st.session_state.history = load_history()\nif \'new_message\' not in st.session_state:\n st.session_state.new_message = \'\'\n\nst.title("Interactive Chat Interface")\n\nst.markdown("### Chat History")\n\n# 2. Display and edit history without "Exchange N" labels\nfor idx, entry in enumerate(st.session_state.history):\n user_key = f"user_{idx}"\n bot_key = f"bot_{idx}"\n # Display user and bot messages side by side\n col1, col2 = st.columns(2)\n with col1:\n st.text_area("User:", value=entry.get(\'user\', \'\'), key=user_key, height=100)\n with col2:\n st.text_area("Bot:", value=entry.get(\'bot\', \'\'), key=bot_key, height=100)\n # Update history with edited text\n st.session_state.history[idx][\'user\'] = st.session_state[user_key]\n st.session_state.history[idx][\'bot\'] = st.session_state[bot_key]\n\nst.markdown("---")\nst.markdown("### New Message")\n\n# 3. Input for new message with a unique key\nnew_message = st.text_area("Enter your message here:", height=100, key=\'new_message\')\n\n# 4. Button to send message with a unique key\nsend_button = st.button("Send", key=\'send_button\')\n\nif send_button:\n if new_message.strip() != "":\n # Append user message to history\n st.session_state.history.append({\'user\': new_message.strip(), \'bot\': ""})\n save_history(st.session_state.history)\n \n # Prepare inputs for the model\n inputs = []\n for entry in st.session_state.history:\n inputs.append(entry[\'user\'])\n if entry[\'bot\']:\n inputs.append(entry[\'bot\'])\n \n # Call the model\n try:\n result = client.predict(\n inputs=inputs,\n top_p=1,\n temperature=1,\n chat_counter=0,\n chatbot=[],\n api_name=\'/predict\'\n )\n if result and len(result) > 0 and len(result[0]) > 0 and len(result[0][0]) > 1:\n output = result[0][0][1]\n else:\n output = "Sorry, I couldn\'t generate a response."\n except Exception as e:\n output = f"Error: {str(e)}"\n \n # Append bot response to history\n st.session_state.history[-1][\'bot\'] = output\n save_history(st.session_state.history)\n \n # 4. Clear the input area\n st.session_state.new_message = \'\'\n st.rerun()\n\n# Button to save edits\nif st.button("Save Edits", key=\'save_edits_button\'):\n save_history(st.session_state.history)\n st.success("Edits saved successfully!")\n'] - Assistant