arpit13 commited on
Commit
6434192
·
verified ·
1 Parent(s): fac9e90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -35
app.py CHANGED
@@ -33,9 +33,8 @@ def get_groq_response(message, category):
33
  # Chatbot function
34
  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, bot_response
39
 
40
  # Gradio Interface with enhanced styling
41
  chat_interface = gr.Blocks(css="""
@@ -53,8 +52,8 @@ body {
53
  100% { background-position: 0% 50%; }
54
  }
55
  button {
56
- background: linear-gradient(90deg, #a1c4fd, #c2e9fb);
57
- color: #333;
58
  padding: 0.8rem 1.5rem;
59
  font-size: 1rem;
60
  font-weight: bold;
@@ -64,8 +63,8 @@ button {
64
  transition: transform 0.2s ease, background 0.2s ease;
65
  }
66
  button:hover {
67
- background: linear-gradient(90deg, #c2e9fb, #a1c4fd);
68
- transform: scale(1.05);
69
  }
70
  header {
71
  text-align: center;
@@ -85,6 +84,19 @@ header {
85
  max-height: 300px;
86
  overflow-y: auto;
87
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  """)
89
 
90
  with chat_interface:
@@ -101,33 +113,27 @@ with chat_interface:
101
  with gr.Row():
102
  send_button = gr.Button("Send")
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
- with gr.Row():
108
- copy_message = gr.HTML("") # To display the confirmation message
 
 
 
 
 
 
 
 
109
 
110
  # Add functionality to handle interactions
111
  def handle_chat(user_input, category, history):
112
  if not user_input.strip():
113
  return history, history
114
- updated_history, bot_response = chatbot(user_input, category, history)
115
- return updated_history, updated_history
116
-
117
- def handle_copy(last_bot_response):
118
- # Copy the last bot response to clipboard using the JavaScript in Gradio.
119
- return gr.HTML(f"""
120
- <script>
121
- const copyText = '{last_bot_response}';
122
- navigator.clipboard.writeText(copyText)
123
- .then(() => {{
124
- alert('Response copied to clipboard!');
125
- }})
126
- .catch(() => {{
127
- alert('Failed to copy!');
128
- }});
129
- </script>
130
- """), "Response copied to clipboard!"
131
 
132
  send_button.click(
133
  handle_chat,
@@ -135,10 +141,4 @@ with chat_interface:
135
  outputs=[chatbot_output, chatbot_output]
136
  )
137
 
138
- copy_button.click(
139
- handle_copy,
140
- inputs=[chatbot_output],
141
- outputs=[copy_message] # Display the copy confirmation message
142
- )
143
-
144
  chat_interface.launch()
 
33
  # Chatbot function
34
  def chatbot(user_input, category, history=[]):
35
  bot_response = get_groq_response(user_input, category)
36
+ history.append((f"You: {user_input}", f"Bot: {bot_response}"))
37
+ return history, history
 
38
 
39
  # Gradio Interface with enhanced styling
40
  chat_interface = gr.Blocks(css="""
 
52
  100% { background-position: 0% 50%; }
53
  }
54
  button {
55
+ background: linear-gradient(90deg, #6a11cb, #2575fc);
56
+ color: white;
57
  padding: 0.8rem 1.5rem;
58
  font-size: 1rem;
59
  font-weight: bold;
 
63
  transition: transform 0.2s ease, background 0.2s ease;
64
  }
65
  button:hover {
66
+ background: linear-gradient(90deg, #2575fc, #6a11cb);
67
+ transform: scale(1.1);
68
  }
69
  header {
70
  text-align: center;
 
84
  max-height: 300px;
85
  overflow-y: auto;
86
  }
87
+ .copy-btn {
88
+ margin-left: 5px;
89
+ background: #ff7eb3;
90
+ color: white;
91
+ border: none;
92
+ border-radius: 5px;
93
+ padding: 5px 10px;
94
+ cursor: pointer;
95
+ font-size: 0.9rem;
96
+ }
97
+ .copy-btn:hover {
98
+ background: #ff4f81;
99
+ }
100
  """)
101
 
102
  with chat_interface:
 
113
  with gr.Row():
114
  send_button = gr.Button("Send")
115
  with gr.Row():
116
+ with gr.Column() as chat_container:
117
+ chatbot_output = gr.Chatbot(label="Chat History")
118
+
119
+ # Add copy-to-clipboard button
120
+ def add_copy_buttons(history):
121
+ enhanced_history = []
122
+ for user, bot in history:
123
+ copy_button = (
124
+ f'<button class="copy-btn" onclick="navigator.clipboard.writeText(`{bot}`)">Copy</button>'
125
+ )
126
+ bot_with_copy = f"{bot} {copy_button}"
127
+ enhanced_history.append((user, bot_with_copy))
128
+ return enhanced_history
129
 
130
  # Add functionality to handle interactions
131
  def handle_chat(user_input, category, history):
132
  if not user_input.strip():
133
  return history, history
134
+ updated_history, _ = chatbot(user_input, category, history)
135
+ enhanced_history = add_copy_buttons(updated_history)
136
+ return enhanced_history, enhanced_history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
  send_button.click(
139
  handle_chat,
 
141
  outputs=[chatbot_output, chatbot_output]
142
  )
143
 
 
 
 
 
 
 
144
  chat_interface.launch()