Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@
|
|
| 7 |
# demo.launch()
|
| 8 |
|
| 9 |
|
| 10 |
-
|
| 11 |
import gradio as gr
|
| 12 |
from llama_cpp import Llama
|
| 13 |
|
|
@@ -68,3 +68,85 @@ demo = gr.ChatInterface(
|
|
| 68 |
)
|
| 69 |
|
| 70 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# demo.launch()
|
| 8 |
|
| 9 |
|
| 10 |
+
"""
|
| 11 |
import gradio as gr
|
| 12 |
from llama_cpp import Llama
|
| 13 |
|
|
|
|
| 68 |
)
|
| 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 |
+
],
|
| 130 |
+
label="Reason"
|
| 131 |
+
)
|
| 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()
|