Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -89,49 +89,66 @@ def chat_with_openrouter(prompt: str, selected_model: str, history: list):
|
|
| 89 |
return history, format_history(history)
|
| 90 |
|
| 91 |
# ------------------------------------------------------------------
|
| 92 |
-
# Interfaccia
|
| 93 |
# ------------------------------------------------------------------
|
| 94 |
def build_interface():
|
| 95 |
grouped_models = fetch_models_grouped()
|
| 96 |
-
|
| 97 |
default_model = grouped_models["reasoning"][0] if grouped_models["reasoning"] else ""
|
| 98 |
|
| 99 |
-
with gr.Blocks(title="NotExistChatter β Chat con
|
| 100 |
-
gr.Markdown("## π€ Project Adam β Chat dinamica con modelli
|
| 101 |
-
gr.Markdown("Seleziona un modello da uno dei due gruppi.")
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
choices=grouped_models["reasoning"],
|
| 106 |
-
label="π§ Modelli con Ragionamento",
|
| 107 |
-
interactive=True
|
| 108 |
-
)
|
| 109 |
-
casual_dropdown = gr.Dropdown(
|
| 110 |
-
choices=grouped_models["casual"],
|
| 111 |
-
label="β‘οΈ Modelli Generici / Casual",
|
| 112 |
-
interactive=True
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
-
reset_btn = gr.Button("π Nuova conversazione")
|
| 116 |
-
|
| 117 |
-
output_box = gr.Textbox(
|
| 118 |
-
label="Conversazione",
|
| 119 |
-
interactive=False,
|
| 120 |
-
lines=20,
|
| 121 |
-
max_lines=40
|
| 122 |
-
)
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
# Seleziona modello valido (da uno dei due)
|
| 135 |
def resolve_model(prompt, reasoning_model, casual_model, history):
|
| 136 |
model = reasoning_model or casual_model
|
| 137 |
if not model:
|
|
@@ -139,6 +156,24 @@ def build_interface():
|
|
| 139 |
return history, format_history(history)
|
| 140 |
return chat_with_openrouter(prompt, model, history)
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
send_btn.click(
|
| 143 |
fn=resolve_model,
|
| 144 |
inputs=[prompt_box, reasoning_dropdown, casual_dropdown, chat_history],
|
|
@@ -151,10 +186,22 @@ def build_interface():
|
|
| 151 |
outputs=[chat_history, output_box]
|
| 152 |
)
|
| 153 |
|
| 154 |
-
|
| 155 |
-
fn=
|
| 156 |
inputs=[],
|
| 157 |
-
outputs=[chat_history, output_box]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
)
|
| 159 |
|
| 160 |
return demo
|
|
|
|
| 89 |
return history, format_history(history)
|
| 90 |
|
| 91 |
# ------------------------------------------------------------------
|
| 92 |
+
# Interfaccia utente
|
| 93 |
# ------------------------------------------------------------------
|
| 94 |
def build_interface():
|
| 95 |
grouped_models = fetch_models_grouped()
|
|
|
|
| 96 |
default_model = grouped_models["reasoning"][0] if grouped_models["reasoning"] else ""
|
| 97 |
|
| 98 |
+
with gr.Blocks(title="NotExistChatter β Chat con storico") as demo:
|
| 99 |
+
gr.Markdown("## π€ Project Adam β Chat dinamica con modelli open source")
|
|
|
|
| 100 |
|
| 101 |
+
saved_chats = gr.State({})
|
| 102 |
+
current_chat_name = gr.State("Chat #1")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
with gr.Row():
|
| 105 |
+
# Sidebar sinistra
|
| 106 |
+
with gr.Column(scale=1):
|
| 107 |
+
gr.Markdown("### π¬ Chat salvate")
|
| 108 |
+
|
| 109 |
+
chat_selector = gr.Radio(
|
| 110 |
+
choices=[], label="Storico", interactive=True
|
| 111 |
+
)
|
| 112 |
+
load_btn = gr.Button("π Carica Chat")
|
| 113 |
+
new_chat_btn = gr.Button("π Nuova Chat")
|
| 114 |
+
save_name = gr.Textbox(label="πΎ Nome", placeholder="Es. brainstorming")
|
| 115 |
+
save_btn = gr.Button("πΎ Salva Chat")
|
| 116 |
+
|
| 117 |
+
# Area centrale
|
| 118 |
+
with gr.Column(scale=4):
|
| 119 |
+
with gr.Row():
|
| 120 |
+
reasoning_dropdown = gr.Dropdown(
|
| 121 |
+
choices=grouped_models["reasoning"],
|
| 122 |
+
label="π§ Modelli con Ragionamento",
|
| 123 |
+
interactive=True
|
| 124 |
+
)
|
| 125 |
+
casual_dropdown = gr.Dropdown(
|
| 126 |
+
choices=grouped_models["casual"],
|
| 127 |
+
label="β‘οΈ Modelli Generici",
|
| 128 |
+
interactive=True
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
output_box = gr.Textbox(
|
| 132 |
+
label="Conversazione",
|
| 133 |
+
interactive=False,
|
| 134 |
+
lines=20,
|
| 135 |
+
max_lines=40
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
prompt_box = gr.Textbox(
|
| 139 |
+
label="Prompt",
|
| 140 |
+
placeholder="Scrivi qui il tuo messaggio...",
|
| 141 |
+
lines=4,
|
| 142 |
+
max_lines=10
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
send_btn = gr.Button("Invia", variant="primary")
|
| 146 |
+
chat_history = gr.State([])
|
| 147 |
+
|
| 148 |
+
# ------------------------------------------------------------------
|
| 149 |
+
# Funzioni interazione
|
| 150 |
+
# ------------------------------------------------------------------
|
| 151 |
|
|
|
|
| 152 |
def resolve_model(prompt, reasoning_model, casual_model, history):
|
| 153 |
model = reasoning_model or casual_model
|
| 154 |
if not model:
|
|
|
|
| 156 |
return history, format_history(history)
|
| 157 |
return chat_with_openrouter(prompt, model, history)
|
| 158 |
|
| 159 |
+
def reset_chat():
|
| 160 |
+
return [], "", f"Chat #{os.urandom(2).hex()}", gr.update(value=None)
|
| 161 |
+
|
| 162 |
+
def save_chat_fn(chats, name, history):
|
| 163 |
+
if not name:
|
| 164 |
+
return chats, gr.update(choices=list(chats.keys())), "β οΈ Inserisci un nome valido."
|
| 165 |
+
chats[name] = history.copy()
|
| 166 |
+
return chats, gr.update(choices=list(chats.keys())), f"β
Chat salvata: '{name}'"
|
| 167 |
+
|
| 168 |
+
def load_chat_fn(chats, selected_name):
|
| 169 |
+
if not selected_name or selected_name not in chats:
|
| 170 |
+
return [], "", "β οΈ Seleziona una chat valida."
|
| 171 |
+
history = chats[selected_name]
|
| 172 |
+
return history, format_history(history), selected_name
|
| 173 |
+
|
| 174 |
+
# ------------------------------------------------------------------
|
| 175 |
+
# Eventi
|
| 176 |
+
# ------------------------------------------------------------------
|
| 177 |
send_btn.click(
|
| 178 |
fn=resolve_model,
|
| 179 |
inputs=[prompt_box, reasoning_dropdown, casual_dropdown, chat_history],
|
|
|
|
| 186 |
outputs=[chat_history, output_box]
|
| 187 |
)
|
| 188 |
|
| 189 |
+
new_chat_btn.click(
|
| 190 |
+
fn=reset_chat,
|
| 191 |
inputs=[],
|
| 192 |
+
outputs=[chat_history, output_box, current_chat_name, chat_selector]
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
save_btn.click(
|
| 196 |
+
fn=save_chat_fn,
|
| 197 |
+
inputs=[saved_chats, save_name, chat_history],
|
| 198 |
+
outputs=[saved_chats, chat_selector, output_box]
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
load_btn.click(
|
| 202 |
+
fn=load_chat_fn,
|
| 203 |
+
inputs=[saved_chats, chat_selector],
|
| 204 |
+
outputs=[chat_history, output_box, current_chat_name]
|
| 205 |
)
|
| 206 |
|
| 207 |
return demo
|