saherPervaiz commited on
Commit
d94240d
·
verified ·
1 Parent(s): 61f441d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -9
app.py CHANGED
@@ -2,31 +2,68 @@ import gradio as gr
2
  from utils import generate_response
3
 
4
  # Define chatbot interaction function
5
- def chat(user_input):
6
- response = generate_response(user_input)
7
- return response
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # Create Gradio interface
10
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
11
  gr.Markdown("## 🤖 Professional Groq Chatbot")
12
- gr.Markdown("Type your message below and chat with the AI!")
13
 
14
  with gr.Row():
15
  user_input = gr.Textbox(
16
  label="Your Message",
17
  placeholder="Ask me anything!",
18
  lines=2,
 
19
  )
20
  submit_button = gr.Button("Send")
 
21
 
22
  chatbot_output = gr.Textbox(
23
- label="Response",
24
- placeholder="AI's response will appear here.",
25
- lines=6,
26
  interactive=False,
 
27
  )
28
 
29
- submit_button.click(fn=chat, inputs=[user_input], outputs=[chatbot_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Launch locally
32
  if __name__ == "__main__":
 
2
  from utils import generate_response
3
 
4
  # Define chatbot interaction function
5
+ def chat(user_input, chat_history):
6
+ try:
7
+ # Generate response
8
+ response = generate_response(user_input)
9
+ # Update chat history
10
+ chat_history.append(("User", user_input))
11
+ chat_history.append(("AI", response))
12
+ # Format chat history for display
13
+ formatted_history = "\n".join(
14
+ [f"{role}: {message}" for role, message in chat_history]
15
+ )
16
+ return formatted_history, chat_history
17
+ except Exception as e:
18
+ return f"Error: {e}", chat_history
19
 
20
  # Create Gradio interface
21
+ with gr.Blocks(css="""
22
+ #chatbox {
23
+ height: 400px;
24
+ overflow-y: auto;
25
+ border: 1px solid #ccc;
26
+ padding: 10px;
27
+ border-radius: 8px;
28
+ background-color: #f9f9f9;
29
+ }
30
+ """) as demo:
31
  gr.Markdown("## 🤖 Professional Groq Chatbot")
32
+ gr.Markdown("Type your message below to chat with the AI!")
33
 
34
  with gr.Row():
35
  user_input = gr.Textbox(
36
  label="Your Message",
37
  placeholder="Ask me anything!",
38
  lines=2,
39
+ interactive=True,
40
  )
41
  submit_button = gr.Button("Send")
42
+ clear_button = gr.Button("Clear Chat")
43
 
44
  chatbot_output = gr.Textbox(
45
+ label="Chat History",
46
+ placeholder="AI's responses will appear here.",
47
+ lines=15,
48
  interactive=False,
49
+ elem_id="chatbox",
50
  )
51
 
52
+ # State to hold chat history
53
+ chat_history = gr.State([])
54
+
55
+ # Button functionalities
56
+ submit_button.click(
57
+ fn=chat,
58
+ inputs=[user_input, chat_history],
59
+ outputs=[chatbot_output, chat_history],
60
+ )
61
+
62
+ clear_button.click(
63
+ fn=lambda: ("", []),
64
+ inputs=[],
65
+ outputs=[chatbot_output, chat_history],
66
+ )
67
 
68
  # Launch locally
69
  if __name__ == "__main__":