0Learn commited on
Commit
5aa8708
Β·
verified Β·
1 Parent(s): 4d65c15

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import random
3
+
4
+ # Initialize an empty list to store chat history
5
+ chat_history = []
6
+
7
+ # List of emojis for the emoji selector
8
+ emojis = ["πŸ˜€", "πŸ˜‚", "😊", "😍", "πŸ€”", "😎", "πŸ™Œ", "πŸ‘", "❀️", "πŸŽ‰"]
9
+
10
+ # Function to generate a response (placeholder for actual chatbot logic)
11
+ def get_bot_response(message):
12
+ responses = [
13
+ "That's interesting! Tell me more.",
14
+ "I see. How does that make you feel?",
15
+ "Thanks for sharing. What do you think about that?",
16
+ "Interesting perspective. Can you elaborate?",
17
+ "I understand. What would you like to talk about next?"
18
+ ]
19
+ return random.choice(responses)
20
+
21
+ # Function to handle user input and generate response
22
+ def chat(message, history):
23
+ history = history or []
24
+ # Add user message to chat history
25
+ history.append({"role": "user", "content": message})
26
+
27
+ # Get bot response
28
+ bot_message = get_bot_response(message)
29
+
30
+ # Add bot message to chat history
31
+ history.append({"role": "assistant", "content": bot_message})
32
+
33
+ return history, history
34
+
35
+ # Function to add selected emoji to input
36
+ def add_emoji(emoji, current_input):
37
+ return current_input + emoji
38
+
39
+ # Function to copy the last bot message
40
+ def copy_last_message(history):
41
+ if history and history[-1]["role"] == "assistant":
42
+ return history[-1]["content"]
43
+ return "No message to copy."
44
+
45
+ # Function to handle feedback
46
+ def handle_feedback(feedback, history):
47
+ if history:
48
+ last_message = history[-1]["content"]
49
+ print(f"Feedback for '{last_message}': {feedback}")
50
+ return history
51
+
52
+ # Create the Gradio interface
53
+ with gr.Blocks() as demo:
54
+ chatbot = gr.Chatbot()
55
+
56
+ with gr.Row():
57
+ with gr.Column(scale=0.9):
58
+ msg = gr.Textbox(label="Type your message here", placeholder="Enter your message...")
59
+ with gr.Column(scale=0.1):
60
+ emoji_dropdown = gr.Dropdown(choices=emojis, label="", interactive=True)
61
+
62
+ with gr.Row():
63
+ submit = gr.Button("Submit")
64
+ clear = gr.Button("Clear")
65
+
66
+ copy_button = gr.Button("Copy Last Bot Message")
67
+ copied_text = gr.Textbox(label="Copied Message")
68
+
69
+ with gr.Row():
70
+ thumbs_up = gr.Button("πŸ‘")
71
+ thumbs_down = gr.Button("πŸ‘Ž")
72
+
73
+ # Set up event handlers
74
+ submit.click(chat, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
75
+ msg.submit(chat, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
76
+ clear.click(lambda: [], None, chatbot, queue=False)
77
+ emoji_dropdown.change(add_emoji, inputs=[emoji_dropdown, msg], outputs=msg)
78
+ copy_button.click(copy_last_message, inputs=[chatbot], outputs=[copied_text])
79
+ thumbs_up.click(handle_feedback, inputs=["thumbs up", chatbot], outputs=[chatbot])
80
+ thumbs_down.click(handle_feedback, inputs=["thumbs down", chatbot], outputs=[chatbot])
81
+
82
+ # For Hugging Face Spaces compatibility
83
+ demo.launch()