File size: 6,223 Bytes
f339dfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import os
import uuid
from datetime import datetime, timezone

import gradio as gr
from azure.cosmos import CosmosClient, PartitionKey
from azure.cosmos.exceptions import CosmosHttpResponseError
from dotenv import load_dotenv

load_dotenv()


def _get_env(name: str) -> str:
    value = os.getenv(name, "").strip()
    return value


def get_container():
    endpoint = _get_env("COSMOS_ENDPOINT")
    key = _get_env("COSMOS_KEY")
    db_name = _get_env("COSMOS_DATABASE")
    container_name = _get_env("COSMOS_CONTAINER")

    if not all([endpoint, key, db_name, container_name]):
        missing = [
            k
            for k in [
                "COSMOS_ENDPOINT",
                "COSMOS_KEY",
                "COSMOS_DATABASE",
                "COSMOS_CONTAINER",
            ]
            if not _get_env(k)
        ]
        raise ValueError(
            "Missing Cosmos configuration: " + ", ".join(missing)
        )

    client = CosmosClient(endpoint, credential=key)
    database = client.create_database_if_not_exists(id=db_name)
    container = database.create_container_if_not_exists(
        id=container_name,
        partition_key=PartitionKey(path="/id"),
    )
    return container


_container_cache = None


def submit_feedback(name, team, assistant_type, submit_anonymous, do_text, dont_text):
    name = (name or "").strip()
    team = (team or "").strip()
    assistant_type = (assistant_type or "").strip()
    do_text = (do_text or "").strip()
    dont_text = (dont_text or "").strip()

    if not do_text and not dont_text:
        return (
            (
                "<div style=\"padding:14px 16px;border-radius:8px;"
                "background:#ffe9e9;color:#7a1d1d;border:1px solid #f2bcbc;"
                "font-weight:600;\">Please enter a Dos or a Don'ts (or both) before submitting.</div>"
            ),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
        )

    if submit_anonymous:
        name = ""
        team = ""

    global _container_cache
    try:
        if _container_cache is None:
            _container_cache = get_container()

        item = {
            "id": str(uuid.uuid4()),
            "created_at": datetime.now(timezone.utc).isoformat(),
            "name": name or None,
            "team": team or None,
            "assistant_type": assistant_type or None,
            "do": do_text or None,
            "dont": dont_text or None,
        }
        _container_cache.create_item(body=item)
    except ValueError as exc:
        return (
            (
                "<div style=\"padding:14px 16px;border-radius:8px;"
                "background:#ffe9e9;color:#7a1d1d;border:1px solid #f2bcbc;"
                f"font-weight:600;\">Configuration error: {exc}</div>"
            ),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
        )
    except CosmosHttpResponseError as exc:
        return (
            (
                "<div style=\"padding:14px 16px;border-radius:8px;"
                "background:#ffe9e9;color:#7a1d1d;border:1px solid #f2bcbc;"
                f"font-weight:600;\">Cosmos DB error: {exc.message}</div>"
            ),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
        )
    except Exception as exc:  # noqa: BLE001
        return (
            (
                "<div style=\"padding:14px 16px;border-radius:8px;"
                "background:#ffe9e9;color:#7a1d1d;border:1px solid #f2bcbc;"
                f"font-weight:600;\">Unexpected error: {exc}</div>"
            ),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
        )

    return (
        (
            "<div style=\"padding:16px 18px;border-radius:10px;"
            "background:#e8f7ee;color:#0f5b2f;border:1px solid #bfe5cc;"
            "font-weight:700;font-size:16px;\">✅ Saved! Thanks for your feedback.</div>"
        ),
        gr.update(value=""),
        gr.update(value=""),
        gr.update(value="General"),
        gr.update(value=False),
        gr.update(value=""),
        gr.update(value=""),
    )


with gr.Blocks(title="AI Assistant Do/Don't Collection") as demo:
    gr.Markdown(
        "# AI Assistant Do/Don't Collection\n"
        "Share guidance for building our AI assistants. You can fill either field or both."
    )

    with gr.Row():
        name_input = gr.Textbox(label="Your name (optional)", placeholder="Jane Doe")
        team_input = gr.Textbox(label="Team / Org (optional)", placeholder="Product, IT, ...")
        assistant_type_input = gr.Dropdown(
            label="Assistant type (optional)",
            choices=[
                "General",
                "Resiliency Programs (WFPH, FORTIFIED, ..)",
                "Communication",
                "Code and Standards",
                "Internal Ops",
                "Research",
            ],
            value="General",
        )

    submit_anonymous_input = gr.Checkbox(
        label="Submit anonymously (ignore name and team)",
        value=False,
    )

    do_input = gr.Textbox(
        label="Dos",
        placeholder="What should we consider when creating AI assistants?",
        lines=6,
    )
    dont_input = gr.Textbox(
        label="Don'ts",
        placeholder="What should we avoid when creating AI assistants?",
        lines=6,
    )

    submit_btn = gr.Button("Submit")
    status = gr.HTML()

    submit_btn.click(
        fn=submit_feedback,
        inputs=[
            name_input,
            team_input,
            assistant_type_input,
            submit_anonymous_input,
            do_input,
            dont_input,
        ],
        outputs=[
            status,
            name_input,
            team_input,
            assistant_type_input,
            submit_anonymous_input,
            do_input,
            dont_input,
        ],
    )


if __name__ == "__main__":
    demo.launch()