burtenshaw commited on
Commit
0689df8
1 Parent(s): 8c1caf3

basic implementation of a chat app with message flagging

Browse files
Files changed (7) hide show
  1. .python-version +1 -0
  2. .vscode/launch.json +15 -0
  3. README.md +0 -0
  4. app.py +118 -0
  5. flag.png +0 -0
  6. pyproject.toml +10 -0
  7. uv.lock +0 -0
.python-version ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.11
.vscode/launch.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "name": "Python Debugger: Current File",
9
+ "type": "debugpy",
10
+ "request": "launch",
11
+ "program": "${file}",
12
+ "console": "integratedTerminal"
13
+ }
14
+ ]
15
+ }
README.md ADDED
File without changes
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from datasets import Dataset
3
+ from pandas import DataFrame
4
+
5
+ DATASET_REPO_ID = "username/dataset-name"
6
+
7
+
8
+ def add_user_message(history, message):
9
+ """Add a user message to the chat history"""
10
+ if message["text"] is not None:
11
+ content = message["text"]
12
+ history.append(gr.ChatMessage(role="user", content=content))
13
+ return history, gr.Textbox(value=None, interactive=False)
14
+
15
+
16
+ def respond_system_message(history: list):
17
+ """Respond to the user message with a system message"""
18
+
19
+ ##############################
20
+ # FAKE RESPONSE
21
+ response = "**That's cool!**"
22
+ ##############################
23
+
24
+ # TODO: Add a response to the user message
25
+
26
+ message = gr.ChatMessage(role="assistant", content=response)
27
+ history.append(message)
28
+ return history
29
+
30
+
31
+ def wrangle_like_data(x: gr.LikeData, history) -> DataFrame:
32
+ """Wrangle conversations and liked data into a DataFrame"""
33
+
34
+ liked_index = x.index[0]
35
+
36
+ output_data = []
37
+
38
+ for idx, message in enumerate(history):
39
+ if idx == liked_index:
40
+ message["liked"] = x.liked
41
+ else:
42
+ message["liked"] = False
43
+
44
+ output_data.append(dict(message))
45
+
46
+ del message["metadata"]
47
+
48
+ return DataFrame(data=output_data)
49
+
50
+
51
+ def submit_conversation(dataframe):
52
+ """ "Submit the conversation to dataset repo"""
53
+ print(dataframe)
54
+ # TODO: Submit the conversation to the dataset repo
55
+ # Dataset.from_pandas(dataframe).push_to_hub(repo_id=DATASET_REPO_ID)
56
+
57
+
58
+ with gr.Blocks(
59
+ css="""
60
+ button[aria-label="dislike"] {
61
+ display: none;
62
+ }
63
+ button[aria-label="like"] {
64
+ width: auto;
65
+ }
66
+ button[aria-label="like"] svg {
67
+ display: none;
68
+ }
69
+ button[aria-label="like"]::before {
70
+ content: "⛔️";
71
+ font-size: 1.5em;
72
+ display: inline-block;
73
+ }
74
+ """
75
+ ) as demo:
76
+
77
+ ##############################
78
+ # Chatbot
79
+ ##############################
80
+
81
+ chatbot = gr.Chatbot(
82
+ elem_id="chatbot",
83
+ bubble_full_width=False,
84
+ type="messages",
85
+ )
86
+
87
+ chat_input = gr.Textbox(
88
+ interactive=True,
89
+ placeholder="Enter a message...",
90
+ show_label=True,
91
+ )
92
+
93
+ chat_msg = chat_input.submit(
94
+ fn=add_user_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input]
95
+ )
96
+
97
+ bot_msg = chat_msg.then(
98
+ respond_system_message, chatbot, chatbot, api_name="bot_response"
99
+ )
100
+
101
+ bot_msg.then(lambda: gr.Textbox(interactive=True), None, [chat_input])
102
+
103
+ ##############################
104
+ # Deal with feedback
105
+ ##############################
106
+
107
+ dataframe = gr.DataFrame()
108
+
109
+ chatbot.like(
110
+ fn=wrangle_like_data,
111
+ inputs=[chatbot],
112
+ outputs=[dataframe],
113
+ like_user_message=False,
114
+ )
115
+
116
+ gr.Button().click(fn=submit_conversation, inputs=[dataframe], outputs=None)
117
+
118
+ demo.launch()
flag.png ADDED
pyproject.toml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "ohp"
3
+ version = "0.1.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ requires-python = ">=3.11"
7
+ dependencies = [
8
+ "datasets>=3.1.0",
9
+ "gradio>=5.6.0",
10
+ ]
uv.lock ADDED
The diff for this file is too large to render. See raw diff