arpit13 commited on
Commit
f063c08
·
verified ·
1 Parent(s): 3f6d4ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -27
app.py CHANGED
@@ -35,7 +35,7 @@ def chatbot(user_input, category, history=[]):
35
  bot_response = get_groq_response(user_input, category)
36
  history.append({"role": "user", "content": user_input})
37
  history.append({"role": "assistant", "content": bot_response})
38
- return history, history
39
 
40
  # Gradio Interface with enhanced styling
41
  chat_interface = gr.Blocks(css="""
@@ -103,39 +103,36 @@ with chat_interface:
103
  with gr.Row():
104
  chatbot_output = gr.Chatbot(label="Chat History", type="messages")
105
  with gr.Row():
106
- # Custom HTML button
107
- gr.HTML("""
108
- <button id="copy-btn" style="
109
- background: linear-gradient(90deg, #f9f9f9, #e6e6e6);
110
- color: #555;
111
- padding: 0.8rem 1.5rem;
112
- font-size: 1rem;
113
- font-weight: bold;
114
- border-radius: 20px;
115
- border: none;
116
- cursor: pointer;
117
- transition: transform 0.2s ease, background 0.2s ease;
118
- ">Copy Response</button>
119
  <script>
120
- document.getElementById('copy-btn').addEventListener('click', () => {
121
- const responses = document.querySelectorAll('.bot-message');
122
- if (responses.length > 0) {
123
- const lastResponse = responses[responses.length - 1].innerText;
124
- navigator.clipboard.writeText(lastResponse)
125
- .then(() => alert('Copied to clipboard!'))
126
- .catch(() => alert('Failed to copy!'));
127
- } else {
128
- alert('No bot response to copy!');
129
- }
130
- });
131
  </script>
132
  """)
133
 
134
- # Add functionality to handle interactions
135
  send_button.click(
136
- chatbot,
137
  inputs=[user_input, category_dropdown, chatbot_output],
138
  outputs=[chatbot_output, chatbot_output]
139
  )
140
 
 
 
 
 
 
 
141
  chat_interface.launch()
 
35
  bot_response = get_groq_response(user_input, category)
36
  history.append({"role": "user", "content": user_input})
37
  history.append({"role": "assistant", "content": bot_response})
38
+ return history, bot_response
39
 
40
  # Gradio Interface with enhanced styling
41
  chat_interface = gr.Blocks(css="""
 
103
  with gr.Row():
104
  chatbot_output = gr.Chatbot(label="Chat History", type="messages")
105
  with gr.Row():
106
+ copy_button = gr.Button("Copy Response")
107
+
108
+ # Add functionality to handle interactions
109
+ def handle_chat(user_input, category, history):
110
+ if not user_input.strip():
111
+ return history, history
112
+ updated_history, bot_response = chatbot(user_input, category, history)
113
+ return updated_history, updated_history
114
+
115
+ def handle_copy(last_bot_response):
116
+ # Copy the last bot response to clipboard using the JavaScript in Gradio.
117
+ return gr.HTML(f"""
 
118
  <script>
119
+ const copyText = '{last_bot_response}';
120
+ navigator.clipboard.writeText(copyText)
121
+ .then(() => alert('Copied to clipboard!'))
122
+ .catch(() => alert('Failed to copy!'));
 
 
 
 
 
 
 
123
  </script>
124
  """)
125
 
 
126
  send_button.click(
127
+ handle_chat,
128
  inputs=[user_input, category_dropdown, chatbot_output],
129
  outputs=[chatbot_output, chatbot_output]
130
  )
131
 
132
+ copy_button.click(
133
+ handle_copy,
134
+ inputs=[chatbot_output],
135
+ outputs=[]
136
+ )
137
+
138
  chat_interface.launch()