AlanOC commited on
Commit
0589140
1 Parent(s): 6e0b636

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -1
app.py CHANGED
@@ -32,7 +32,25 @@ from langchain.prompts import HumanMessagePromptTemplate
32
  from langchain.prompts import ChatMessagePromptTemplate
33
  from langchain.prompts import ChatPromptTemplate
34
 
35
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  # Retrieve the API key from the environment variables
38
  api_key = os.getenv("OPENAI_API_KEY")
@@ -149,6 +167,10 @@ def main():
149
  st.write("Answer:")
150
  st.write(ai_response)
151
 
 
 
 
 
152
  # Add a button to clear chat history
153
  if st.button("Clear Chat"):
154
  st.session_state['chat_history'] = [] # Reset the chat history
 
32
  from langchain.prompts import ChatMessagePromptTemplate
33
  from langchain.prompts import ChatPromptTemplate
34
 
35
+ # Function to create a copy-to-clipboard button
36
+ def create_copy_button(text_to_copy):
37
+ button_uuid = str(uuid.uuid4()).replace("-", "")
38
+ button_id = re.sub('\D', '', button_uuid)
39
+ copy_js = f"""
40
+ <script>
41
+ function copyToClipboard{button_id}() {{
42
+ const str = `{text_to_copy}`;
43
+ const el = document.createElement('textarea');
44
+ el.value = str;
45
+ document.body.appendChild(el);
46
+ el.select();
47
+ document.execCommand('copy');
48
+ document.body.removeChild(el);
49
+ }}
50
+ </script>
51
+ <button onclick="copyToClipboard{button_id}()">Copy</button>
52
+ """
53
+ return copy_js
54
 
55
  # Retrieve the API key from the environment variables
56
  api_key = os.getenv("OPENAI_API_KEY")
 
167
  st.write("Answer:")
168
  st.write(ai_response)
169
 
170
+ # Create and display the copy button
171
+ copy_button_html = create_copy_button(ai_response)
172
+ components.html(copy_button_html, height=30)
173
+
174
  # Add a button to clear chat history
175
  if st.button("Clear Chat"):
176
  st.session_state['chat_history'] = [] # Reset the chat history