Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
):
|
| 10 |
-
"""
|
| 11 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 12 |
-
"""
|
| 13 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 14 |
-
|
| 15 |
-
messages = [{"role": "system", "content": "You are an helpful assistant"}]
|
| 16 |
-
|
| 17 |
-
# messages.extend(history)
|
| 18 |
-
|
| 19 |
-
messages.append({"role": "user", "content": message})
|
| 20 |
-
|
| 21 |
-
response = ""
|
| 22 |
-
|
| 23 |
-
for message in client.chat_completion(
|
| 24 |
-
messages,
|
| 25 |
-
max_tokens=2048,
|
| 26 |
-
stream=True,
|
| 27 |
-
temperature=0.0,
|
| 28 |
-
top_p=None,
|
| 29 |
-
):
|
| 30 |
-
choices = message.choices
|
| 31 |
-
token = ""
|
| 32 |
-
if len(choices) and choices[0].delta.content:
|
| 33 |
-
token = choices[0].delta.content
|
| 34 |
-
|
| 35 |
-
response += token
|
| 36 |
-
yield response
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"""
|
| 40 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 41 |
-
"""
|
| 42 |
-
chatbot = gr.ChatInterface(
|
| 43 |
-
respond,
|
| 44 |
-
type="messages",
|
| 45 |
-
#additional_inputs=[
|
| 46 |
-
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 47 |
-
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 48 |
-
# gr.Slider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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
+
def load_registered_teams():
|
| 9 |
+
return ["admin", "team1"]
|
| 10 |
+
|
| 11 |
+
REGISTERED_TEAMS = load_registered_teams()
|
| 12 |
|
| 13 |
+
INTRO_DEFAULT = "### The Safety Game · Prompt Submission\n"
|
| 14 |
+
INTRO_VERIFIED_TEMPLATE = (
|
| 15 |
+
"### The Safety Game · Prompt Submission\n"
|
| 16 |
+
"Benvenuti, **{team}**. Ora potete inviare il vostro prompt per la valutazione."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
+
PROMPT_LOCKED = (
|
| 20 |
+
gr.update(visible=False),
|
| 21 |
+
gr.update(value="", interactive=False),
|
| 22 |
+
gr.update(interactive=False),
|
| 23 |
+
gr.update(value="", interactive=False),
|
| 24 |
+
gr.update(interactive=False),
|
| 25 |
+
)
|
| 26 |
+
PROMPT_UNLOCKED = (
|
| 27 |
+
gr.update(visible=True),
|
| 28 |
+
gr.update(value="", interactive=True),
|
| 29 |
+
gr.update(interactive=True),
|
| 30 |
+
gr.update(value="", interactive=False),
|
| 31 |
+
gr.update(interactive=False),
|
| 32 |
+
)
|
| 33 |
+
TEAM_PANEL_VISIBLE = gr.update()
|
| 34 |
+
TEAM_PANEL_HIDDEN = gr.update(visible=False)
|
| 35 |
|
| 36 |
|
| 37 |
+
def generate_response(prompt: str):
|
| 38 |
+
messages = [
|
| 39 |
+
{"role": "system", "content": "You are an helpful assistant"},
|
| 40 |
+
{"role": "user", "content": prompt},
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
client = InferenceClient(
|
| 45 |
+
model="openai/gpt-oss-20b",
|
| 46 |
+
token=os.getenv("HF_TOKEN"),
|
| 47 |
+
)
|
| 48 |
+
out = client.chat_completion(
|
| 49 |
+
messages,
|
| 50 |
+
max_tokens=256,
|
| 51 |
+
temperature=0.0,
|
| 52 |
+
stream=False,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
response = ""
|
| 56 |
+
for c in out.choices:
|
| 57 |
+
response += c.message["content"]
|
| 58 |
+
return response
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"Errore durante la generazione della risposta: {e}")
|
| 62 |
+
return (
|
| 63 |
+
"⚠️ Impossibile generare la risposta in questo momento. Riprova più tardi."
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def verify_team(team_name: str):
|
| 68 |
+
team_name = team_name.strip()
|
| 69 |
+
|
| 70 |
+
if not team_name:
|
| 71 |
+
return (
|
| 72 |
+
"⚠️ Inserisci il nome del team registrato prima di procedere.",
|
| 73 |
+
*PROMPT_LOCKED,
|
| 74 |
+
TEAM_PANEL_VISIBLE,
|
| 75 |
+
gr.update(value=INTRO_DEFAULT),
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
official_name = REGISTERED_TEAMS.get(team_name.casefold())
|
| 79 |
+
|
| 80 |
+
if official_name:
|
| 81 |
+
return (
|
| 82 |
+
"✅ Accesso abilitato. Puoi procedere con il prompt.",
|
| 83 |
+
*PROMPT_UNLOCKED,
|
| 84 |
+
TEAM_PANEL_HIDDEN,
|
| 85 |
+
gr.update(value=INTRO_VERIFIED_TEMPLATE.format(team=official_name)),
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
return (
|
| 89 |
+
"❌ Il team indicato non risulta registrato. Verifica il nome oppure contatta gli organizzatori per assistenza.",
|
| 90 |
+
*PROMPT_LOCKED,
|
| 91 |
+
TEAM_PANEL_VISIBLE,
|
| 92 |
+
gr.update(value=INTRO_DEFAULT),
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def answer_prompt(prompt: str):
|
| 97 |
+
prompt = prompt.strip()
|
| 98 |
+
|
| 99 |
+
if not prompt:
|
| 100 |
+
return (
|
| 101 |
+
"⚠️ Scrivi un prompt prima di inviarlo.",
|
| 102 |
+
gr.update(),
|
| 103 |
+
gr.update(),
|
| 104 |
+
gr.update(),
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
reply = generate_response(prompt)
|
| 108 |
+
return (
|
| 109 |
+
reply,
|
| 110 |
+
gr.update(interactive=False),
|
| 111 |
+
gr.update(interactive=False),
|
| 112 |
+
gr.update(interactive=True),
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def reset_session():
|
| 117 |
+
"""Reset submission state and clear prompt/response fields."""
|
| 118 |
+
return (
|
| 119 |
+
"✅ Sessione azzerata. Puoi inviare un nuovo prompt.",
|
| 120 |
+
gr.update(interactive=True),
|
| 121 |
+
gr.update(value="", interactive=True),
|
| 122 |
+
gr.update(value=""),
|
| 123 |
+
gr.update(interactive=False),
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def main():
|
| 129 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
| 130 |
+
intro_message = gr.Markdown(INTRO_DEFAULT)
|
| 131 |
+
|
| 132 |
+
with gr.Column(variant="panel") as team_panel:
|
| 133 |
+
status_message = gr.Markdown("**Inserisci il nome del tuo team**")
|
| 134 |
+
|
| 135 |
+
with gr.Row():
|
| 136 |
+
team_name = gr.Textbox(
|
| 137 |
+
container=False,
|
| 138 |
+
placeholder="Nome del team",
|
| 139 |
+
lines=1,
|
| 140 |
+
max_lines=1,
|
| 141 |
+
scale=7,
|
| 142 |
+
)
|
| 143 |
+
verify_button = gr.Button("Verifica", variant="primary")
|
| 144 |
+
|
| 145 |
+
with gr.Column(variant="panel", visible=False) as prompt_group:
|
| 146 |
+
gr.Markdown("**Prompt**")
|
| 147 |
+
prompt_box = gr.Textbox(
|
| 148 |
+
label="Prompt",
|
| 149 |
+
placeholder="Inserisci qui il prompt da testare",
|
| 150 |
+
container=False,
|
| 151 |
+
lines=6,
|
| 152 |
+
)
|
| 153 |
+
with gr.Row():
|
| 154 |
+
submit_button = gr.Button("Genera", variant="primary")
|
| 155 |
+
reset_button = gr.Button("Reset", variant="secondary", interactive=False)
|
| 156 |
+
gr.Markdown("**Risposta del chatbot**")
|
| 157 |
+
response_box = gr.Textbox(
|
| 158 |
+
label="Risposta del chatbot",
|
| 159 |
+
container=False,
|
| 160 |
+
lines=8,
|
| 161 |
+
interactive=False,
|
| 162 |
+
placeholder="",
|
| 163 |
+
show_copy_button=True,
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
verify_button.click(
|
| 167 |
+
verify_team,
|
| 168 |
+
show_progress="hidden",
|
| 169 |
+
inputs=[team_name],
|
| 170 |
+
outputs=[
|
| 171 |
+
status_message,
|
| 172 |
+
prompt_group,
|
| 173 |
+
prompt_box,
|
| 174 |
+
submit_button,
|
| 175 |
+
response_box,
|
| 176 |
+
reset_button,
|
| 177 |
+
team_panel,
|
| 178 |
+
intro_message,
|
| 179 |
+
],
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
submit_button.click(
|
| 183 |
+
answer_prompt,
|
| 184 |
+
inputs=[prompt_box],
|
| 185 |
+
outputs=[
|
| 186 |
+
response_box,
|
| 187 |
+
submit_button,
|
| 188 |
+
prompt_box,
|
| 189 |
+
reset_button,
|
| 190 |
+
],
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
reset_button.click(
|
| 194 |
+
reset_session,
|
| 195 |
+
outputs=[
|
| 196 |
+
status_message,
|
| 197 |
+
submit_button,
|
| 198 |
+
prompt_box,
|
| 199 |
+
response_box,
|
| 200 |
+
reset_button,
|
| 201 |
+
],
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
demo.launch()
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
if __name__ == "__main__":
|
| 208 |
+
main()
|