AlphaNightLight commited on
Commit
dee9b74
·
verified ·
1 Parent(s): 0db7d7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -30
app.py CHANGED
@@ -69,61 +69,77 @@ demo = gr.ChatInterface(
69
 
70
  demo.launch()
71
  """
 
72
  import gradio as gr
73
- import csv, time
 
 
 
 
 
74
 
75
 
76
  # -------------------------------------------------
77
- # Your chatbot model
78
  # -------------------------------------------------
79
  def chat_model(message, history):
80
- # Example model: reverse text
81
- return message[::-1]
82
 
83
 
84
  # -------------------------------------------------
85
- # Save flagged conversation
86
  # -------------------------------------------------
87
- def flag_conversation(history, reason):
88
- # Save the entire chat + reason
89
- with open("chat_flags.csv", "a", newline="") as f:
90
- writer = csv.writer(f)
91
- writer.writerow([
92
- time.time(),
93
- history, # full conversation as a Python list
94
- reason
95
- ])
96
- return "Flag recorded!"
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
 
99
  # -------------------------------------------------
100
  # UI
101
  # -------------------------------------------------
102
  with gr.Blocks() as app:
 
103
 
104
- gr.Markdown("### Chatbot with Multi-Reason Flagging")
105
-
106
- # Create chat interface
107
  chat = gr.ChatInterface(
108
  fn=chat_model,
109
  chatbot=gr.Chatbot(height=400),
110
  textbox=gr.Textbox(placeholder="Say something..."),
111
- title="Demo Chatbot",
112
- description="Supports multi-reason flagging."
113
  )
114
 
115
- # Flag button
116
  flag_btn = gr.Button("Flag Conversation", variant="stop")
117
 
118
- # Modal for selecting flag reason
119
  with gr.Group(visible=False) as flag_modal:
120
  gr.Markdown("### Why are you flagging this conversation?")
121
  reason_dd = gr.Dropdown(
122
  choices=[
123
  "Offensive / Toxic",
124
- "Incorrect",
125
  "Hallucination",
126
- "Safety Issue",
127
  "Biased Output",
128
  "Other"
129
  ],
@@ -132,21 +148,22 @@ with gr.Blocks() as app:
132
  submit_flag = gr.Button("Submit Flag", variant="primary")
133
  cancel_flag = gr.Button("Cancel")
134
 
 
135
 
136
  # Show modal
137
  flag_btn.click(lambda: gr.update(visible=True), None, flag_modal)
138
 
139
- # Cancel hides modal
140
  cancel_flag.click(lambda: gr.update(visible=False), None, flag_modal)
141
 
142
- # Submit flag → save CSV → hide modal → show status
143
- flag_status = gr.Textbox(label="Flagging Status", visible=True)
144
-
145
  submit_flag.click(
146
- lambda chat_history, reason: flag_conversation(chat_history, reason),
147
  inputs=[chat.chatbot, reason_dd],
148
  outputs=flag_status
149
- ).then(lambda: gr.update(visible=False), None, flag_modal)
 
 
150
 
151
 
152
  app.launch()
 
69
 
70
  demo.launch()
71
  """
72
+
73
  import gradio as gr
74
+ import json, time, os
75
+ from huggingface_hub import HfApi
76
+
77
+ DATASET_ID = "dn/flags" # your dataset
78
+ SECRET_TOKEN = os.getenv("HF_TOKEN") # token stored in your Space secrets
79
+ LOCAL_JSONL = "flags.jsonl" # local buffer file before upload
80
 
81
 
82
  # -------------------------------------------------
83
+ # Example Chat Model
84
  # -------------------------------------------------
85
  def chat_model(message, history):
86
+ return message[::-1] # dummy: reverse text
 
87
 
88
 
89
  # -------------------------------------------------
90
+ # Save flags to Hugging Face Dataset
91
  # -------------------------------------------------
92
+ def save_flag_to_dataset(history, reason):
93
+ # Prepare flag record
94
+ record = {
95
+ "timestamp": time.time(),
96
+ "history": history,
97
+ "reason": reason
98
+ }
99
+
100
+ # Append locally to JSONL
101
+ with open(LOCAL_JSONL, "a") as f:
102
+ f.write(json.dumps(record) + "\n")
103
+
104
+ # Upload to the dataset (overwrite the JSONL with the new one)
105
+ api = HfApi()
106
+ api.upload_file(
107
+ path_or_fileobj=LOCAL_JSONL,
108
+ path_in_repo=LOCAL_JSONL, # same name in the dataset
109
+ repo_id=DATASET_ID,
110
+ repo_type="dataset",
111
+ token=SECRET_TOKEN
112
+ )
113
+
114
+ return "Flag successfully saved to dataset!"
115
 
116
 
117
  # -------------------------------------------------
118
  # UI
119
  # -------------------------------------------------
120
  with gr.Blocks() as app:
121
+ gr.Markdown("### Chatbot with Multi-Reason Flagging → HF Dataset")
122
 
123
+ # Chat UI
 
 
124
  chat = gr.ChatInterface(
125
  fn=chat_model,
126
  chatbot=gr.Chatbot(height=400),
127
  textbox=gr.Textbox(placeholder="Say something..."),
128
+ title="Chatbot",
129
+ description="Flags saved to the dn/flags dataset."
130
  )
131
 
132
+ # Flagging UI components
133
  flag_btn = gr.Button("Flag Conversation", variant="stop")
134
 
 
135
  with gr.Group(visible=False) as flag_modal:
136
  gr.Markdown("### Why are you flagging this conversation?")
137
  reason_dd = gr.Dropdown(
138
  choices=[
139
  "Offensive / Toxic",
140
+ "Incorrect Output",
141
  "Hallucination",
142
+ "Safety Concern",
143
  "Biased Output",
144
  "Other"
145
  ],
 
148
  submit_flag = gr.Button("Submit Flag", variant="primary")
149
  cancel_flag = gr.Button("Cancel")
150
 
151
+ flag_status = gr.Textbox(label="Flag Status", visible=True)
152
 
153
  # Show modal
154
  flag_btn.click(lambda: gr.update(visible=True), None, flag_modal)
155
 
156
+ # Hide modal
157
  cancel_flag.click(lambda: gr.update(visible=False), None, flag_modal)
158
 
159
+ # Submit → save to dataset → hide modal
 
 
160
  submit_flag.click(
161
+ lambda history, reason: save_flag_to_dataset(history, reason),
162
  inputs=[chat.chatbot, reason_dd],
163
  outputs=flag_status
164
+ ).then(
165
+ lambda: gr.update(visible=False), None, flag_modal
166
+ )
167
 
168
 
169
  app.launch()